Skip to content

Commit

Permalink
Fix Shortcode Orders When Using WooPay Direct Checkout (#8802)
Browse files Browse the repository at this point in the history
Co-authored-by: Kristófer Reykjalín <13835680+reykjalin@users.noreply.github.com>
  • Loading branch information
lovo-h and reykjalin committed May 10, 2024
1 parent 4a78d37 commit 6f110cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-8775-direct-checkout-shortcode-orders
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix shortcode orders when using WooPay Direct Checkout.
21 changes: 18 additions & 3 deletions includes/class-wc-payments-woopay-direct-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function init() {
/**
* This filter is used to ensure the session's store_api_draft_order is used, if it exists.
* This prevents a bug where the store_api_draft_order is not used and instead, a new
* order_awaiting_payment is created during the checkout request.
* order_awaiting_payment is created during the checkout request. The bug being evident
* if a product had one remaining stock and the store_api_draft_order was reserving it,
* an order would fail to be placed since when order_awaiting_payment is created, it would
* not be able to reserve the one stock.
*
* @param int $order_id The order ID being used.
* @return int|mixed The new order ID to use.
Expand All @@ -39,11 +42,23 @@ public function maybe_use_store_api_draft_order_id( $order_id ) {
$is_already_defined_order_id = ! empty( $order_id );
// Only apply this filter if the session doesn't already have an order_awaiting_payment.
$is_order_awaiting_payment = isset( WC()->session->order_awaiting_payment );
if ( ! $is_checkout || $is_already_defined_order_id || $is_order_awaiting_payment ) {
// Only apply this filter if draft order ID exists.
$has_draft_order = ! empty( WC()->session->get( 'store_api_draft_order' ) );
if ( ! $is_checkout || $is_already_defined_order_id || $is_order_awaiting_payment || ! $has_draft_order ) {
return $order_id;
}

return absint( WC()->session->get( 'store_api_draft_order', $order_id ) );
$draft_order_id = absint( WC()->session->get( 'store_api_draft_order' ) );
// Set the order status to "pending" payment, so that it can be resumed.
$draft_order = wc_get_order( $draft_order_id );
$draft_order->set_status( 'pending' );
$draft_order->save();

// Move $draft_order_id in session, from store_api_draft_order to order_awaiting_payment.
WC()->session->set( 'store_api_draft_order', null );
WC()->session->set( 'order_awaiting_payment', $draft_order_id );

return $order_id;
}

/**
Expand Down

0 comments on commit 6f110cb

Please sign in to comment.