首页 > 解决方案 > 当前数据和数据之间的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_goal999. 如您所见,这种变化反映在内部current_data。但不在data. 我猜这就是编辑产品时未显示更改的原因。有人可以告诉我为什么,以及如何改变data而不是current_data

提前致谢。

标签: phpwordpresswoocommerce

解决方案


您可以为 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()了你的功能。


推荐阅读