Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/b63d575a2d5a1b15befa2444290c372a to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/b63d575a2d5a1b15befa2444290c372a to your computer and use it in GitHub Desktop.
WFOCU — Block checkout during active upsell session
<?php
/**
* Plugin Name: WFOCU — Block checkout during active upsell session (snippet of PR #2180)
* Description: Standalone copy of woofunnels-upstroke-one-click-upsell PR #2180. Blocks a new checkout submission while the customer's upsell session is still active (e.g. browser Back after paying the primary order), and normalizes wfocu-pri-order status on the thank-you page. Remove once the fix ships in the plugin.
* Version: 1.0.0
*/
defined( 'ABSPATH' ) || exit;
add_action( 'init', function () {
if ( ! function_exists( 'WFOCU_Core' ) || is_null( WFOCU_Core()->data ) ) {
return;
}
// Skip when the installed plugin already ships the fix — avoids duplicate notices.
if ( isset( WFOCU_Core()->public ) && is_object( WFOCU_Core()->public ) && method_exists( WFOCU_Core()->public, 'maybe_block_checkout_on_active_upsell_session' ) ) {
return;
}
/**
* Block checkout submission if the customer already has an active upsell session,
* i.e. they placed a primary order and went back to checkout via browser back button.
*/
add_action( 'woocommerce_checkout_process', function () {
if ( ! method_exists( WFOCU_Core()->data, 'has_valid_session' ) || ! WFOCU_Core()->data->has_valid_session() ) {
return;
}
$order = WFOCU_Core()->data->get_current_order();
if ( ! $order instanceof WC_Order ) {
return;
}
$order_link = sprintf(
'<a href="%s" style="text-decoration:underline;">%s</a>',
esc_url( $order->get_checkout_order_received_url() ),
__( 'View your order', 'woofunnels-upstroke-one-click-upsell' )
);
wc_add_notice(
apply_filters(
'wfocu_active_session_checkout_block_message',
sprintf(
/* translators: 1: order number 2: view-order link */
__( 'Your previous order (#%1$s) is currently being processed. %2$s before placing a new order.', 'woofunnels-upstroke-one-click-upsell' ),
$order->get_order_number(),
$order_link
)
),
'error'
);
}, 5 );
/**
* Normalize a primary order stuck in wfocu-pri-order status when the customer
* reaches the thank-you page (runs before WFOCU's own maybe_destroy_session).
*/
add_action( 'woocommerce_thankyou', function ( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order || 'wfocu-pri-order' !== $order->get_status() ) {
return;
}
if ( ! isset( WFOCU_Core()->orders ) || ! method_exists( WFOCU_Core()->orders, 'maybe_normalize_order_statuses' ) ) {
return;
}
$funnel_id = absint( $order->get_meta( '_wfocu_funnel_id' ) );
if ( $funnel_id > 0 ) {
WFOCU_Core()->orders->maybe_normalize_order_statuses( $funnel_id, $order_id );
}
}, 5, 1 );
}, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment