diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 54246268354..4c240c66e99 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -839,7 +839,7 @@ CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup ( KEY `virtual` (`virtual`), KEY `downloadable` (`downloadable`), KEY `stock_status` (`stock_status`), - KEY `stock_status` (`stock_quantity`), + KEY `stock_quantity` (`stock_quantity`), KEY min_max_price (`min_price`, `max_price`) ) $collate; "; diff --git a/includes/class-wc-post-data.php b/includes/class-wc-post-data.php index d7412018c1c..ba0ad454ff8 100644 --- a/includes/class-wc-post-data.php +++ b/includes/class-wc-post-data.php @@ -290,6 +290,7 @@ class WC_Post_Data { case 'product': $data_store = WC_Data_Store::load( 'product-variable' ); $data_store->delete_variations( $id, true ); + $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' ); $parent_id = wp_get_post_parent_id( $id ); if ( $parent_id ) { @@ -297,6 +298,8 @@ class WC_Post_Data { } break; case 'product_variation': + $data_store = WC_Data_Store::load( 'product' ); + $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' ); wc_delete_product_transients( wp_get_post_parent_id( $id ) ); break; case 'shop_order': diff --git a/includes/data-stores/class-wc-data-store-wp.php b/includes/data-stores/class-wc-data-store-wp.php index 11d102bafaa..d5a11c8a576 100644 --- a/includes/data-stores/class-wc-data-store-wp.php +++ b/includes/data-stores/class-wc-data-store-wp.php @@ -512,6 +512,29 @@ class WC_Data_Store_WP { return apply_filters( 'wp_search_stopwords', $stopwords ); } + /** + * Get data to save to a lookup table. + * + * @since 3.6.0 + * @param int $id ID of object to update. + * @param string $table Lookup table name. + * @return array + */ + protected function get_data_for_lookup_table( $id, $table ) { + return array(); + } + + /** + * Get primary key name for lookup table. + * + * @since 3.6.0 + * @param string $table Lookup table name. + * @return string + */ + protected function get_primary_key_for_lookup_table( $table ) { + return ''; + } + /** * Update a lookup table for an object. * @@ -540,4 +563,32 @@ class WC_Data_Store_WP { wp_cache_set( 'lookup_table', $update_data, 'object_' . $id ); } } + + /** + * Delete lookup table data for an ID. + * + * @since 3.6.0 + * @param int $id ID of object to update. + * @param string $table Lookup table name. + */ + public function delete_from_lookup_table( $id, $table ) { + global $wpdb; + + $id = absint( $id ); + $table = sanitize_key( $table ); + + if ( empty( $id ) || empty( $table ) ) { + return false; + } + + $pk = $this->get_primary_key_for_lookup_table( $table ); + + $wpdb->delete( + $wpdb->$table, + array( + $pk => $id, + ) + ); + wp_cache_delete( 'lookup_table', 'object_' . $id ); + } } diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index 755d71c63a0..738f57df91a 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -624,7 +624,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da } } - if ( array_intersect( $this->updated_props, array( 'sku', 'regular_price', 'sale_price', 'date_on_sale_from', 'date_on_sale_to', 'total_sales', 'average_rating', 'stock_quantity', 'manage_stock' ) ) ) { + if ( array_intersect( $this->updated_props, array( 'sku', 'regular_price', 'sale_price', 'date_on_sale_from', 'date_on_sale_to', 'total_sales', 'average_rating', 'stock_quantity', 'stock_status', 'manage_stock', 'downloadable', 'virtual' ) ) ) { $this->update_lookup_table( $product->get_id(), 'wc_product_meta_lookup' ); } @@ -1894,4 +1894,18 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da } return array(); } + + /** + * Get primary key name for lookup table. + * + * @since 3.6.0 + * @param string $table Lookup table name. + * @return string + */ + protected function get_primary_key_for_lookup_table( $table ) { + if ( 'wc_product_meta_lookup' === $table ) { + return 'product_id'; + } + return ''; + } }