【问题标题】:Show Only Grouped Products in Woocommerce在 Woocommerce 中仅显示分组产品
【发布时间】:2019-03-06 17:46:09
【问题描述】:

在 Woocommerce 中,由于服装尺寸的库存 sku 问题,我们为最终相同的产品提供两种不同的产品。我们称它为蓝色衬衫,但由于订购决定,它是我们商店中的 2 种不同产品。我创建了一个分组产品来处理它的显示。
但是,我似乎无法从我的商店中隐藏链接的产品。当我使用 WC_Product_query 类和 get_products 函数时,它似乎没有返回所有正在查询的 46 个产品。我想知道究竟是什么,我做错了和/或是否有更好的解决方案。

在我的 functions.php 子主题中放置以下内容:

add_action( 'woocommerce_product_query', 'only_grouped_products_query' );
function only_grouped_products_query( $q ) {
    $query = new WC_Product_Query(array($q));   

    $products = $query->get_products();

    $linked_array = array();
    $i = 0;
    foreach($products as $product){     
        $i++;
        /*
        echo $product->get_name() .' - ' . $product->product_type .'<br>';
        $is_grouped = $product->product_type;
        if ( $is_grouped == 'grouped' )
        {
            // echo $product->get_name() .' - ' . $product->product_type;
            $get_child_id = $product->get_children();
            if (is_array($get_child_id)){
                foreach($get_child_id as $child_id){
                    $linked_array[] = $child_id;
                }
            } else {
                $linked_array[] = $get_child_id;
            }
        }
        */
    }
    echo $i;
    // $q->set( 'post__not_in', $linked_array );
}

上面的代码 sn-p 确实有效,但是当我转储原始 $query 变量时,它并没有提取查询显示的所有 46 个结果。 $i 只返回 10 个结果。

【问题讨论】:

    标签: php wordpress woocommerce product custom-taxonomy


    【解决方案1】:

    你的问题主体解释不是很清楚......所以我根据你的问题标题来回答这个问题。

    由于 Woocommerce 中的产品类型由 product_type 自定义分类法处理,您需要改用 税务查询

    以下代码将在所有 Woocommerce 存档页面上仅显示分组的产品类型:

    add_filter( 'woocommerce_product_query_tax_query', 'only_grouped_products', 20, 1 );
    function only_grouped_products( $tax_query ){
        $tax_query[] = array(
            'taxonomy'  => 'product_type',
            'field'     => 'name',
            'terms'     => array('grouped'),
        );
        return $tax_query;
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。


    Woocommerce 默认产品类型(和其他自定义产品类型)

    因此您可以处理任何产品类型:

    • 对于您将使用的简单产品:'terms' =&gt; array('simple'),
    • 对于您将使用的变量产品:'terms' =&gt; array('variable'),
    • 对于您将使用的分组产品:'terms' =&gt; array('grouped'),
    • 对于您将使用的外部产品:'terms' =&gt; array('external'),

    还有所有这些自定义产品类型(来自已知的第 3 方插件):

    • 对于您将使用的复合产品:'terms' =&gt; array('composite'),
    • 对于您将使用的简单订阅产品:'terms' =&gt; array('subscriptions'),
    • 对于您将使用的变量订阅产品:'terms' =&gt; array('Variable Subscription'),
    • 对于您将使用的 Booking 产品:'terms' =&gt; array('booking'),

    您也可以组合多种产品类型,例如仅简单和分组的产品类型:
    您将使用:'terms' =&gt; array('simple', 'grouped'), ... 等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-04
      • 2014-06-18
      • 2018-10-12
      • 1970-01-01
      • 2015-12-30
      • 2018-05-17
      • 2014-01-26
      • 1970-01-01
      相关资源
      最近更新 更多