Php 以编程方式设置变量产品的默认属性值
我正在构建一个站点,其中以编程方式插入可变产品(带有产品变体)。我成功地学习了本教程:Php 以编程方式设置变量产品的默认属性值,php,wordpress,woocommerce,product,variations,Php,Wordpress,Woocommerce,Product,Variations,我正在构建一个站点,其中以编程方式插入可变产品(带有产品变体)。我成功地学习了本教程: 我只需要缺少的东西: 如何设置变量产品的默认属性值? 有可能吗?1)。您需要在每个变量产品的json数据中插入此数组,如: "variations_default_attributes": [ { "size" : "Medium",
我只需要缺少的东西:
如何设置变量产品的默认属性值? 有可能吗?1)。您需要在每个变量产品的
json
数据中插入此数组,如:
"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文件或任何插件文件中
此代码经过测试并正常工作。1)。您需要在每个变量产品的json
数据中插入此数组,如:
"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文件或任何插件文件中
这段代码已经过测试并运行。尝试过更新\u post\u meta($post\u id、'u default\u attributes'、$variations\u default\u attributes);在wc3.0中,这不起作用。因此我尝试了以下方法:$standardValArr=array()$标准瓦拉尔['pa_groesse']='M'$父产品->设置默认属性($standardValArr);不幸的是,这不起作用。@JanineKroser你想做什么?你的问题是什么?可能你应该问一个新问题,因为这个答案可能对你不方便。尝试了更新发布meta($post\u id,''u default\u attributes',$variations\u default\u attributes);在wc3.0中,这不起作用。因此我尝试了以下方法:$standardValArr=array()$标准瓦拉尔['pa_groesse']='M'$父产品->设置默认属性($standardValArr);不幸的是,这不起作用。@JanineKroser你想做什么?你的问题是什么?也许你应该问一个新问题,因为这个答案可能对你不方便。