Home Forums WoodMart support forum Create a new widget in child theme

Create a new widget in child theme

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #402534

    thomasgaillard
    Participant

    Hello, I have to custom one of your elementor widget : brands.
    If I custom the class-products-brands.php directly in the parent theme that’s ok but it’s not really good to do that.

    I tried to add the same file, same filename and same path in the child theme but it’s not override the parent’s file.

    How could I do that properly please ?
    Thanks

    #402708

    Hello,

    Could you kindly describe your purpose in more detail? How do you want it should work?

    Best Regards

    #402738

    thomasgaillard
    Participant

    Here you are 3 screenshots to help you.
    I added in the main menu a Brands widget in Elementor to list all the brands

    I have to put a little of specific to show a Swiss flag on the right of Swiss brand (based on ACF attribute)
    I found the .php file in the parent theme to custom that point but it’s very ugly to custom the parent theme.

    So two solutions:

    Could you help me to custom the child theme ? Override to .php file (I already try to put the .php file with all the path in child theme : but not ok)

    or please give me some help to create a new custom widget in the child theme and then, I will replace the standard by the custom in elementor

    Thanks to you

    Attachments:
    You must be logged in to view attached files.
    #402972

    Hello,

    Please add this code to the functions.php of the child theme and add HTML blocks there:

    if ( ! function_exists( 'wd_products_brands_render' ) ) {
    	function wd_products_brands_render( $widget_content, $widget ) {
    		if ( 'wd_products_brands' === $widget->get_name() ) {
    			ob_start();
    
    			$default_settings = [
    				'username'                   => 'flickr',
    				'number'                     => 20,
    				'hover'                      => 'default',
    				'target'                     => '_self',
    				'link'                       => '',
    				'ids'                        => '',
    				'style'                      => 'carousel',
    				'brand_style'                => 'default',
    				'slides_per_view'            => array( 'size' => 3 ),
    				'slides_per_view_tablet'     => array( 'size' => '' ),
    				'slides_per_view_mobile'     => array( 'size' => '' ),
    				'columns'                    => array( 'size' => 3 ),
    				'columns_tablet'             => array( 'size' => '' ),
    				'columns_mobile'             => array( 'size' => '' ),
    				'orderby'                    => '',
    				'hide_empty'                 => 0,
    				'order'                      => 'ASC',
    				'scroll_carousel_init'       => 'no',
    				'filter_in_current_category' => 'no',
    				'custom_sizes'               => apply_filters( 'woodmart_brands_shortcode_custom_sizes', false ),
    			];
    
    			$settings = wp_parse_args( $widget->get_settings_for_display(), array_merge( woodmart_get_owl_atts(), $default_settings ) );
    
    			$carousel_id = 'brands_' . rand( 1000, 9999 );
    
    			$attribute = woodmart_get_opt( 'brands_attribute' );
    
    			if ( empty( $attribute ) || ! taxonomy_exists( $attribute ) ) {
    				echo '<div class="wd-notice wd-info">' . esc_html__( 'You must select your brand attribute in Theme Settings -> Shop -> Brands', 'woodmart' ) . '</div>';
    				return ob_get_clean();
    			}
    
    			$settings['columns']         = isset( $settings['columns']['size'] ) ? $settings['columns']['size'] : 3;
    			$settings['slides_per_view'] = isset( $settings['slides_per_view']['size'] ) ? $settings['slides_per_view']['size'] : 3;
    
    			$owl_attributes = '';
    
    			$widget->add_render_attribute(
    				[
    					'wrapper' => [
    						'class' => [
    							'brands-items-wrapper',
    							'brands-widget',
    							'slider-' . $carousel_id,
    							'brands-hover-' . $settings['hover'],
    							'brands-style-' . $settings['brand_style'],
    						],
    						'id'    => [
    							$carousel_id,
    						],
    					],
    					'items'   => [
    						'class' => [
    							'brand-item',
    						],
    					],
    				]
    			);
    
    			if ( $settings['style'] ) {
    				$widget->add_render_attribute( 'wrapper', 'class', 'brands-' . $settings['style'] );
    			}
    
    			if ( 'carousel' === $settings['style'] ) {
    				woodmart_enqueue_inline_style( 'owl-carousel' );
    				$settings['scroll_per_page'] = 'yes';
    				$settings['carousel_id']     = $carousel_id;
    
    				if ( ! empty( $settings['slides_per_view_tablet']['size'] ) || ! empty( $settings['slides_per_view_mobile']['size'] ) ) {
    					$settings['custom_sizes'] = array(
    						'desktop'          => $settings['slides_per_view'],
    						'tablet_landscape' => $settings['slides_per_view_tablet']['size'],
    						'tablet'           => $settings['slides_per_view_mobile']['size'],
    						'mobile'           => $settings['slides_per_view_mobile']['size'],
    					);
    				}
    
    				$owl_attributes = woodmart_get_owl_attributes( $settings );
    
    				$widget->add_render_attribute( 'items_wrapper', 'class', 'owl-carousel ' . woodmart_owl_items_per_slide( $settings['slides_per_view'], array(), false, false, $settings['custom_sizes'] ) );
    				$widget->add_render_attribute( 'wrapper', 'class', 'wd-carousel-container' );
    				$widget->add_render_attribute( 'wrapper', 'class', 'wd-carousel-spacing-0' );
    
    				if ( 'yes' === $settings['scroll_carousel_init'] ) {
    					woodmart_enqueue_js_library( 'waypoints' );
    					$widget->add_render_attribute( 'wrapper', 'class', 'scroll-init' );
    				}
    
    				if ( woodmart_get_opt( 'disable_owl_mobile_devices' ) ) {
    					$widget->add_render_attribute( 'wrapper', 'class', 'disable-owl-mobile' );
    				}
    			} else {
    				$widget->add_render_attribute( 'items_wrapper', 'class', 'row' );
    				$widget->add_render_attribute( 'items_wrapper', 'class', 'wd-spacing-0' );
    				$widget->add_render_attribute( 'items', 'class', woodmart_get_grid_el_class_new( 0, false, $settings['columns'], $settings['columns_tablet']['size'], $settings['columns_mobile']['size'] ) );
    			}
    
    			$args = array(
    				'taxonomy'   => $attribute,
    				'hide_empty' => $settings['hide_empty'],
    				'order'      => $settings['order'],
    				'number'     => $settings['number'],
    			);
    
    			if ( $settings['orderby'] ) {
    				$args['orderby'] = $settings['orderby'];
    			}
    
    			if ( 'random' === $settings['orderby'] ) {
    				$args['orderby'] = 'id';
    				$brand_count     = wp_count_terms(
    					$attribute,
    					array(
    						'hide_empty' => $settings['hide_empty'],
    					)
    				);
    
    				$offset = rand( 0, $brand_count - (int) $settings['number'] );
    				if ( $offset <= 0 ) {
    					$offset = '';
    				}
    				$args['offset'] = $offset;
    			}
    
    			if ( $settings['ids'] ) {
    				$args['include'] = $settings['ids'];
    			}
    
    			$brands   = get_terms( $args );
    			$taxonomy = get_taxonomy( $attribute );
    
    			if ( 'random' === $settings['orderby'] ) {
    				shuffle( $brands );
    			}
    
    			if ( woodmart_is_shop_on_front() ) {
    				$link = home_url();
    			} elseif ( 'yes' === $settings['filter_in_current_category'] && is_product_category() ) {
    				$link = woodmart_get_current_url();
    			} else {
    				$link = get_post_type_archive_link( 'product' );
    			}
    
    			woodmart_enqueue_inline_style( 'brands' );
    
    			?>
    			<div <?php echo $widget->get_render_attribute_string( 'wrapper' ); ?> <?php echo $owl_attributes; ?>>
    				<div <?php echo $widget->get_render_attribute_string( 'items_wrapper' ); ?>>
    					<?php if ( ! is_wp_error( $brands ) && count( $brands ) > 0 ) : ?>
    						<?php foreach ( $brands as $key => $brand ) : ?>
    							<?php
    							$image       = get_term_meta( $brand->term_id, 'image', true );
    							$filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $attribute ) );
    
    							if ( is_object( $taxonomy ) && $taxonomy->public ) {
    								$attr_link = get_term_link( $brand->term_id, $brand->taxonomy );
    							} else {
    								$attr_link = add_query_arg( $filter_name, $brand->slug, $link );
    							}
    
    							?>
    
    							<div <?php echo $widget->get_render_attribute_string( 'items' ); ?>>
    								<a title="<?php echo esc_html( $brand->name ); ?>" href="<?php echo esc_url( $attr_link ); ?>">
    									<?php if ( 'list' === $settings['style'] || ! $image || ( is_array( $image ) && empty( $image['id'] ) ) ) : ?>
    										<span class="brand-title-wrap">
    										<?php echo esc_html( $brand->name ); ?>
    									</span>
    									<?php elseif ( is_array( $image ) ) : ?>
    										<?php echo wp_get_attachment_image( $image['id'], 'full' ); ?>
    									<?php else : ?>
    										<?php echo '<img src="' . $image . '" alt="' . $brand->name . '" title="' . $brand->name . '">'; ?>
    									<?php endif; ?>
    								</a>
    							</div>
    						<?php endforeach; ?>
    					<?php endif; ?>
    				</div>
    			</div>
    			<?php
    			return ob_get_clean();
    		}
    
    		return $widget_content;
    	}
    
    	add_filter( 'elementor/widget/render_content', 'wd_products_brands_render', 10, 2 );
    }

    Best Regards

    #403060

    thomasgaillard
    Participant

    Many Thanks !!!

    That is PERFECT 🙂

    You can close this case

    #403112

    You are welcome! We are here to help.

    Wish you a wonderful day!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Create a new widget in child theme’ is closed to new replies.