php - 当前数据和数据之间的Woocommerce区别
问题描述
我正在尝试meta_data
使用对象更新 WooCommerce 中update_meta_data()
的值WC_Product_Simple
。
但是,如果我导航以在 Woocommerce/WordPress 后端编辑指定产品,我所做的更改不会显示。为了调试这个,我记录了我试图更改到我的屏幕的产品元数据:
[8] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1312
[key] => _nf_funding_goal
[value] => 999
)
[data:protected] => Array
(
[id] => 1312
[key] => _nf_funding_goal
[value] => 30
)
)
我试图将元数据更改_nf_funding_goal
为999
. 如您所见,这种变化反映在内部current_data
。但不在data
. 我猜这就是编辑产品时未显示更改的原因。有人可以告诉我为什么,以及如何改变data
而不是current_data
?
提前致谢。
解决方案
您可以为 woocommerce 产品保存数据的方式如下。
// Get Product Object
$_product = wc_get_product($product_id);
// Do the updation, this doesn't acutally update the product.
$_product->update_meta_data('_nf_funding_goal', 999);
// This would acutally call save_post and persist changes to your product
$_product->save(); // You might be missing this!
我怀疑你一定错过$_product->save()
了你的功能。
推荐阅读
- python - 如何打印具有不同颜色的对象的坐标?Python3 OpenCV
- r - create a new variable in each iteration
- typescript - 给一个字符串一个类型
- c# - c# DataGridView DataTable 内部索引在并行循环中损坏
- css - vue cli 3 – 在样式标签中使用背景图片
- javascript - 谷歌浏览器扩展权限问题,带有谷歌文档(仅限)
- vb.net - 子报表增长到主报表不创建新页面
- android - TextView 中的水平居中文本不起作用
- php - 如何将 php、nodejs 和 mysql 应用程序部署到 IBM Cloud
- sql - 如何在 SQL Server 表的所有行中添加唯一标识符