Skip to content

Commit

Permalink
Prevent price update/race condition while updating product unit price…
Browse files Browse the repository at this point in the history
… data within the before_product_update hook.
  • Loading branch information
dennisnissle committed Nov 2, 2023
1 parent 9d9e1b4 commit 0551934
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions includes/wc-gzd-product-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ function wc_gzd_recalculate_unit_price( $args = array(), $product = false ) {
)
);

/**
* WooCommerce updates the product price after triggering the before_product_save hook.
* Make sure to use the current price based on regular/sale price if discrepancies detected.
*/
if ( $default_args['price'] !== $default_args['regular_price'] && $default_args['price'] !== $default_args['sale_price'] ) {
if ( $product->is_on_sale() ) {
$default_args['price'] = $default_args['sale_price'];
} else {
$default_args['price'] = $default_args['regular_price'];
}
}

if ( isset( $default_args['tax_mode'] ) && 'incl' === $default_args['tax_mode'] ) {
$default_args['regular_price'] = wc_get_price_including_tax( $wc_product, array( 'price' => $default_args['regular_price'] ) );
$default_args['sale_price'] = wc_get_price_including_tax( $wc_product, array( 'price' => $default_args['sale_price'] ) );
Expand Down

0 comments on commit 0551934

Please sign in to comment.