Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions includes/class-wc-product-tables-backwards-compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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();