wordpress - How update product attributes programmatically?
问题描述
I use following code to add attributes to a product:
foreach ($_attributes as $name => $value) {
wp_set_object_terms($post_id, $value, $name, true);
$product_attributes[$name] = array (
'name' => $name, // set attribute name
'value' => $value, // set attribute value
'is_visible' => 0,
'is_variation' => 1,
'is_taxonomy' => 1
);
}
update_post_meta($post_id, '_product_attributes', $product_attributes );
but it remove previous attributes I added in product edit in admin, like product brand or model. How can I update current product attributes without remove previous ones?
Thanks to help me.
解决方案
您可以在更新之前简单地备份您的数据库内容,如下所示:
$tmpBk = get_post_meta($post_id, '_product_attributes',true);
update_post_meta($post_id, '_product_attributes', array_merge(tmpBk,$product_attributes) );
这应该足以保存您以前存储的值
推荐阅读
- python - Selenium:网站中的特定页面出现空白/白屏
- amazon-web-services - AWS CLI 列出所有 S3 存储桶的加密状态
- php - 删除 url 中的文件扩展名不起作用
- javascript - 模拟类似于 GOTO 的命令的标签
- javascript - 推送新对象时,对象中的嵌套数组最后一个元素被删除
- google-api - Google Classroom API 访问要求
- angular - Angular,RxJs,初始化组件的子变量
- node.js - 服务器没有向前端发送正确的响应?
- ruby - 新手 Ruby 问题 - 项目基础计算器
- sql - 由于未找到标识符,存储过程插入表无法正常工作