pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

獲取產(chǎn)品變體屬性值術(shù)語(yǔ) ID 和名稱

Get the product variations attributes values term ID and name(獲取產(chǎn)品變體屬性值術(shù)語(yǔ) ID 和名稱)
本文介紹了獲取產(chǎn)品變體屬性值術(shù)語(yǔ) ID 和名稱的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

在 WooCommerce 中,我使用 $product->get_variation_attributes() 來(lái)獲取產(chǎn)品的變體屬性.此函數(shù)返回一個(gè)名稱不帶 ID 的數(shù)組.

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
                        )

對(duì)于我正在創(chuàng)建的 AJAX 商店,我還需要來(lái)自變體的 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.

我創(chuàng)建了這個(gè)代碼:

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

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

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

我的問(wèn)題是,有誰(shuí)知道我如何使用 ID 來(lái)完成與 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
        );
    }
}

<小時(shí)><塊引用>

低于 WooCommerce 版本 3


我在您的原始數(shù)據(jù)數(shù)組中沒(méi)有看到任何重復(fù)的變體 ID……您的問(wèn)題不是很清楚,因此很難猜測(cè)您正在查看的缺失 ID 是什么.然后我冒險(xiǎn)回答,我想缺少的 ID 是來(lái)自屬性值的術(shù)語(yǔ) Ids...

Below WooCommerce version 3

為了獲得這些條款 ID,我使用 Wordpress 函數(shù) 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>';

我將得到這個(gè)輸出,每個(gè)屬性的真實(shí)術(shù)語(yǔ) ID 用于產(chǎn)品變體(我已經(jīng)安排了數(shù)組輸出以使其更緊湊):

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
            )
        )
    )
)

這篇關(guān)于獲取產(chǎn)品變體屬性值術(shù)語(yǔ) ID 和名稱的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問(wèn)題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 光环国际-新三板公司_股票代码:838504 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 透平油真空滤油机-变压器油板框滤油机-滤油车-华之源过滤设备 | 点胶机_点胶阀_自动点胶机_智能点胶机_喷胶机_点胶机厂家【欧力克斯】 | 福州仿石漆加盟_福建仿石漆厂家-外墙仿石漆加盟推荐铁壁金钢(福建)新材料科技有限公司有保障 | 客服外包专业服务商_客服外包中心_网萌科技| BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 东莞动力锂电池保护板_BMS智能软件保护板_锂电池主动均衡保护板-东莞市倡芯电子科技有限公司 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 威客电竞(vk·game)·电子竞技赛事官网 | 超声波清洗机_超声波清洗机设备_超声波清洗机厂家_鼎泰恒胜 | QQ房产导航-免费收录优秀房地产网站_房地产信息网 | 大米加工设备|大米加工机械|碾米成套设备|大米加工成套设备-河南成立粮油机械有限公司 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 浙江富广阀门有限公司 | 烟雾净化器-滤筒除尘器-防爆除尘器-除尘器厂家-东莞执信环保科技有限公司 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 执业药师报名条件,考试时间,考试真题,报名入口—首页 | 丹佛斯变频器-丹佛斯压力开关-变送器-广州市风华机电设备有限公司 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 膜结构车棚|上海膜结构车棚|上海车棚厂家|上海膜结构公司 | 乐考网-银行从业_基金从业资格考试_初级/中级会计报名时间_中级经济师 | 苏州伊诺尔拆除公司_专业酒店厂房拆除_商场学校拆除_办公楼房屋拆除_家工装拆除拆旧 | 河南中专学校|职高|技校招生-河南中职中专网 | 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 板材品牌-中国胶合板行业十大品牌-环保板材-上海声达板材 | 成都顶呱呱信息技术有限公司-贷款_个人贷款_银行贷款在线申请 - 成都贷款公司 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 深圳市八百通智能技术有限公司官方网站 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 环氧树脂地坪漆_济宁市新天地漆业有限公司 |