Показать (связанный) родитель и подкатегорию из виджета категории продуктов WooCommerce на (каждой) странице категории - PullRequest
0 голосов
/ 22 марта 2020

Я не могу применить приведенный ниже код, когда оба условных тега (is_product_category) вместе на странице категории продукта (и дочернего элемента) и на странице магазина.

Надеюсь, что кто-нибудь может помочь.

Моя цель: каждый продукт страница категории (включая страницу родительской и дочерней категорий), виджет отображает все категории товаров родительского и дочернего (семантичные c связанные) категории (несвязанные категории продуктов, которые необходимо скрыть в виджете).

см. код фрагменты , а также фрагменты кода

//* Used when the widget is displayed as a dropdown
add_filter('woocommerce_product_categories_widget_dropdown_args', 'appliances', 10, 10);
//* Used when the widget is displayed as a list
add_filter('woocommerce_product_categories_widget_args', 'appliances', 10, 10);
function appliances($cat_args) {
  if (is_product_category(75) || is_product_category($termchildren)) {
    // Create an array that will hold the ids that need to be included
    $include_terms = array();
    // Push the default term that you need to shown 
    array_push($include_terms, 75);
    // Create an array that will hold the ids that need to be included
    $termchildren = get_term_children(75, 'product_cat');
    }
  if (is_product_category(59) || is_product_category($termchildren)) {
    // Create an array that will hold the ids that need to be included
    $include_terms = array();
    // Push the default term that you need to shown 
    array_push($include_terms, 59);
    // Create an array that will hold the ids that need to be included
    $termchildren = get_term_children(59, 'product_cat');
    }
    foreach($termchildren as $child) {
      $term = get_term_by('id', $child, 'product_cat');
      array_push($include_terms, $term - > term_id);
    }
    // Finally pass the array
    $cat_args['include'] = $include_terms;
  }
  return $cat_args;
}

1 Ответ

0 голосов
/ 28 марта 2020

После многих дней проб и ошибок, при этом фрагмент кода, который работал для меня:

  1. Использовал несколько условных тегов;
  2. Показывать только связанные категории (& дочерние) в каждой категории продуктов ;
  3. Но не знаю, как надстройка другого фильтра woocommerce_product_categories_widget_dropdown_args для (если) отмеченного выпадающего виджета категории продуктов;
  4. Недостаток: в виджете категории продуктов "Показывать только детей текущей категории "не может функционировать. (Из-за get_term_children, как показано ниже, фрагменты кода).

Надеюсь, кто-нибудь поможет здесь, и sh кто-то может улучшить этот ответ.

//* Used when the widget is displayed as an slug (aabc) / term_id (50) lists
add_filter( 'woocommerce_product_categories_widget_args', function ( $cat_args ) {
if ( is_product_category('aabc') || is_product_category('ddef') || is_product_category('gghi') || is_product_category('jjkl') || is_product_category('mmno') || is_product_category('ppqr') || is_product_category('sstu') || is_product_category('vvwx') || is_product_category('yyz')){
        // Create an array that will hold the ids that need to be included
        $include_terms = array();
        // Push the default term that you need to show 
        array_push( $include_terms, 50 );
        // Create an array that will hold the ids that need to be included
        $termchildren = get_term_children( 50, 'product_cat' );
        // Iterate over the terms found and add it to the array which holds the IDs to include
        foreach( $termchildren as $child ) {
            $term = get_term_by( 'id', $child, 'product_cat' );     
            array_push( $include_terms, $term->term_id );
            }
        // Finally pass the array
        $cat_args['include'] = $include_terms;
    }   
        return $cat_args;
    });
//* Used when the widget is displayed as an slug (abc) / term_id (60) lists
add_filter( 'woocommerce_product_categories_widget_args', function ( $cat_args ) {
if ( is_product_category('abc') || is_product_category('def') || is_product_category('ghi') || is_product_category('jkl') || is_product_category('mno') || is_product_category('pqr') || is_product_category('stu') || is_product_category('vwx') || is_product_category('yz')){
        // Create an array that will hold the ids that need to be included
        $include_terms = array();
        // Push the default term that you need to show 
        array_push( $include_terms, 60 );
        // Create an array that will hold the ids that need to be included
        $termchildren = get_term_children( 60, 'product_cat' );
        // Iterate over the terms found and add it to the array which holds the IDs to include
        foreach( $termchildren as $child ) {
            $term = get_term_by( 'id', $child, 'product_cat' );     
            array_push( $include_terms, $term->term_id );
            }
        // Finally pass the array
        $cat_args['include'] = $include_terms;
    }   
        return $cat_args;
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...