Mike_Ro
@Mike_Ro
Python, JS, WordPress, SEO, Bots, Adversting

Как вывести изображения категорий товаров?

Всем привет! Вывел виджет категорий товаров WC, в настройках есть все, что требуется, но нет вывода миниатюр этих сами категорий товаров...
QqN9C

Погуглив, ковырял различными способами в файле
wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-product-categories.php
, но результата не получил.
Подскажите пожалуйста, как решить эту задачу. Спасибо!
  • Вопрос задан
  • 357 просмотров
Решения вопроса 1
wppanda5
@wppanda5
WordPress Mедведь
В отдельный файл сложите поправленный класс
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;


В functions.php или где то еще, делаем так

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');
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
YCLIENTS Москва
от 200 000 до 350 000 ₽
Ведисофт Екатеринбург
от 25 000 ₽
ИТЦ Аусферр Магнитогорск
от 100 000 до 160 000 ₽
24 апр. 2024, в 16:19
4000 руб./за проект
24 апр. 2024, в 15:34
20000 руб./за проект
24 апр. 2024, в 15:32
130000 руб./за проект