php Wordpress Woocommerce - 使用 update_post_meta 添加产品属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23444319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress Woocommerce - use update_post_meta to add product attributes
提问by Daniel
The products in my clients website require certain attributes which I have added via Products -> Attributesin the Wordpress administration. In this import script I'm coding I need to use the function update_post_meta($post_id, $meta_key, $meta_value)
to import the proper attributes and values.
我客户网站上的产品需要某些属性,我通过产品 ->Wordpress 管理中的属性添加了这些属性。在我编写的这个导入脚本中,我需要使用该函数update_post_meta($post_id, $meta_key, $meta_value)
来导入正确的属性和值。
Currently I have the function like so:
目前我有这样的功能:
update_post_meta( $post_id, '_product_attributes', array());
However I'm not sure how to properly pass along the attributes and their values?
但是我不确定如何正确传递属性及其值?
回答by Daniel
Right so it took me a while to figure it out myself but I finally managed to do this by writing the following function:
是的,所以我自己花了一段时间才弄明白,但我终于通过编写以下函数设法做到了这一点:
// @param int $post_id - The id of the post that you are setting the attributes for
// @param array[] $attributes - This needs to be an array containing ALL your attributes so it can insert them in one go
function wcproduct_set_attributes($post_id, $attributes) {
$i = 0;
// Loop through the attributes array
foreach ($attributes as $name => $value) {
$product_attributes[$i] = array (
'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
'value' => $value, // set attribute value
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
$i++;
}
// Now update the post with its new attributes
update_post_meta($post_id, '_product_attributes', $product_attributes);
}
// Example on using this function
// The attribute parameter that you pass along must contain all attributes for your product in one go
// so that the wcproduct_set_attributes function can insert them into the correct meta field.
$my_product_attributes = array('hdd_size' => $product->hdd_size, 'ram_size' => $product->ram_size);
// After inserting post
wcproduct_set_attributes($post_id, $my_product_attributes);
// Woohay done!
I hope this function will help other people if they need to import multiple attributes pro-grammatically in WooCommerce!
我希望这个功能可以帮助其他需要在 WooCommerce 中以编程方式导入多个属性的人!
回答by Tizzy
I tried Daniel's answer, and it didn't work for me. It might be that the Wordpress/Woocommerce code has changed since, or perhaps I didn't quite understand how to do it, but either way that code did nothing for me. After a lot of work using it as a base, however, I came up with this snippet of code and put it on my theme's functions.php
:
我尝试了丹尼尔的回答,但对我不起作用。可能是 Wordpress/Woocommerce 代码从那以后发生了变化,或者我不太明白如何去做,但无论哪种方式,这些代码对我都没有任何帮助。然而,在使用它作为基础进行大量工作之后,我想出了这段代码并将其放在我的主题上functions.php
:
function wcproduct_set_attributes($id) {
$material = get_the_terms( $id, 'pa_material');
$material = $material[0]->name;
// Now update the post with its new attributes
update_post_meta($id, '_material', $material);
}
// After inserting post
add_action( 'save_post_product', 'wcproduct_set_attributes', 10);
With this, I can take what I set as "material" on my WooCommerce install as a custom attribute and add it to the formal meta as _material. This in turn allows me to use another snippet of code so the WooCommerce search function extends to meta fields, meaning I can search for a material in the WooCommerce search field and have all items with that material appear.
有了这个,我可以将我在 WooCommerce 安装中设置为“材料”的内容作为自定义属性,并将其作为 _material 添加到正式元数据中。这反过来又允许我使用另一段代码,以便 WooCommerce 搜索功能扩展到元字段,这意味着我可以在 WooCommerce 搜索字段中搜索材料,并显示包含该材料的所有项目。
I hope this is useful to somebody.
我希望这对某人有用。
回答by Tizzy
@Daniels's answer works, won't decide on right or wrong, however if you want to add the values as a taxonomy term under attributes you have to adapt the code as below (set is_taxonomy = 1). Otherwise Woocommerce sees it as custom meta field(?). It still adds the value under attributes. This will only work for strings. For values that are arrays the code has to be adapted.
@Daniels 的答案有效,不会决定对与错,但是如果您想将值作为分类术语添加到属性下,您必须调整如下代码(设置 is_taxonomy = 1)。否则 Woocommerce 会将其视为自定义元字段(?)。它仍然在属性下添加值。这仅适用于字符串。对于数组值,必须修改代码。
Additionally it uses the wp_set_object_terms that @Anand suggests as well. I was using that, because all the documentation I could find led to believe that had to be used. However if one only uses the wp_set_object_terms then I couldn't see the attributes in the edit product screen. Using the information from both answers and reading on the subject resulted in the solution.
此外,它还使用@Anand 建议的 wp_set_object_terms。我正在使用它,因为我能找到的所有文档都让人相信必须使用它。但是,如果仅使用 wp_set_object_terms,则我无法在编辑产品屏幕中看到属性。使用来自答案和阅读主题的信息得出了解决方案。
You will need to tweak the code for things such as product variations.
您将需要针对产品变体等内容调整代码。
/*
* Save Woocommerce custom attributes
*/
function save_wc_custom_attributes($post_id, $custom_attributes) {
$i = 0;
// Loop through the attributes array
foreach ($custom_attributes as $name => $value) {
// Relate post to a custom attribute, add term if it does not exist
wp_set_object_terms($post_id, $value, $name, true);
// Create product attributes array
$product_attributes[$i] = array(
'name' => $name, // set attribute name
'value' => $value, // set attribute value
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 1
);
$i++;
}
// Now update the post with its new attributes
update_post_meta($post_id, '_product_attributes', $product_attributes);
}
Then call the function:
然后调用函数:
$custom_attributes = array('pa_name_1' => $value_1, 'pa_name_2' => $value_2, 'pa_name_3' => $value_3);
save_wc_custom_attributes($post_id, $custom_attributes);
Thank you for posting the code Daniel & Anand. It helped me a great deal.
感谢您发布代码 Daniel & Anand。它帮了我很大的忙。
回答by john
Don't know if this is the "correct" way to do this... But I needed a function to add ACF repeater fields with a date value as a attribute on post save, so this was the function I came up with:
不知道这是否是执行此操作的“正确”方法......但我需要一个函数来添加带有日期值的 ACF 转发器字段作为保存后的属性,所以这就是我想出的函数:
add_action( 'save_post', 'ed_save_post_function', 10, 3 );
function ed_save_post_function( $post_ID, $post, $update ) {
//print_r($post);
if($post->post_type == 'product')
{
$dates = get_field('course_dates', $post->ID);
//print_r($dates);
if($dates)
{
$date_arr = array();
$val = '';
$i = 0;
foreach($dates as $d)
{
if($i > 0)
{
$val .= ' | '.date('d-m-Y', strtotime($d['date']));
}
else{
$val .= date('d-m-Y', strtotime($d['date']));
}
$i++;
}
$entry = array(
'course-dates' => array(
'name' => 'Course Dates',
'value' => $val,
'position' => '0',
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
)
);
update_post_meta($post->ID, '_product_attributes', $entry);
}
}
}
Hope this helps someone.
希望这可以帮助某人。