Add WooCommerce product attribute without losing existings不能解决我的问题,因为我会手动创建一个包含所有现有属性的数组,而且它并不像我想要的那样通用,似乎是一种糟糕的做法.假设一个产品具有X个属性,其中X个属性的值为X,我想为该产品添加一个带有值的新属性,以下是我的try :

$modelagem = "desconhecida"; // new attribute value
$attribute_pa_at_modelagem_id = wc_attribute_taxonomy_id_by_name('at-modelagem'); // new attribute, already registered in wordpress NOT IN THE PRODUCT
$modelagem_attribute = new WC_Product_Attribute(); // new attribute object
$modelagem_attribute->set_id($attribute_pa_at_modelagem_id); // setting attribute configs
$modelagem_attribute->set_name('pa_at-modelagem'); // setting attribute configs
$modelagem_attribute->set_options([$modelagem]); // setting attribute configs
$modelagem_attribute->set_visible(true); // setting attribute configs
$modelagem_attribute->set_variation(false); // setting attribute configs
$modelagem_attribute->set_position(0); // setting attribute configs
$existing_attributes = $product->get_attributes(); // taking existing attributes (IT'S EMPTY I DONT KNOW WHY)
$existing_attributes[] = $modelagem_attribute; // then appending new attribute to it

$product->set_attributes($existing_attributes); // IT ERASES ALL ATTRIBUTES
$product->save();

正如我所说的,上面的代码删除了给定产品的所有属性和变化,而不管我需要一种方法来设置一个新的属性,而不是一次设置所有属性,这是一个不好的做法,可能会导致错误的变化.

另注:请忽略琐碎的问题,WooCommerce未加载,插件冲突等…

推荐答案

产品属性作为序列化数组存储在wp_postmeta中.尽管您不希望再次设置所有现有值,但将新值与现有的AND设置合并是正确的方法.

我在您的代码中看到的问题之一是,您试图按原样使用WP_Product::Get_Attributes的输出.您需要向下迭代并访问各个对象,然后将它们推送到$EXISTING_ATTRIBUTES数组中.

$product_attributes = $product->get_attributes();

foreach ($product_attributes as $attribute_name => $attribute_object) {
    $existing_attributes[] = $attribute_object;
}

$modelagem = "desconhecida"; // new attribute value
$attribute_pa_at_modelagem_id = wc_attribute_taxonomy_id_by_name('at-modelagem'); // new attribute, already registered in wordpress NOT IN THE PRODUCT
$modelagem_attribute = new WC_Product_Attribute(); // new attribute object
$modelagem_attribute->set_id($attribute_pa_at_modelagem_id); // setting attribute configs
$modelagem_attribute->set_name('pa_at-modelagem'); // setting attribute configs
$modelagem_attribute->set_options([$modelagem]); // setting attribute configs
$modelagem_attribute->set_visible(true); // setting attribute configs
$modelagem_attribute->set_variation(false); // setting attribute configs
$modelagem_attribute->set_position(0); // setting attribute configs

$existing_attributes[] = $modelagem_attribute; // then appending new attribute to it

$product->set_attributes($set_attributes);
$product->save();

Php相关问答推荐

当我们使用PHPUnit时,控制台中的--uses param到底起了什么作用以及如何使用?

Laravel 9如何过滤$的项目与结果?

如何隐藏x轴图表上的值

在WooCommerce管理订单页面中显示区域名称

WooCommerce:有条件地更改特定区域的发货类别

无法在Laravel/Vue停靠容器中启动MySQL

PHP测试字符串的所有POST值

将WooCommerce WC_Order对象与wp_Schedule_Event一起使用

PHP SimpleXML:按属性值访问 node

登录后重定向在WooCommerce中购买产品的用户

调用未定义的方法 mysqli::execute_query()

如何更新 Laravel Eloquent 列强制转换为集合

PHP 中的 libgd 无法读取 Apache 配置中的 SetEnv GDFONTPATH

如何在 PHP 中对具有相同数组值的值求和

PHP简单地将物品装在盒子里会导致未使用的空间,但还有剩余容量

在WooCommerce可变产品中显示所选变体的自定义字段

在wordpress注销后防止后退操作

保留 .htaccess 中的符号登录 URL

为什么可以从 PHP 类访问 PHP Trait 的私有成员?

getenv() 有时在 CodeIgniter 4 中返回 false.为什么?