php - 在 WooCommerce 3+ 中更改现有产品的产品类型
问题描述
我需要在创建后使用我的子主题中的函数更改 woocommerce 产品类型,我已经尝试过:
wp_set_object_terms($product_id, 'variable', 'product_type', true);
我的方法在创建后触发但类型仍然不变?
解决方案
从 WooCommerce 3 开始,要从现有产品更改产品类型,您可以使用以下内容:
/**
* Change product type.
*
* @param int $product_id - The product id.
* @param string $new_product_type - The new product type
*/
// Get the correct product classname from the new product type
$product_classname = WC_Product_Factory::get_product_classname( $product_id, $new_product_type );
// Get the new product object from the correct classname
$new_product = new $product_classname( $product_id );
// Save product to database and sync caches
$new_product->save();
在所有情况下都经过测试和工作,因为wp_set_object_terms()
, 由于多种原因不能很好地工作,请参阅这个较旧的相关线程。新产品将保留其所有现有数据。
推荐阅读
- automated-tests - RestAssured API 调用出了什么问题
- r - 数据框上的摘要显示了字符字段的专业值。当我对其应用 substr 时:我收到“无用”信息
- hive - 将数据加载到单个列中时的 Hive Serde
- react-native - React-native Fb(错误:没有找到适合 AccessToken 的构造函数)
- android - 为什么手机gps有时会在不同的位置提供相同的坐标
- c# - 两个具有常见布尔问题的 If 语句
- docker - Zeppelin Docker 解释器配置
- javascript - api调用后更新组件的正确方法是什么
- python - 数组排序、切片和反向返回空
- python - 在字符串中查找重复模式