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 0af4fe66764..c833dbd03b8 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1235,37 +1235,33 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da // Update stock in DB directly. switch ( $operation ) { case 'increase': - // phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery - $wpdb->query( - $wpdb->prepare( - "UPDATE {$wpdb->postmeta} SET meta_value = meta_value + %f WHERE post_id = %d AND meta_key='_stock'", - $stock_quantity, - $product_id_with_stock - ) + $sql = $wpdb->prepare( + "UPDATE {$wpdb->postmeta} SET meta_value = meta_value + %f WHERE post_id = %d AND meta_key='_stock'", + $stock_quantity, + $product_id_with_stock ); break; case 'decrease': - // phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery - $wpdb->query( - $wpdb->prepare( - "UPDATE {$wpdb->postmeta} SET meta_value = meta_value - %f WHERE post_id = %d AND meta_key='_stock'", - $stock_quantity, - $product_id_with_stock - ) + $sql = $wpdb->prepare( + "UPDATE {$wpdb->postmeta} SET meta_value = meta_value - %f WHERE post_id = %d AND meta_key='_stock'", + $stock_quantity, + $product_id_with_stock ); break; default: - // phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery - $wpdb->query( - $wpdb->prepare( - "UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='_stock'", - $stock_quantity, - $product_id_with_stock - ) + $sql = $wpdb->prepare( + "UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='_stock'", + $stock_quantity, + $product_id_with_stock ); break; } + $sql = apply_filters( 'woocommerce_update_product_stock_query', $sql, $product_id_with_stock, $stock_quantity, $operation ); + + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery + $wpdb->query( $sql ); + wp_cache_delete( $product_id_with_stock, 'post_meta' ); }