WooCommerce:显示按属性排序的不同产品的所有变体

时间:2017-08-13 20:40:35

标签: wordpress woocommerce wordpress-theming woocommerce-theming

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

我无法找到解决方案。我可以显示所有产品并显示每个产品的变化。但我无法按属性订购显示的产品。

有没有办法显示所有变种?

1 个答案:

答案 0 :(得分: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()

谢谢!