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

Create multiple product variations issue for a WooCommerce variable product

我正在尝试以编程方式为可变产品添加 2 个产品变体。 基于 ,我正在使用以下缩短的函数:

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();
}

我使用以下数据数组:

$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,
    )
);

然后我运行函数如下:

create_product_variation( $variable_product_id, $variations_data ); 
    

其中 $variable_product_id 是我要对其进行变体的可变产品的 ID,$variations_data 是上面定义的数据数组。

我的问题是 update_post_meta() 函数只是插入函数中 foreach 的最后一个数据。

即在 woocommerce 的产品变体中有:

红W1

红W1

但我想要:

蓝W1

红W1

在您的代码中,第一个 foreach 循环需要紧接在以下代码行之前:

$variation_id = wp_insert_post( $variation_post );

喜欢:

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();
    }
}

由于 wp_insert_post() 在数据库中创建产品变体,具有唯一的变体 ID,因此需要为数据数组中的每个变体完成此操作。

这应该可以解决您的主要相关问题。


相关: