【问题标题】:Show (related) Parent & Subcategory from WooCommerce Product Category Widget within (each) category page在(每个)类别页面中显示来自 WooCommerce 产品类别小部件的(相关)父类别和子类别
【发布时间】:2020-04-01 00:33:11
【问题描述】:

当条件标签 (is_product_category) 同时在产品(和子)类别页面和商店页面中时,我无法应用以下代码。

希望任何人都可以提供帮助。

我的目标:每个产品类别页面(包括父子类别页面),小部件显示所有父子(语义相关)产品类别(不相关的产品类别需要隐藏在小部件中)。

请参阅code snippets 和code snippets

//* 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;
}

【问题讨论】:

    标签: woocommerce code-snippets


    【解决方案1】:

    经过多天的反复试验,特此提供对我有用的代码 sn-p:

    1. 使用了多个条件标签;
    2. 仅显示每个产品类别中的相关类别(&child);
    3. 但不知道如何添加另一个过滤器 woocommerce_product_categories_widget_dropdown_args 以获取(如果)选中的下拉产品类别小部件;
    4. 缺点:在产品类别小部件中“仅显示当前类别的子项”无法正常工作。 (由于 get_term_children 如下代码 sn-ps)。

    希望在这里帮助任何人,并希望有人可以改进这个答案。

    //* 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;
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2021-09-03
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多