将具有值的产品属性添加到Woocommerce中的产品 [英] Add Product Attributes with values to a product in Woocommerce

查看:148
本文介绍了将具有值的产品属性添加到Woocommerce中的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码添加自定义属性

I am using this code to add custom attributes

$attributes = array(
    array("name"=>"Size","options"=>array("S","L","XL","XXL"),"position"=>1,"visible"=>1,"variation"=>1),
    array("name"=>"Color","options"=>array("Red","Blue","Black","White"),"position"=>2,"visible"=>1,"variation"=>1)
);
if($attributes){
    $productAttributes=array();
    foreach($attributes as $attribute){
        $attr = wc_sanitize_taxonomy_name(stripslashes($attribute["name"])); // remove any unwanted chars and return the valid string for taxonomy name
        $attr = 'pa_'.$attr; // woocommerce prepend pa_ to each attribute name
        if($attribute["options"]){
            foreach($attribute["options"] as $option){
                wp_set_object_terms($product_id,$option,$attr,true); // save the possible option value for the attribute which will be used for variation later
            }
        }
        $productAttributes[sanitize_title($attr)] = array(
            'name' => sanitize_title($attr),
            'value' => $attribute["options"],
            'position' => $attribute["position"],
            'is_visible' => $attribute["visible"],
            'is_variation' => $attribute["variation"],
            'is_taxonomy' => '1'
        );
    }
    update_post_meta(11874,'_product_attributes',$productAttributes); // save the meta entry for product attributes

此代码的结果是我只添加了产品属性Name,没有术语值...

The result of this code I got added just product attribute Name without term values...

看图片

我进行了很多搜索,但没有得到任何答案.

I searched about that very much but did not got any answer.

推荐答案

您的代码中有一些错误.您的主要错误:要保存为产品元数据(最后)的属性术语必须是术语ID数组 (而不是术语名称).

There is some mistakes in your code. Your main mistake: The attribute terms to save as product meta data (at the end) need to be an array of term IDs (instead of term names).

尝试以下重新访问的代码:

Try the following revisited code:

$product_id = 11874;

$attributes_data = array(
    array('name'=>'Size',  'options'=>array('S', 'L', 'XL', 'XXL'), 'visible' => 1, 'variation' => 1 ),
    array('name'=>'Color', 'options'=>array('Red', 'Blue', 'Black', 'White'), 'visible' => 1, 'variation' => 1 )
);

if( sizeof($attributes_data) > 0 ){
    $attributes = array(); // Initializing

    // Loop through defined attribute data
    foreach( $attributes_data as $key => $attribute_array ) {
        if( isset($attribute_array['name']) && isset($attribute_array['options']) ){
            // Clean attribute name to get the taxonomy
            $taxonomy = 'pa_' . wc_sanitize_taxonomy_name( $attribute_array['name'] );

            $option_term_ids = array(); // Initializing

            // Loop through defined attribute data options (terms values)
            foreach( $attribute_array['options'] as $option ){
                if( term_exists( $option, $taxonomy ) ){
                    // Save the possible option value for the attribute which will be used for variation later
                    wp_set_object_terms( $product_id, $option, $taxonomy, true );
                    // Get the term ID
                    $option_term_ids[] = get_term_by( 'name', $option, $taxonomy )->term_id;
                }
            }
        }
        // Loop through defined attribute data
        $attributes[$taxonomy] = array(
            'name'          => $taxonomy,
            'value'         => $option_term_ids, // Need to be term IDs
            'position'      => $key + 1,
            'is_visible'    => $attribute_array['visible'],
            'is_variation'  => $attribute_array['variation'],
            'is_taxonomy'   => '1'
        );
    }
    // Save the meta entry for product attributes
    update_post_meta( $product_id, '_product_attributes', $attributes );
}

经过测试,可以正常工作.

Tested and works.

注意:所有产品属性及其值都需要定义(使用您的实际代码)

Note: All product attributes and their values need to be defined (with your actual code)

相关:在Woocommerce中以编程方式创建新产品属性

这篇关于将具有值的产品属性添加到Woocommerce中的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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