Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter hook to allow currently hardcoded 10 minute checkout draft stock reservation to be filtered #45246

Merged
merged 16 commits into from Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
7cb521e
Update Checkout.php
ninetyninew Mar 1, 2024
3757991
Add changefile(s) from automation for the following project(s): wooco…
invalid-email-address Mar 1, 2024
f58704c
Add filter hook for draft order stock hold (#45245)
ninetyninew Mar 1, 2024
950f8c9
Merge branch 'trunk' of https://github.com/ninetyninew/woocommerce in…
ninetyninew Mar 1, 2024
3a62305
Amending filter hook in changelog for draft order stock hold changes …
ninetyninew Mar 3, 2024
96e4fe8
Add changefile(s) from automation for the following project(s): wooco…
invalid-email-address Mar 3, 2024
a219a59
Add changefile(s) from automation for the following project(s): wooco…
invalid-email-address Mar 3, 2024
a4eab12
Add (int) to hook for draft order stock hold (#45245)
ninetyninew Mar 4, 2024
10c87ba
Fixing codesniff lint issues found on previous commits (#45245)
ninetyninew Mar 5, 2024
c361bfa
Adding negative check to draft order hold stock minutes (#45245)
ninetyninew Mar 6, 2024
9291ff0
Discarding older changes in favour of PR recommendations (#45245)
ninetyninew Mar 7, 2024
c131ab9
Add changefile(s) from automation for the following project(s): wooco…
invalid-email-address Mar 7, 2024
8df29af
Amends of woocommerce_order_hold_stock_minutes in favour of PR recomm…
ninetyninew Mar 9, 2024
35ba0bf
Add changefile(s) from automation for the following project(s): wooco…
invalid-email-address Mar 9, 2024
204180b
Amending woocommerce_order_hold_stock_minutes filter hook doc block c…
ninetyninew Mar 11, 2024
ed6c285
Merge branch 'trunk' of https://github.com/ninetyninew/woocommerce in…
ninetyninew Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/45246-trunk
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add `woocommerce_draft_order_hold_stock_minutes` filter hook to allow duration of stock reservation to hold stock for draft orders on checkout entry
16 changes: 15 additions & 1 deletion plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php
@@ -1,4 +1,4 @@
<?php

Check notice on line 1 in plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php

View workflow job for this annotation

GitHub Actions / Analyze Branch Changes

new filter found - woocommerce_draft_order_hold_stock_minutes

\'woocommerce_draft_order_hold_stock_minutes\' introduced in 8.8.0
namespace Automattic\WooCommerce\StoreApi\Routes\V1;

use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;
Expand Down Expand Up @@ -391,9 +391,23 @@
* If POSTing to the checkout (attempting to pay), set the timeout to 60 mins (using the woocommerce_hold_stock_minutes option).
*/
try {

$reserve_stock = new ReserveStock();
$duration = $request->get_method() === 'POST' ? (int) get_option( 'woocommerce_hold_stock_minutes', 60 ) : 10;

/**
* Filters the hold stock duration in minutes for draft orders on checkout entry.
*
* This hook filters the duration in minutes that stock is held for draft orders on checkout entry, it allows a third party to amend the duration to increase/reduce the time the stock is held for draft orders on checkout entry.
*
* @since 8.8.0
*
* @param integer $minutes Minutes to hold stock for draft orders on checkout entry.
*/
$draft_order_hold_stock_minutes = (int) apply_filters( 'woocommerce_draft_order_hold_stock_minutes', 10 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should check that the result is: not empty, is an integer (or can be cast to one) and is not negative, if any of these are true it should default back to 10. Do you agree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@opr I think casting to a integer and a check for negative is fine, however the !empty would stop being able to filter to 0, which for our particular scenario (to stop stock reservation) is why we need this filter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you're right 👍🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@opr The int casting is already there, i've just amended to include the negative check in the latest commit if you'd like to check over it.


$duration = $request->get_method() === 'POST' ? (int) get_option( 'woocommerce_hold_stock_minutes', 60 ) : $draft_order_hold_stock_minutes;
$reserve_stock->reserve_stock_for_order( $this->order, $duration );

} catch ( ReserveStockException $e ) {
throw new RouteException(
$e->getErrorCode(),
Expand Down