如何使用代码从 WooCommerce 产品中删除特定属性值

问题描述 投票:0回答:1

我有一个全局属性

pa_status
,其值包括
new
old
custom
。 我的产品中使用了此属性。

我想从特定产品中删除

old
值。

我的第一个想法是使用 Woocommerce CRUD,如下所示,但它不起作用。 也许我应该以完全不同的方式来做?也许

wp_remove_object_terms
在这种情况下会有帮助?

$id_product = 73886;
$attribute_name = 'pa_status';
$id_attribute_value_to_remove = 1330;  // name: old

$product = wc_get_product($id_product);
$attributes = $product->get_attributes();

if (isset($attributes[$attribute_name])) {
    $attributes[$attribute_name]['options'] = array_filter(
        $attributes[$attribute_name]['options'],
        function ($value) use ($id_attribute_value_to_remove) {
            return $value !== $id_attribute_value_to_remove;
        }
    );
}
$product->set_attributes($attributes);
$product->save();

我需要使用 php 来完成此操作,因为它将在稍后阶段由 cron 处理

wordpress woocommerce attributes crud hook-woocommerce
1个回答
-1
投票

@kanlukasz,

您应该能够从产品页面中删除单个商品的属性。

从此页面删除属性 pa_status 的旧选项并保存项目。

我希望这有帮助。

参考:

https://learnwoo.com/woocommerce-attributes-variations/

© www.soinside.com 2019 - 2024. All rights reserved.