如何获得WooCommerce产品变化值

时间:2018-10-31 09:44:04

标签: php arrays wordpress foreach woocommerce

我希望能够列出变化选项的值。例如,我有一个12in,14in和16in的灯笼。我希望能够获得这些值。我一直在尝试使用foreach循环来获取这些值,但是我需要一些帮助。这是我的代码;

function test_func(){
    global $woocommerce, $product, $post;
    // test if product is variable
    if( $product->is_type( 'variable' ) ){
        $available_variations = $product->get_available_variations();
        // var_dump($available_variations);

        foreach( $available_variations as $key => $value ){ 
            var_dump( $value['attributes'] ) ;
        }
    }
}

这是输出:

array(1) { ["attribute_pa_size"]=> string(4) "12in" } array(1) { ["attribute_pa_size"]=> string(4) "14in" } array(1) { ["attribute_pa_size"]=> string(4) "16in" }

您可以看到我想要的值在那里,但是我不知道如何让它们回显它们。

这就是我var_dump()$ available_variations;

“ [[” backorders_allowed“] => bool(false)[” dimensions“] => array(3){[”“ length”] => string(4)“ 11.8” [“ width”] => string( 4)“ 11.8” [“ height”] =>字符串(4)“ 11.8”} [“ dimensions_html”] =>字符串(21)“ 11.8 x 11.8 x 11.8 in” [“ display_price”] => float(3.2) [“ display_regular_price”] => float(3.2)[“ image”] => array(18){[“ title”] => string(11)“ 6712R-1.jpg” [“ caption”] => string( 0)“” [“ url”] =>字符串(59)“ http://website/wp-content/uploads/2018/10/6712R-1.jpg” [“ alt”] =>字符串(0)“” [“ src”] =>字符串(67)“ {{3 }}“ [[srcset”] =>字符串(445)“ http://website/wp-content/uploads/2018/10/6712R-1-600x600.jpg 600w,http://website/wp-content/uploads/2018/10/6712R-1-600x600.jpg 150w,http://website/wp-content/uploads/2018/10/6712R-1-150x150.jpg 300w,http://website/wp-content/uploads/2018/10/6712R-1-300x300.jpg 768w,http://website/wp-content/uploads/2018/10/6712R-1-768x768.jpg 1024w, http://website/wp-content/uploads/2018/10/6712R-1-1024x1024.jpg 100w“ [” sizes“] =>字符串(31)”(最大宽度:600像素)100vw,600像素“ [” full_src“] =>字符串(59)” http://website/wp-content/uploads/2018/10/6712R-1-100x100.jpg“ [[ full_src_w“] => int(2000)[” full_src_h“] => int(2000)[” gallery_thumbnail_src“] =>字符串(67)” http://website/wp-content/uploads/2018/10/6712R-1.jpg“ [” gallery_thumbnail_src_w“] => int(100)[ “ gallery_thumbnail_src_h”] => int(100)[“ thumb_src”] =>字符串(67)“ http://website/wp-content/uploads/2018/10/6712R-1-100x100.jpg” [[thumb_src_w“] => int(300)[” thumb_src_h“] => int(300) [“ src_w”] => int(600)[“ src_h”] => int(600)} [“ image_id”] =>字符串(3)“ 164” [“ is_downl oadable“] => bool(false)[” is_in_stock“] => bool(true)[” is_purchasable“] => bool(true)[” is_sold_individually“] => string(2)” no“ [” is_virtual“] => bool(false)[“ max_qty”] => int(17)[“ min_qty”] => int(1)[“ price_html”] =>字符串(145)“

“ [” sku“] =>字符串(5)” 6712R“ [” variation_description“] =>字符串(0)”“ [” variation_id“] => int(1462)[” variation_is_active“] =>布尔(true)[“ variation_is_visible”] => bool(true)[“ weight”] =>字符串(0)“” [“ weight_html”] =>字符串(3)“ N / A”} [1] =>数组(24){[“ attributes”] => array(1){[“ attribute_pa_size”] => string(4)“ 14in”} [“ availability_html”] => string(51)“

这仅适用于一种产品,每个变体都有一个,但这使您了解其工作原理。我也愿意尝试另一种方法来获得相同的结果,因此,如果您知道一个,请告诉我。谢谢

1 个答案:

答案 0 :(得分:1)

您需要对产品属性使用第二个foreach循环:

function test_func(){
    global $woocommerce, $product, $post;
    // test if product is variable
    if( $product->is_type( 'variable' ) ){
        // Loop through available product variation data
        foreach ( $product->get_available_variations() as $key => $variation ) {
            // Loop through the product attributes for this variation
            foreach ($variation['attributes'] as $attribute => $term_slug ) {
                // Get the taxonomy slug
                $taxonmomy = str_replace( 'attribute_', '', $attribute );

                // Get the attribute label name
                $attr_label_name = wc_attribute_label( $taxonmomy );

                // Display attribute labe name
                $term_name = get_term_by( 'slug', $term_slug, $taxonmomy )->name;

                // Testing output
                echo '<p>' . $attr_label_name . ': ' . $term_name . '</p>';
            }
        }
    }
}