【问题标题】:WooCommerce: show all variations from different products ordered by attributeWooCommerce:显示按属性排序的不同产品的所有变体
【发布时间】:2017-08-13 20:40:34
【问题描述】:

我想显示多个产品的所有变体并按属性(数字)对其进行排序。

我找不到解决方案。我可以显示所有产品并显示每个产品的变化。但是我无法按属性对显示的产品进行排序。

有没有办法显示所有的变体?

【问题讨论】:

    标签: wordpress woocommerce wordpress-theming woocommerce-theming


    【解决方案1】:

    请尝试下面的代码,它可能对您只使用 PHP 技巧有帮助。

    $variations=array();
    foreach($products as $product){
    $product_s = wc_get_product( $product->ID );
        if ($product_s->product_type == 'variable') {
            $args = array(
                'post_parent' => $plan->ID,
                'post_type'   => 'product_variation',
                'numberposts' => -1,
            );
            $variants = $product_s->get_available_variations();
            foreach($variants as $variant){
                array_push($variations,$variant);
            }
    
        }
    }
    $variations=array_unique($variations);
    echo '<pre>';
    print_r($variations);
    echo '</pre>';
    

    Woo-commerce 方法来源:get_available_variations()

    谢谢!

    【讨论】:

    • 编辑:啊,我明白了。它只显示单个产品的变体,对吗?我需要多个产品的所有变体列表
    • 对不起!我想你错过了一些东西!首先获取所有产品,然后在 $products 引用所有产品的地方运行此代码。
    • 使用 $args = array( 'post_type' => 'product','post_status' => 'publish', 'posts_per_page' => -1); $products = get_posts($args);
    • 仅尝试参数 $args = array( 'post_type' => 'product');
    • 如果我这样做,数组又是空的
    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2018-10-11
    • 2019-01-23
    相关资源
    最近更新 更多