From ec734071d8a2cc58d6b019d0509852f6cd28f591 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 21 Feb 2020 23:53:05 -0500 Subject: [PATCH] *Hook woocommerce_product_type_changed to ensure product type stays synced #172 --- ...-product-tables-backwards-compatibility.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/includes/class-wc-product-tables-backwards-compatibility.php b/includes/class-wc-product-tables-backwards-compatibility.php index e79ab82..5a9e344 100644 --- a/includes/class-wc-product-tables-backwards-compatibility.php +++ b/includes/class-wc-product-tables-backwards-compatibility.php @@ -28,6 +28,7 @@ public static function hook() { add_filter( 'add_post_metadata', array( __CLASS__, 'add_metadata_to_tables' ), 99, 5 ); add_filter( 'update_post_metadata', array( __CLASS__, 'update_metadata_in_tables' ), 99, 5 ); add_filter( 'delete_post_metadata', array( __CLASS__, 'delete_metadata_from_tables' ), 99, 5 ); + add_filter( 'woocommerce_product_type_changed', array( __CLASS__, 'sync_product_type' ), 10, 3 ); } /** @@ -38,6 +39,7 @@ public static function unhook() { remove_filter( 'add_post_metadata', array( __CLASS__, 'add_metadata_to_tables' ), 99, 5 ); remove_filter( 'update_post_metadata', array( __CLASS__, 'update_metadata_in_tables' ), 99, 5 ); remove_filter( 'delete_post_metadata', array( __CLASS__, 'delete_metadata_from_tables' ), 99, 5 ); + add_filter( 'woocommerce_product_type_changed', array( __CLASS__, 'sync_product_type' ), 10 ); } /** @@ -1643,6 +1645,22 @@ protected static function get_product( $product_id ) { return $product; } + + /** + * @param \WC_Product $product + * @param string $old_type + * @param string $new_type + * + * @return void + */ + public static function sync_product_type( $product, $old_type, $new_type ) { + self::update_in_product_table( array( + 'product_id' => $product->get_id(), + 'column' => 'type', + 'format' => '%s', + 'value' => $new_type, + ) ); + } } WC_Product_Tables_Backwards_Compatibility::hook();