如何以编程方式更新产品属性?

How update product attributes programmatically?

我使用以下代码向产品添加属性:

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 );

但它删除了我之前在管理员的产品编辑中添加的属性,例如产品品牌或型号。 如何在不删除以前的产品属性的情况下更新当前的产品属性?

感谢帮助我。

您可以在更新之前简单地备份您的数据库内容,如下所示:

$tmpBk = get_post_meta($post_id, '_product_attributes',true);
update_post_meta($post_id, '_product_attributes', array_merge(tmpBk,$product_attributes) );

这应该足以保存您之前存储的值