diff --git a/includes/class-wc-product-tables-backwards-compatibility.php b/includes/class-wc-product-tables-backwards-compatibility.php index e79ab82..31ca6e5 100644 --- a/includes/class-wc-product-tables-backwards-compatibility.php +++ b/includes/class-wc-product-tables-backwards-compatibility.php @@ -52,6 +52,10 @@ public static function unhook() { public static function get_metadata_from_tables( $result, $post_id, $meta_key, $single ) { $mapping = self::get_mapping(); + if ( ! self::is_product( $post_id ) ) { + return $result; + } + if ( ! isset( $mapping[ $meta_key ] ) ) { return $result; } @@ -86,7 +90,7 @@ public static function get_metadata_from_tables( $result, $post_id, $meta_key, $ public static function add_metadata_to_tables( $result, $post_id, $meta_key, $meta_value, $unique ) { $mapping = self::get_mapping(); - if ( ! isset( $mapping[ $meta_key ] ) ) { + if ( ! self::is_product( $post_id ) || ! isset( $mapping[ $meta_key ] ) ) { return $result; } @@ -118,7 +122,7 @@ public static function add_metadata_to_tables( $result, $post_id, $meta_key, $me public static function update_metadata_in_tables( $result, $post_id, $meta_key, $meta_value, $prev_value ) { $mapping = self::get_mapping(); - if ( ! isset( $mapping[ $meta_key ] ) ) { + if ( ! self::is_product( $post_id ) || ! isset( $mapping[ $meta_key ] ) ) { return $result; } @@ -144,7 +148,7 @@ public static function update_metadata_in_tables( $result, $post_id, $meta_key, public static function delete_metadata_from_tables( $result, $post_id, $meta_key, $prev_value, $delete_all ) { $mapping = self::get_mapping(); - if ( ! isset( $mapping[ $meta_key ] ) ) { + if ( ! self::is_product( $post_id ) || ! isset( $mapping[ $meta_key ] ) ) { return $result; } @@ -1643,6 +1647,20 @@ protected static function get_product( $product_id ) { return $product; } + + /** + * Helper method to ensure we are dealing with a product + * + * @param int $post_id Product ID. + * @return bool + */ + private static function is_product( $post_id ) { + if ( 'product' === get_post_type( $post_id ) && WC_Product_Factory::get_product_type( $post_id ) ) { + return true; + } + + return false; + } } WC_Product_Tables_Backwards_Compatibility::hook();