以编程方式为可变产品设置变体默认属性值
Set variations default attributes values for a variable product Programmatically
我正在构建一个网站,其中以编程方式插入 WooCommerce 可变产品(具有产品变体)。我已成功遵循本教程:
Insert WooCommerce Products & Variations Programmatically
我只需要缺少的东西:
如何为可变产品设置变体默认属性值?
可能吗?
1).您需要为每个变量产品插入 json
数据,例如:
"variations_default_attributes":
[
{
"size" : "Medium",
"color" : "Blue"
}
]
或
"variations_default_attributes":
{
"size" : "Medium",
"color" : "Blue"
}
这个数组是用属性 short slug(没有'pa_
')和默认术语 name 值。
2).然后是专用函数:
function insert_variations_default_attributes( $post_id, $products_data ){
foreach( $products_data as $attribute => $value )
$variations_default_attributes['pa_'.$attribute] = get_term_by( 'name', $value, 'pa_'.$attribute )->slug;
// Save the variation default attributes to variable product meta data
update_post_meta( $post_id, '_default_attributes', $variations_default_attributes );
}
3).您需要触发此函数,在末尾添加一行:
function insert_product ($product_data)
{
$post = array( // Set up the basic post data to insert for our product
'post_author' => 1,
'post_content' => $product_data['description'],
'post_status' => 'publish',
'post_title' => $product_data['name'],
'post_parent' => '',
'post_type' => 'product'
);
$post_id = wp_insert_post($post); // Insert the post returning the new post id
if (!$post_id) // If there is no post id something has gone wrong so don't proceed
{
return false;
}
update_post_meta($post_id, '_sku', $product_data['sku']); // Set its SKU
update_post_meta( $post_id,'_visibility','visible'); // Set the product to visible, if not it won't show on the front end
wp_set_object_terms($post_id, $product_data['categories'], 'product_cat'); // Set up its categories
wp_set_object_terms($post_id, 'variable', 'product_type'); // Set it to a variable product type
insert_product_attributes($post_id, $product_data['available_attributes'], $product_data['variations']); // Add attributes passing the new post id, attributes & variations
insert_product_variations($post_id, $product_data['variations']); // Insert variations passing the new post id & variations
## Insert variations default attributes passing the new post id & variations_default_attributes
insert_variations_default_attributes( $post_id, $products_data['variations_default_attributes'] );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
我正在构建一个网站,其中以编程方式插入 WooCommerce 可变产品(具有产品变体)。我已成功遵循本教程:
Insert WooCommerce Products & Variations Programmatically
我只需要缺少的东西:
如何为可变产品设置变体默认属性值?
可能吗?
1).您需要为每个变量产品插入 json
数据,例如:
"variations_default_attributes":
[
{
"size" : "Medium",
"color" : "Blue"
}
]
或
"variations_default_attributes":
{
"size" : "Medium",
"color" : "Blue"
}
这个数组是用属性 short slug(没有'pa_
')和默认术语 name 值。
2).然后是专用函数:
function insert_variations_default_attributes( $post_id, $products_data ){
foreach( $products_data as $attribute => $value )
$variations_default_attributes['pa_'.$attribute] = get_term_by( 'name', $value, 'pa_'.$attribute )->slug;
// Save the variation default attributes to variable product meta data
update_post_meta( $post_id, '_default_attributes', $variations_default_attributes );
}
3).您需要触发此函数,在末尾添加一行:
function insert_product ($product_data)
{
$post = array( // Set up the basic post data to insert for our product
'post_author' => 1,
'post_content' => $product_data['description'],
'post_status' => 'publish',
'post_title' => $product_data['name'],
'post_parent' => '',
'post_type' => 'product'
);
$post_id = wp_insert_post($post); // Insert the post returning the new post id
if (!$post_id) // If there is no post id something has gone wrong so don't proceed
{
return false;
}
update_post_meta($post_id, '_sku', $product_data['sku']); // Set its SKU
update_post_meta( $post_id,'_visibility','visible'); // Set the product to visible, if not it won't show on the front end
wp_set_object_terms($post_id, $product_data['categories'], 'product_cat'); // Set up its categories
wp_set_object_terms($post_id, 'variable', 'product_type'); // Set it to a variable product type
insert_product_attributes($post_id, $product_data['available_attributes'], $product_data['variations']); // Add attributes passing the new post id, attributes & variations
insert_product_variations($post_id, $product_data['variations']); // Insert variations passing the new post id & variations
## Insert variations default attributes passing the new post id & variations_default_attributes
insert_variations_default_attributes( $post_id, $products_data['variations_default_attributes'] );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。