在 WooCommerce 3+ 中更改现有产品的产品类型
Change the product type of an existing product in 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()
,由于多种原因不能正常工作,请参阅 this older related thread。新产品将保留其所有现有数据。
我需要在创建后使用我的子主题中的功能更改 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()
,由于多种原因不能正常工作,请参阅 this older related thread。新产品将保留其所有现有数据。