M
M
Michael R.2017-02-11 14:18:13
PHP
Michael R., 2017-02-11 14:18:13

How to display product category images?

Hello! Displayed a widget of WC product categories, the settings have everything that is required, but there is no output of thumbnails of these product categories themselves ...
QqN9C
Googling, picking in the file in various ways

wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-product-categories.php
but got no result.
Please tell me how to solve this problem. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2017-02-12
@Mike_Ro

Add the corrected class to a separate file

if ( !class_exists( 'WPP_WC_Product_Cat_List_Walker_With_Thumb' ) ) :
        class WPP_WC_Product_Cat_List_Walker_With_Thumb extends Walker {

            public $tree_type = 'product_cat';

            public $db_fields = array(
                'parent' => 'parent',
                'id'     => 'term_id',
                'slug'   => 'slug',
            );

            public function start_lvl(&$output, $depth = 0, $args = array()) {
                if ( 'list' != $args[ 'style' ] )
                    return;
                $indent = str_repeat( "\t", $depth );
                $output .= "$indent<ul class='children'>\n";
            }

            public function end_lvl(&$output, $depth = 0, $args = array()) {
                if ( 'list' != $args[ 'style' ] )
                    return;
                $indent = str_repeat( "\t", $depth );
                $output .= "$indent</ul>\n";
            }

            public function start_el(&$output, $cat, $depth = 0, $args = array(), $current_object_id = 0) {
                $output .= '<li class="cat-item cat-item-' . $cat->term_id;
                if ( $args[ 'current_category' ] == $cat->term_id ) {
                    $output .= ' current-cat';
                }
                if ( $args[ 'has_children' ] && $args[ 'hierarchical' ] ) {
                    $output .= ' cat-parent';
                }
                if ( $args[ 'current_category_ancestors' ] && $args[ 'current_category' ] && in_array( $cat->term_id, $args[ 'current_category_ancestors' ] ) ) {
                    $output .= ' current-cat-parent';
                }

                $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
                $img_url= wp_get_attachment_url( $thumbnail_id );
                $image = ! empty($img_url) ?  '<img class="wpp-term-thumb" src="' . $img_url . '" alt="" />' : '';

                $output .= '"><a href="' . get_term_link( (int) $cat->term_id, $this->tree_type ) . '">'  . $image . _x( $cat->name, 'product category name', 'woocommerce' ) . '</a>';
                if ( $args[ 'show_count' ] ) {
                    $output .= ' <span class="count">(' . $cat->count . ')</span>';
                }
            }

            public function end_el(&$output, $cat, $depth = 0, $args = array()) {
                $output .= "</li>\n";
            }

            public function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
                if ( !$element || ( 0 === $element->count && !empty( $args[ 0 ][ 'hide_empty' ] ) ) ) {
                    return;
                }
                parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
            }
        }
    endif;

In functions.php or somewhere else, do this
function wpp_change_widget_product_categories_walker($array) {

        require_once 'Путь_к_файлу_с_добаленным_классом';

        $array[ 'walker' ] = new WPP_WC_Product_Cat_List_Walker_With_Thumb;

        return $array;

    }

    add_filter('woocommerce_product_categories_widget_args','wpp_change_widget_product_categories_walker');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question