簡體   English   中英

如何獲取 WooCommerce 產品變體值

[英]How to get WooCommerce product variation values

我希望能夠列出變化選項值。 例如,我有一個 12 英寸、14 英寸和 16 英寸的燈籠。 我希望能夠獲得這些值。 我一直在嘗試使用 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"]=> string(4) "11.8" } ["dimensions_html"]=> string(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"]=> string(59) " http://website/wp-content/uploads/2018/10/6712R-1.jpg " ["alt"]=> string(0) "" [" src"]=> string(67) " http://website/wp-content/uploads/2018/10/6712R-1-600x600.jpg " ["srcset"]=> string(445) " http://網站/wp-content/uploads/2018/10/6712R-1-600x600.jpg 600w, http://website/wp-content/uploads/2018/10/6712R-1-150x150.jpg 150w, http://網站/wp-content/uploads/2018/10/6712R-1-300x300.jpg 300w, http://website/wp-content/uploads/2018/10/6712R-1-768x768.jpg 768w,http://網站/wp-content/uploads/2018/10/6712R-1-1024x1024.jpg 1024whttp://website/wp-content/uploads/2018/10/6712R-1-100x100.jpg 100w" ["sizes"]=> string(31) "(max-width: 600px) 100vw, 600px" ["full_src"]=> string(59) " http://website/wp-content/uploads/2018/10 /6712R-1.jpg " ["full_src_w"]=> int(2000) ["full_src_h"]=> int(2000) ["gallery_thumbnail_src"]=> string(67) " http://website/wp-content /uploads/2018/10/6712R-1-100x100.jpg " ["gallery_thumbnail_src_w"]=> int(100) ["gallery_thumbnail_src_h"]=> int(100) ["thumb_src"]=> http string(67) " ://website/wp-content/uploads/2018/10/6712R-1-300x300.jpg " ["thumb_src_w"]=> int(300) ["thumb_src_h"]=> int(300) ["src_w"] => int(600) ["src_h"]=> int(600) } ["image_id"]=> string(3) "164" ["is_downloadable"]=> 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"]=> string(145) "

" ["sku"]=> string(5) "6712R" ["variation_description"]=> string(0) "" ["variation_id"]=> int(1462) ["variation_is_active"]=> bool(true) ["variation_is_visible"]=> bool(true) ["weight"]=> string(0) "" ["weight_html"]=> string(3) "N/A" } [1]=> array(24) { ["attributes"]=> array(1) { ["attribute_pa_size"]=> string(4) "14in" } ["availability_html"]=> string(51) "

這僅適用於一種產品,每種變體都有一個,但這可以讓您了解它的工作原理。 我也願意嘗試另一種方法來獲得相同的結果,所以如果你知道一個方法,請告訴我。 謝謝

您需要為產品屬性使用第二個 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>';
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM