WC_Post_Data::product_type_changed()
Handle type changes.
Method of the class: WC_Post_Data{}
Hooks from the method
Return
null
. Nothing (null).
Usage
$result = WC_Post_Data::product_type_changed( $product, $from, $to );
- $product(WC_Product) (required)
- Product data.
- $from(string) (required)
- Origin type.
- $to(string) (required)
- New type.
Changelog
Since 3.0.0 | Introduced. |
WC_Post_Data::product_type_changed() WC Post Data::product type changed code WC 9.7.1
public static function product_type_changed( $product, $from, $to ) { /** * Filter to prevent variations from being deleted while switching from a variable product type to a variable product type. * * @since 5.0.0 * * @param bool A boolean value of true will delete the variations. * @param WC_Product $product Product data. * @return string $from Origin type. * @param string $to New type. */ if ( apply_filters( 'woocommerce_delete_variations_on_product_type_change', ProductType::VARIABLE === $from && ProductType::VARIABLE !== $to, $product, $from, $to ) ) { // If the product is no longer variable, we should ensure all variations are removed. $data_store = WC_Data_Store::load( 'product-variable' ); $data_store->delete_variations( $product->get_id(), true ); } }