Updated filter name to woocommerce_hold_stock_for_checkout

Also fixed coding standards
This commit is contained in:
Claudio Sanches 2019-12-03 16:38:17 -03:00
parent 8b64471f3a
commit 988b72e461
5 changed files with 60 additions and 34 deletions

View File

@ -2048,11 +2048,19 @@ class WC_Order extends WC_Abstract_Order {
/**
* Adds a '_held_for_checkout` record for all products in cart.
*
* @since 3.9.0
* @param WC_Cart $cart Cart instance.
* @throws Exception When unable to hold stock for checkout.
*/
public function hold_stock_for_checkout( $cart ) {
if ( ! apply_filters( 'enable_hold_stock_3_9', true ) ) {
/**
* Filter: woocommerce_hold_stock_for_checkout
* Allows enable/disable hold stock functionality on checkout.
*
* @since 3.9.0
* @param bool $enabled Default to true.
*/
if ( ! apply_filters( 'woocommerce_hold_stock_for_checkout', true ) ) {
return;
}
@ -2067,11 +2075,11 @@ class WC_Order extends WC_Abstract_Order {
$product_qty_in_cart = $cart->get_cart_item_quantities();
$stock_held_keys = array();
$error = null;
$error = null;
try {
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$product = wc_get_product( $values['data'] );
$product = wc_get_product( $values['data'] );
if ( ! $product ) {
// Unsupported product!
continue;
@ -2102,9 +2110,9 @@ class WC_Order extends WC_Abstract_Order {
/**
* Adds a `_held_for_checkout` record for a product in checkout.
*
* @since 3.9.0
* @param WC_Product $product Instance of product.
* @param int $quantity Quantity of product to hold.
*
* @return bool|string|null Returns `false` when unable to hold stock, meta key when stock was held successfully, `null` when holding stock is not needed.
*/
protected function hold_product_for_checkout( $product, $quantity ) {
@ -2122,25 +2130,29 @@ class WC_Order extends WC_Abstract_Order {
$query_for_held_stock = $product_data_store->get_query_for_held_stock( $product_id );
$query_for_stock = $product_data_store->get_query_for_stock( $product_id );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$insert_statement = $wpdb->prepare(
"
INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value )
SELECT %d, %s, %d from DUAL
WHERE ( $query_for_stock ) - ( $query_for_held_stock ) >= %d
",
INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value )
SELECT %d, %s, %d from DUAL
WHERE ( $query_for_stock ) - ( $query_for_held_stock ) >= %d
",
$product->get_stock_managed_by_id(),
$held_key,
$quantity,
$quantity
); // WPCS: unprepared SQL ok.
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$result = $wpdb->query( $insert_statement ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$result = $wpdb->query( $insert_statement ); // WPCS: unprepared SQL ok.
return $result > 0 ? $held_key : false;
}
/**
* Save keys used to held stock to DB.
*
* @since 3.9.0
* @param array $keys Array of keys to save.
*/
public function record_held_stock( $keys ) {
@ -2151,6 +2163,8 @@ class WC_Order extends WC_Abstract_Order {
/**
* Releases held stock, also deletes keys for the order.
*
* @since 3.9.0
*/
public function release_held_stock() {
$stock_held_keys = $this->get_meta( '_stock_held_keys' );

View File

@ -879,8 +879,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$outofstock_where = ' AND exclude_join.object_id IS NULL';
}
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return $wpdb->get_results(
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
"
SELECT posts.ID as id, posts.post_parent as parent_id
FROM {$wpdb->posts} AS posts
@ -898,7 +898,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
)
GROUP BY posts.ID
"
); // WPCS: unprepared SQL ok.
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
/**
@ -1594,7 +1595,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
foreach ( $search_terms as $search_term ) {
$like = '%' . $wpdb->esc_like( $search_term ) . '%';
$term_group_query .= $wpdb->prepare( " {$searchand} ( ( posts.post_title LIKE %s) OR ( posts.post_excerpt LIKE %s) OR ( posts.post_content LIKE %s ) OR ( wc_product_meta_lookup.sku LIKE %s ) )", $like, $like, $like, $like ); // WPCS: unprepared SQL ok.
$term_group_query .= $wpdb->prepare( " {$searchand} ( ( posts.post_title LIKE %s) OR ( posts.post_excerpt LIKE %s) OR ( posts.post_content LIKE %s ) OR ( wc_product_meta_lookup.sku LIKE %s ) )", $like, $like, $like, $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$searchand = ' AND ';
}
@ -2055,8 +2056,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
/**
* Returns query statement for getting current `_stock` of a product.
*
* @since 3.9.0
* @param int $product_id Product ID.
*
* @return string|void Query statement.
*/
public function get_query_for_stock( $product_id ) {
@ -2076,8 +2077,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
/**
* Returns query statement for getting quantity of stock held by orders in checkout.
*
* @since 3.9.0
* @param int $product_id Product ID.
*
* @return string|void Query statement.
*/
public function get_query_for_held_stock( $product_id ) {

View File

@ -886,6 +886,7 @@ function wc_update_coupon_usage_counts( $order_id ) {
/**
* Release held stock if any for an order.
*
* @since 3.9.0
* @param int $order_id Order ID.
*/
function wc_release_held_stock( $order_id ) {

View File

@ -302,7 +302,14 @@ function wc_increase_stock_levels( $order_id ) {
function wc_get_held_stock_quantity( $product, $exclude_order_id = 0 ) {
global $wpdb;
if ( apply_filters( 'enable_hold_stock_3_9', true ) ) {
/**
* Filter: woocommerce_hold_stock_for_checkout
* Allows enable/disable hold stock functionality on checkout.
*
* @since 3.9.0
* @param bool $enabled Default to true.
*/
if ( ! apply_filters( 'woocommerce_hold_stock_for_checkout', true ) ) {
/**
* DOES NOT SUPPORT `exclude_order_id` param, which was primarily being used to exclude the current order!
* While creating an order during checkout flow, this logic relies on the fact that order is not yet saved and therefore will be excluded.
@ -313,32 +320,35 @@ function wc_get_held_stock_quantity( $product, $exclude_order_id = 0 ) {
* @since 3.9.0
*/
$product_data_store = WC_Data_Store::load( 'product' );
$product_id = $product->get_stock_managed_by_id();
return $wpdb->get_var( $product_data_store->get_query_for_held_stock( $product_id ) ); // WPCS: unprepared SQL ok.
$product_id = $product->get_stock_managed_by_id();
return $wpdb->get_var( $product_data_store->get_query_for_held_stock( $product_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
return $wpdb->get_var(
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$wpdb->prepare(
"
SELECT SUM( order_item_meta.meta_value ) AS held_qty
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} as postmeta ON posts.ID = postmeta.post_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id
WHERE order_item_meta.meta_key = '_qty'
AND order_item_meta2.meta_key = %s
AND order_item_meta2.meta_value = %d
AND postmeta.meta_key = '_created_via'
AND postmeta.meta_value = 'checkout'
AND posts.post_type IN ( '" . implode( "','", wc_get_order_types() ) . "' )
AND posts.post_status = 'wc-pending'
AND posts.ID != %d;",
SELECT SUM( order_item_meta.meta_value ) AS held_qty
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} as postmeta ON posts.ID = postmeta.post_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id
WHERE order_item_meta.meta_key = '_qty'
AND order_item_meta2.meta_key = %s
AND order_item_meta2.meta_value = %d
AND postmeta.meta_key = '_created_via'
AND postmeta.meta_value = 'checkout'
AND posts.post_type IN ( '" . implode( "','", wc_get_order_types() ) . "' )
AND posts.post_status = 'wc-pending'
AND posts.ID != %d;
",
'product_variation' === get_post_type( $product->get_stock_managed_by_id() ) ? '_variation_id' : '_product_id',
$product->get_stock_managed_by_id(),
$exclude_order_id
)
); // WPCS: unprepared SQL ok.
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
);
}
/**

View File

@ -77,7 +77,7 @@ class WC_Tests_Checkout extends WC_Unit_Test_Case {
* @throws Exception When unable to create order.
*/
public function test_create_order_when_out_of_stock_legacy() {
add_filter( 'enable_hold_stock_3_9', '__return_false' );
add_filter( 'woocommerce_hold_stock_for_checkout', '__return_false' );
$this->test_create_order_when_out_of_stock();
}