Woocommerce:使用现有属性为现有产品添加变体

时间:2021-07-30 20:18:43

I'm trying to figure out the way how to add variation to existing product that is not originally variable product.

我试图找出如何为现有产品添加变体的方式,而不是最初的变量产品。

So I have a product Shirt and I get another one of those in stock in different color so my product importer needs to add a new variation to this existing product.

所以我有一个产品衬衫,我有另外一个有不同颜色的库存,所以我的产品进口商需要为这个现有产品添加一个新的变化。

wp_set_object_terms ($product_id, 'black', 'pa_color', 1);

$attr_data = Array(
            'pa_color'=>Array(
                'name' => 'pa_color',
                'value' => '',
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
            )
        );
update_post_meta($product_id, '_product_attributes', $attr_data);

This adds a color to my product but destroys all my existing attributes on the product. Pulling the existing _product_attributes just gives me serialized attributes so just adding the new variation on top of everything isn't working.

这会为我的产品添加颜色,但会破坏产品上的所有现有属性。拉动现有的_product_attributes只会给我序列化的属性,所以只是在所有内容之上添加新的变体是行不通的。

Any ideas?

2 个解决方案

#1


1  

Basically the problem is that product_attribute is not a single variable and it seems that there is no merge in wp_set_object_terms

基本上问题是product_attribute不是单个变量,似乎wp_set_object_terms中没有合并

I solved my problem like this:

我这样解决了我的问题:

wp_set_object_terms ($product_id, 'black', 'pa_color', 1);

        $attr_data = Array(
            'pa_color'=>Array(
                'name' => 'pa_color',
                'value' => '',
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
            )
        );

        $product = new WC_Product($product_id);

        update_post_meta( $product_id, '_product_attributes', array_merge($product->get_attributes(), $attr_data) );

#2


-1  

I having the same problems, and just resolved.

我有同样的问题,刚刚解决了。

get term_id for product_type = variable

获取product_type = variable的term_id

/**
    SELECT $wpdb->terms.term_id FROM $wpdb->terms, $wpdb->term_taxonomy 
    WHERE name = 'variable' 
    AND taxonomy = 'product_type'
    AND $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id
    LIMIT 1
**/

$variable_term_id  = 4;

wp_set_object_terms( $product_id, $variable_term_id, 'product_type' , true); //for variable product

:)

#1


1  

Basically the problem is that product_attribute is not a single variable and it seems that there is no merge in wp_set_object_terms

基本上问题是product_attribute不是单个变量,似乎wp_set_object_terms中没有合并

I solved my problem like this:

我这样解决了我的问题:

wp_set_object_terms ($product_id, 'black', 'pa_color', 1);

        $attr_data = Array(
            'pa_color'=>Array(
                'name' => 'pa_color',
                'value' => '',
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
            )
        );

        $product = new WC_Product($product_id);

        update_post_meta( $product_id, '_product_attributes', array_merge($product->get_attributes(), $attr_data) );

#2


-1  

I having the same problems, and just resolved.

我有同样的问题,刚刚解决了。

get term_id for product_type = variable

获取product_type = variable的term_id

/**
    SELECT $wpdb->terms.term_id FROM $wpdb->terms, $wpdb->term_taxonomy 
    WHERE name = 'variable' 
    AND taxonomy = 'product_type'
    AND $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id
    LIMIT 1
**/

$variable_term_id  = 4;

wp_set_object_terms( $product_id, $variable_term_id, 'product_type' , true); //for variable product

:)