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

时间:2020-09-30 09:19:42

标签: wordpress woocommerce plugins

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

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

,但会删除我在admin产品编辑中添加的先前属性,例如产品品牌或型号。 如何在不删除先前属性的情况下更新当前产品属性?

谢谢帮助。

1 个答案:

答案 0 :(得分:1)

您只需在更新之前备份数据库内容,就像这样:

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

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