简体   繁体   English

为 WooCommerce 可变产品创建多个产品变体问题

[英]Create multiple product variations issue for a WooCommerce variable product

I am trying to add 2 product variations programmatically for a variable product.我正在尝试以编程方式为可变产品添加 2 个产品变体。 Based on this answer thread , I am using the following shortened function:基于this answer thread ,我使用以下缩短的function:

function create_product_variation( $product_id, $variation_data ){

    $product = wc_get_product($product_id);
    
    $variation_post = array(
        'post_title'  => $product->get_name(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );
    
    $variation_id = wp_insert_post( $variation_post );
    $variation = new WC_Product_Variation( $variation_id );
    
    foreach($variation_data as $_variation_data){
        foreach($_variation_data['attributes'] as $attribute => $term_name){
            $taxonomy = 'pa_'.$attribute;

            if( !taxonomy_exists( $taxonomy ) ){
                register_taxonomy(
                    $taxonomy,
                   'product_variation',
                    array(
                        'hierarchical' => false,
                        'label' => ucfirst($attribute),
                        'query_var' => true,
                        'rewrite' => array( 'slug' => sanitize_title($attribute)),
                    )
                );
            }

            if( ! term_exists( $term_name, $taxonomy ) )
                wp_insert_term( $term_name, $taxonomy );

            $term_slug = get_term_by('name', $term_name, $taxonomy )->slug;

            $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

            if( ! in_array( $term_name, $post_term_names ) )
                wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
            
            update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
        }       
    }
    
    
    $variation->save();
}

I use the following data array:我使用以下数据数组:

$variations_data = array( 
    array( 
        'attributes' => array( 
            'color' => 'Blue',
            'warranty-and-insurance' => 'W1',
         ),
        'sku' => ,
        'regular_price' => 2000,
        'sale_price' => 1800,
        'stock_qty' => 5,
    ),
    array( 
        'attributes' => array( 
            'color' => 'Red',
            'warranty-and-insurance' => 'W1',
        ),
        'sku' => ,
        'regular_price' => 3000,
        'sale_price' => 2800,
        'stock_qty' => 3,
    )
);

Then I run the function with the following:然后我使用以下命令运行 function:

create_product_variation( $variable_product_id, $variations_data ); 
    

where $variable_product_id is the id of the variable product which I want to make variations for it and $variations_data the data array defined above.其中$variable_product_id是我想要对其进行变体的变量产品的 id, $variations_data是上面定义的数据数组。

My problem is that the update_post_meta() function just insert the last data from the foreach in function.我的问题是update_post_meta() function 只是在 function 中插入来自 foreach 的最后一个数据。

ie in product variation in woocommerce there are:即在 woocommerce 的产品变化中有:

Red W1红W1

Red W1红W1

but I want:但我想要:

Blue W1蓝色 W1

Red W1红W1

In your code, the first foreach loop need to be just before the following line of code:在您的代码中,第一个 foreach 循环必须位于以下代码行之前:

$variation_id = wp_insert_post( $variation_post );

like:喜欢:

function create_product_variations( $product_id, $variations_data ){
    $product = wc_get_product($product_id);
    
    $variation_post = array(
        'post_title'  => $product->get_name(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );

    foreach( $variations_data as $variation_data ){
        
        $variation_id = wp_insert_post( $variation_post );
        
        $variation = new WC_Product_Variation( $variation_id );

        foreach($_variation_data['attributes'] as $attribute => $term_name){
            $taxonomy = 'pa_'.$attribute;

            if( ! taxonomy_exists( $taxonomy ) ){
                register_taxonomy(
                    $taxonomy,
                   'product_variation',
                    array(
                        'hierarchical' => false,
                        'label' => ucfirst($attribute),
                        'query_var' => true,
                        'rewrite' => array( 'slug' => sanitize_title($attribute)),
                    )
                );
            }

            if( ! term_exists( $term_name, $taxonomy ) )
                wp_insert_term( $term_name, $taxonomy );

            $term_slug = get_term_by('name', $term_name, $taxonomy )->slug;

            $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

            if( ! in_array( $term_name, $post_term_names ) )
                wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
            
            update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );      
        }
        $variation->save();
    }
}

As wp_insert_post() create a product variation in database, with a unique variation ID, so it need to be done for each variation in your data array.由于wp_insert_post()在数据库中创建了一个具有唯一变体 ID 的产品变体,因此需要为数据数组中的每个变体执行此操作。

This should solve your main related problem.这应该可以解决您的主要相关问题。


Related:有关的:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM