获取产品变体属性值的术语ID和名称 [英] Get the product variations attributes values term ID and name

查看:99
本文介绍了获取产品变体属性值的术语ID和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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.

我的问题是,有谁知道我能完成与get_variation_attributes()相同的任务,但要使用ID?

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

Below WooCommerce version 3

我在原始数据数组中没有看到任何重复的版本ID…您的问题不是很清楚,因此很难猜测您正在寻找哪些丢失的ID.然后冒险冒险回答,并假设缺少的ID是属性值中的IDs…

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…

要获取此条款ID,我使用Wordpress函数 get_term_by() ,这样:

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

foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){
    // To get the taxonomy object
    $taxonomy_obj = get_taxonomy( $taxonomy );

    $taxonomy_name = $taxonomy_obj->name; // Name (we already got it)
    $taxonomy_label = $taxonomy_obj->label; // Label

    // Setting some data in an array
    $variations_attributes_and_values[$taxonomy] = array('label' => $taxonomy_obj->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
        );
    }
}

并且:

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和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆