使用 CRUD 操作从 WooCommerce 产品中删除属性值

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

我有一个全局属性

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

我想使用 WooCommerce 中提供的

CRUD
操作从产品中删除 old 值。

当前代码如下所示,但不起作用:

$id_product = 77233;
$attribute_name = 'pa_status';
$attribute_value_to_remove_by_id = 59;

$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 ($attribute_value_to_remove_by_id) {
            return $value !== $attribute_value_to_remove_by_id;
        }
    );
}

$product->set_attributes($attributes);
$product->save();
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.