久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取產品變體屬性值術語 ID 和名稱

Get the product variations attributes values term ID and name(獲取產品變體屬性值術語 ID 和名稱)
本文介紹了獲取產品變體屬性值術語 ID 和名稱的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 WooCommerce 中,我使用 $product->get_variation_attributes() 來獲取產品的變體屬性.此函數返回一個名稱不帶 ID 的數組.

In WooCommerce, I'm using $product->get_variation_attributes() to get the variations attributes for a product. This function returns an array with the names without the ID's.

像這樣:

                   [pa_color-shirt] => Array
                        (
                            [0] => red
                            [7] => grey
                            [14] => yellow
                        )

                    [pa_color-sweater] => Array
                        (
                            [0] => red
                            [1] => green
                            [2] => blue
                            [3] => grey
                            [4] => yellow
                            [5] => pink
                            [6] => dark-blue
                        )

對于我正在創建的 AJAX 商店,我還需要來自變體的 ID.所以我可以將 Id 和名稱附加到選擇框(就像 woocommerce 一樣).
我搜索了幾天,但找不到解決方案.

For the AJAX shop I'm creating I also need the ID's from the variations. So I can append the Id's and the names to the select boxes (Like woocommerce does).
I searched around for days but couldn't find a solution.

我創建了這個代碼:

if($product->has_child()) {
                $attributes = $product->get_attributes();
                $variations = $product->get_available_variations();
                $variationsArray = array();
                foreach ($attributes as $attr => $attr_deets) {
                    $variationArray = array();
                    $attribute_label = wc_attribute_label($attr);
                    $variationArray["attribute_label"] = $attribute_label;
                    if (isset($attributes[$attr]) || isset($attributes['pa_' . $attr])) {
                        $attribute = isset($attributes[$attr]) ? $attributes[$attr] : $attributes['pa_' . $attr];
                        if ($attribute['is_taxonomy'] && $attribute['is_visible']) {
                            $variationArray["attribute_name"] = $attribute['name'];
                            $variationIds = array();
                            $variationNames = array();
                            $variationPrices = array();
                            foreach ($variations as $variation) {
                                if (!empty($variation['attributes']['attribute_' . $attribute['name']])) {
                                    array_push($variationIds, $variation['variation_id']);
                                    $taxonomy = $attribute['name'];
                                    $meta = get_post_meta($variation['variation_id'], 'attribute_'.$taxonomy, true);
                                    $term = get_term_by('slug', $meta, $taxonomy);
                                    $variation_name = $term->name;
                                    array_push($variationNames, $variation_name);
                                    array_push($variationPrices, $variation['display_regular_price']);
                                }
                            }
                            $variationArray["variation_prices"] = $variationPrices;
                            $variationArray["variations"] = array_combine($variationIds, $variationNames);
                        }
                    }
                    array_push($variationsArray, $variationArray);
                }
            }

            $product_variations = $variationsArray;

此代碼返回https://hastebin.com/ecebewumoz.php

代碼有效,但返回重復的名稱和 ID.

The code works but returns duplicate names and id's.

我的問題是,有誰知道我如何使用 ID 來完成與 get_variation_attributes() 相同的事情?

My questions is does anyone know how I could accomplish the same as get_variation_attributes() but with the ID's?

謝謝.

推薦答案

WooCommerce 版本 3+ 的更新

Update for WooCommerce versions 3+

foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){
    // To get the attribute label (in WooCommerce 3+)
    $taxonomy_label = wc_attribute_label( $taxonomy, $product );

    // Setting some data in an array
    $variations_attributes_and_values[$taxonomy] = array('label' => $taxonomy_label);

    foreach($terms_slug as $term){

        // Getting the term object from the slug
        $term_obj  = get_term_by('slug', $term, $taxonomy);

        $term_id   = $term_obj->term_id; // The ID  <==  <==  <==  <==  <==  <==  HERE
        $term_name = $term_obj->name; // The Name
        $term_slug = $term_obj->slug; // The Slug
        // $term_description = $term_obj->description; // The Description

        // Setting the terms ID and values in the array
        $variations_attributes_and_values[$taxonomy]['terms'][$term_id] = array(
            'name'        => $term_name,
            'slug'        => $term_slug
        );
    }
}

<小時><塊引用>

低于 WooCommerce 版本 3


我在您的原始數據數組中沒有看到任何重復的變體 ID……您的問題不是很清楚,因此很難猜測您正在查看的缺失 ID 是什么.然后我冒險回答,我想缺少的 ID 是來自屬性值的術語 Ids...

Below WooCommerce version 3

為了獲得這些條款 ID,我使用 Wordpress 函數 get_term_by(),這樣:

I don't see any duplicate variations IDs in your raw data array… Your question is not very clear, so is difficult to guess what are the missing Ids you are looking at. Then I take the risk to answer and I suppose that the missing IDs are the term Ids from the attributes values…

To get this terms IDs, I use Wordpress function get_term_by(), this way:

還有:

And with:
echo '<pre>'; print_r($variations_attributes_and_values);echo '</pre>';

我將得到這個輸出,每個屬性的真實術語 ID 用于產品變體(我已經安排了數組輸出以使其更緊湊):

I will get this output, with the real terms IDs for each attribute for a product variations (I have arranged the array output to get it more compact):

Array(
    [pa_color] => Array(
        [label] => Color
        [terms] => Array(
            [8] => Array(
                [name] => Black
                [slug] => black'
            )
            [9] => Array(
                [name] => Blue
                [slug] => blue
            )
            [11] => Array(
                [name] => Green
                [slug] => green
            )
        ) 
    )
    [pa_bulk_quantity] => Array(
        [label] => Bulk quantity
        [terms] => Array(
            [44] => Array(
                [name] => Pack of 10 dozen
                [slug] => pack-of-10-dozen
            )
            [45] => Array(
                [name] => Case of 50 dozens
                [slug] => case-of-50-dozens
            )
        )
    )
)

這篇關于獲取產品變體屬性值術語 ID 和名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 射久久| 亚洲一区中文字幕在线观看 | 国产视频1区 | 国产女人叫床高潮大片免费 | 国产999精品久久久久久 | 久久免费资源 | 久久精品成人 | 国产在线二区 | 91中文字幕在线 | 欧美激情在线观看一区二区三区 | 精品欧美乱码久久久久久1区2区 | 涩涩视频网站在线观看 | 国产中文字幕av | 99久久婷婷国产亚洲终合精品 | 国产精品久久久久久久一区二区 | 欧美精品一区二区三区在线播放 | 国产精品完整版 | 日本一级淫片免费啪啪3 | 国产精品成人国产乱一区 | 午夜视频免费在线观看 | 久久1区| 最新日韩在线 | 日韩三级电影在线看 | 日韩国产一区二区三区 | 日韩最新网址 | 久久久片| 在线黄av| 情侣酒店偷拍一区二区在线播放 | 精品一区二区三区免费视频 | 在线播放中文 | 亚洲欧美一区二区三区视频 | 欧美在线观看一区 | 欧美一级欧美三级在线观看 | 亚洲欧美日韩国产综合 | 一区二区三区视频在线观看 | 激情六月天 | 精品伊人 | 亚洲精品精品 | 久久免费精品视频 | 久久国产精品一区二区三区 | 日韩激情免费 |