Skip to content

Commit

Permalink
Fix: Prevent Stock Quantity Update During Product Edit if Unchanged (#…
Browse files Browse the repository at this point in the history
…2344)

* Add hidden field to store original stock value on product inventory page

* Add conditional stock update to prevent unnecessary stock changes in product meta save

* Update composer

* Removed white spaces

* Reverted composer.json

* Created a new composer.lock
  • Loading branch information
brunomendespereira authored Aug 29, 2024
1 parent 07116b1 commit f2e78b6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
58 changes: 29 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions includes/wc-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,12 @@ function ( $value ) {
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
$manage_stock = 'no';
$backorders = 'no';
$stock = '';
$stock_status = wc_clean( $data['_stock_status'] );

if ( 'external' === $product_type ) {
$stock_status = 'instock';
} elseif ( 'variable' === $product_type ) {

// Stock status is always determined by children so sync later
$stock_status = '';

if ( ! empty( $data['_manage_stock'] ) && $data['_manage_stock'] === 'yes' ) {
$manage_stock = 'yes';
$backorders = wc_clean( $data['_backorders'] );
Expand All @@ -306,30 +302,35 @@ function ( $value ) {
$manage_stock = $data['_manage_stock'];
$backorders = wc_clean( $data['_backorders'] );
}

update_post_meta( $post_id, '_manage_stock', $manage_stock );
update_post_meta( $post_id, '_backorders', $backorders );

if ( $stock_status ) {
try {
wc_update_product_stock_status( $post_id, $stock_status );
} catch ( Exception $ex ) {
dokan_log( 'product stock update exception' );
}
}

if ( ! empty( $data['_manage_stock'] ) ) {

// Retrieve original stock value from the hidden field
$original_stock = isset( $data['_original_stock'] ) ? wc_stock_amount( wc_clean( $data['_original_stock'] ) ) : '';
// Clean the current stock value
$stock_amount = isset( $data['_stock'] ) ? wc_clean( $data['_stock'] ) : '';
$stock_amount = 'yes' === $manage_stock ? wc_stock_amount( wp_unslash( $stock_amount ) ) : '';
// Only update the stock amount if it has changed
if ( $original_stock != $stock_amount ) {
if ( 'variable' === $product_type ) {
update_post_meta( $post_id, '_stock', $stock_amount );
} else {
wc_update_product_stock( $post_id, $stock_amount );
}

update_post_meta( $post_id, '_low_stock_amount', $_low_stock_amount );
} else {
update_post_meta( $post_id, '_stock', '' );
update_post_meta( $post_id, '_low_stock_amount', '' );
}

// Update low stock amount regardless of stock changes
$_low_stock_amount = isset( $data['_low_stock_amount'] ) ? wc_clean( $data['_low_stock_amount'] ) : '';
$_low_stock_amount = 'yes' === $manage_stock ? wc_stock_amount( wp_unslash( $_low_stock_amount ) ) : '';
update_post_meta( $post_id, '_low_stock_amount', $_low_stock_amount );
} else {
wc_update_product_stock_status( $post_id, wc_clean( $data['_stock_status'] ) );
}
Expand Down
2 changes: 2 additions & 0 deletions templates/products/inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
<div class="content-half-part">
<label for="_stock" class="form-label"><?php esc_html_e( 'Stock quantity', 'dokan-lite' ); ?></label>
<input type="number" class="dokan-form-control" name="_stock" placeholder="<?php esc_attr__( '1', 'dokan-lite' ); ?>" value="<?php echo esc_attr( wc_stock_amount( $_stock ) ); ?>" min="0" step="1">
<!-- Hidden field to store the original stock value at the time the page loads -->
<input type="hidden" name="_original_stock" value="<?php echo esc_attr( wc_stock_amount( $_stock ) ); ?>">
</div>

<?php if ( version_compare( WC_VERSION, '3.4.7', '>' ) ) : ?>
Expand Down

0 comments on commit f2e78b6

Please sign in to comment.