Php 为可变产品创建多个产品变体问题
我试图以编程方式为一个可变产品添加两个产品变体。 基于,我使用以下缩短的函数:Php 为可变产品创建多个产品变体问题,php,wordpress,woocommerce,product,product-variations,Php,Wordpress,Woocommerce,Product,Product Variations,我试图以编程方式为一个可变产品添加两个产品变体。 基于,我使用以下缩短的函数: function create_product_variation( $product_id, $variation_data ){ $product = wc_get_product($product_id); $variation_post = array( 'post_title' => $product->get_name(), 'po
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\u product\u id
是我要对其进行更改的变量产品的id,$variations\u data
是上面定义的数据数组
我的问题是update\u post\u meta()
函数只插入foreach in函数中的最后一个数据
i、 e.在商业中的产品变化有:
红色W1
红色W1
但我想:
蓝色W1
红色W1在您的代码中,第一个foreach循环需要正好位于以下代码行之前:
$variation_id = wp_insert_post( $variation_post );
比如:
正如wp\u insert\u post()
在数据库中创建具有唯一变体ID的产品变体一样,因此需要为数据数组中的每个变体执行此操作
这将解决您的主要相关问题
相关的:
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();
}
}