From f2e78b610b3a88826809b9b51f1868eb2e687969 Mon Sep 17 00:00:00 2001 From: Bruno Mendes <88600930+brunomendespereira@users.noreply.github.com> Date: Thu, 29 Aug 2024 00:24:38 -0300 Subject: [PATCH] Fix: Prevent Stock Quantity Update During Product Edit if Unchanged (#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 --- composer.lock | 58 ++++++++++++++++---------------- includes/wc-functions.php | 27 ++++++++------- templates/products/inventory.php | 2 ++ 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/composer.lock b/composer.lock index 66378b1099..451df861a4 100644 --- a/composer.lock +++ b/composer.lock @@ -62,16 +62,16 @@ }, { "name": "appsero/updater", - "version": "v2.0.0", + "version": "v2.2.0", "source": { "type": "git", "url": "https://github.com/Appsero/updater.git", - "reference": "ddc394439c58d27835a6bfd0564e138f2c8ed106" + "reference": "58be2387dea35ffbca41970186d2251a00b2c44a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Appsero/updater/zipball/ddc394439c58d27835a6bfd0564e138f2c8ed106", - "reference": "ddc394439c58d27835a6bfd0564e138f2c8ed106", + "url": "https://api.github.com/repos/Appsero/updater/zipball/58be2387dea35ffbca41970186d2251a00b2c44a", + "reference": "58be2387dea35ffbca41970186d2251a00b2c44a", "shasum": "" }, "type": "library", @@ -93,9 +93,9 @@ "description": "Appsero client updater module", "support": { "issues": "https://github.com/Appsero/updater/issues", - "source": "https://github.com/Appsero/updater/tree/v2.0.0" + "source": "https://github.com/Appsero/updater/tree/v2.2.0" }, - "time": "2024-08-01T08:15:57+00:00" + "time": "2024-08-22T09:23:04+00:00" }, { "name": "jakeasmith/http_build_url", @@ -1062,12 +1062,12 @@ "source": { "type": "git", "url": "https://github.com/PHPCSStandards/PHPCSUtils.git", - "reference": "8a1f3f533b9e359f4438c7c7dd46bb35a6ffcb89" + "reference": "cc4244b4e6ab34f3e8db83e3987dcb65138450ff" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/8a1f3f533b9e359f4438c7c7dd46bb35a6ffcb89", - "reference": "8a1f3f533b9e359f4438c7c7dd46bb35a6ffcb89", + "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/cc4244b4e6ab34f3e8db83e3987dcb65138450ff", + "reference": "cc4244b4e6ab34f3e8db83e3987dcb65138450ff", "shasum": "" }, "require": { @@ -1143,7 +1143,7 @@ "type": "open_collective" } ], - "time": "2024-08-19T08:51:38+00:00" + "time": "2024-08-23T23:59:03+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1151,28 +1151,28 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "328a747f499cca790acff5634a4e55b957f40634" + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/328a747f499cca790acff5634a4e55b957f40634", - "reference": "328a747f499cca790acff5634a4e55b957f40634", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/85402a822d1ecf1db1096959413d35e1c37cf1a5", + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.18 || ^5.0", + "nikic/php-parser": "^4.19.1 || ^5.1.0", "php": ">=7.3", - "phpunit/php-file-iterator": "^3.0.3", - "phpunit/php-text-template": "^2.0.2", - "sebastian/code-unit-reverse-lookup": "^2.0.2", - "sebastian/complexity": "^2.0", - "sebastian/environment": "^5.1.2", - "sebastian/lines-of-code": "^1.0.3", - "sebastian/version": "^3.0.1", - "theseer/tokenizer": "^1.2.0" + "phpunit/php-file-iterator": "^3.0.6", + "phpunit/php-text-template": "^2.0.4", + "sebastian/code-unit-reverse-lookup": "^2.0.3", + "sebastian/complexity": "^2.0.3", + "sebastian/environment": "^5.1.5", + "sebastian/lines-of-code": "^1.0.4", + "sebastian/version": "^3.0.2", + "theseer/tokenizer": "^1.2.3" }, "require-dev": { "phpunit/phpunit": "^9.6" @@ -1213,7 +1213,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.32" }, "funding": [ { @@ -1221,7 +1221,7 @@ "type": "github" } ], - "time": "2024-07-17T05:19:21+00:00" + "time": "2024-08-22T04:23:01+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1470,12 +1470,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "846c7cefad8824f281f521b9625dd301064ead88" + "reference": "f4148269d95dc62f50cf5b432c282f4a3fda81d4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/846c7cefad8824f281f521b9625dd301064ead88", - "reference": "846c7cefad8824f281f521b9625dd301064ead88", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f4148269d95dc62f50cf5b432c282f4a3fda81d4", + "reference": "f4148269d95dc62f50cf5b432c282f4a3fda81d4", "shasum": "" }, "require": { @@ -1490,7 +1490,7 @@ "phar-io/manifest": "^2.0.4", "phar-io/version": "^3.2.1", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.31", + "phpunit/php-code-coverage": "^9.2.32", "phpunit/php-file-iterator": "^3.0.6", "phpunit/php-invoker": "^3.1.1", "phpunit/php-text-template": "^2.0.4", @@ -1565,7 +1565,7 @@ "type": "tidelift" } ], - "time": "2024-08-14T08:16:04+00:00" + "time": "2024-08-22T04:27:14+00:00" }, { "name": "sebastian/cli-parser", diff --git a/includes/wc-functions.php b/includes/wc-functions.php index 736bf30831..37093fb13c 100755 --- a/includes/wc-functions.php +++ b/includes/wc-functions.php @@ -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'] ); @@ -306,10 +302,9 @@ 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 ); @@ -317,19 +312,25 @@ function ( $value ) { 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'] ) ); } diff --git a/templates/products/inventory.php b/templates/products/inventory.php index ec3c665baf..1e4741a687 100644 --- a/templates/products/inventory.php +++ b/templates/products/inventory.php @@ -46,6 +46,8 @@