Skip to content

Commit

Permalink
Merge pull request #46 from peterwilsoncc/fix/pre-order-support
Browse files Browse the repository at this point in the history
Improve Pre-order support.
  • Loading branch information
jimjasson authored Oct 16, 2024
2 parents 54ea9bd + d0e1d49 commit dee5f30
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion includes/class-wc-gateway-dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function __construct() {
// Actions.
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_scheduled_subscription_payment_dummy', array( $this, 'process_subscription_payment' ), 10, 2 );
add_action ( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_release_payment' ), 10 );
}

/**
Expand Down Expand Up @@ -136,7 +137,19 @@ public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );

if ( 'success' === $payment_result ) {
$order->payment_complete();
// Handle pre-orders charged upon release.
if (
class_exists( 'WC_Pre_Orders_Order' )
&& WC_Pre_Orders_Order::order_contains_pre_order( $order )
&& WC_Pre_Orders_Order::order_will_be_charged_upon_release( $order )
) {
// Mark order as tokenized (no token is saved for the dummy gateway).
$order->update_meta_data( '_wc_pre_orders_has_payment_token', '1' );
$order->save_meta_data();
WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
} else {
$order->payment_complete();
}

// Remove cart
WC()->cart->empty_cart();
Expand Down Expand Up @@ -169,4 +182,22 @@ public function process_subscription_payment( $amount, $order ) {
$order->update_status( 'failed', __( 'Subscription payment failed. To make a successful payment using Dummy Payments, please review the gateway settings.', 'woocommerce-gateway-dummy' ) );
}
}

/**
* Process pre-order payment upon order release.
*
* Processes the payment for pre-orders charged upon release.
*
* @param WC_Order $order The order object.
*/
public function process_pre_order_release_payment( $order ) {
$payment_result = $this->get_option( 'result' );

if ( 'success' === $payment_result ) {
$order->payment_complete();
} else {
$message = __( 'Order payment failed. To make a successful payment using Dummy Payments, please review the gateway settings.', 'woocommerce-gateway-dummy' );
$order->update_status( 'failed', $message );
}
}
}

0 comments on commit dee5f30

Please sign in to comment.