获取产品变体属性值术语ID和名称

时间:2017-02-27 22:35:18

标签: php wordpress woocommerce product variations

在WooCommerce中,我使用$product->get_variation_attributes()获取产品的变体属性。此函数返回一个名称不带ID的数组。

像这样:

                   [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那样) 我搜了好几天但找不到解决办法。

我创建了这段代码:

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。

我的问题是,是否有人知道我如何能够像get_variation_attributes()那样完成相同的工作,但身份证明了?

感谢。

1 个答案:

答案 0 :(得分:7)

  

WooCommerce版本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 ......

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

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 (我已安排数组输出以使其更紧凑):< / p>

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