Advanced Dynamic Pricing For WooCommerce

Don’t allow to switch payment method for unpaid order

The Advanced Dynamic Pricing for WooCommerce plugin lets you build powerful discount rules based on the customer’s chosen payment method – for example, offering a 5% discount for payments made via PayPal or credit card. However, WooCommerce itself allows customers to change the payment method for an existing unpaid order. This opens a loophole: a customer could initially attempt a discount‑eligible payment method (e.g., a credit card), let the transaction fail (or intentionally cancel it), and then switch to a non‑eligible method such as cash on delivery – while still receiving the original discount. The discount had already been applied during the first checkout attempt, and WooCommerce does not automatically recalculate the order when the payment method is changed.

The code snippet below closes this loophole. It locks the payment method on the Pay for Order page, forcing the customer to stick with the gateway that was originally selected. Consequently, the discount rules are enforced exactly as intended.


The Complete Code Snippet

What the code does:

Line / ElementExplanation
add_action( 'before_woocommerce_pay', ... )Attaches the custom function to the WooCommerce action that runs just before the Pay for Order page is loaded.
$order_key = isset( $_GET['key'] ) ? $_GET['key'] : null;Retrieves the order key from the URL query parameter. This key uniquely identifies the order on the payment page.
$order_id = wc_get_order_id_by_order_key( $order_key );Converts the order key into the numeric order ID.
$order = wc_get_order( $order_id );Fetches the full order object using the order ID.
$payment_method = $order->get_payment_method();Retrieves the payment method that was originally selected for the order (e.g. 'paypal''stripe''cod').
add_filter( 'woocommerce_available_payment_gateways', ... )Applies a filter that modifies the list of payment gateways available on the Pay for Order page. The closure captures the original $payment_methodand returns an array containing only that gateway, effectively hiding all others.

Important note: The discount itself is not recalculated after the payment method is locked. This code simply prevents the customer from seeing or selecting a different gateway on the Pay for Order page, thereby avoiding the loophole. For a more thorough re‑evaluation of discounts, a more complex server‑side solution would be required (see the FAQ section).


Step‑by‑Step Implementation

Follow these steps to add the custom code to your WordPress site.

Step 1: Choose where to add the code

You have two safe options:

  • Option A (Recommended): Use the free Code Snippets plugin.
    This is the safest method. Install the plugin from Plugins → Add New by searching for “Code Snippets”. It allows you to add, activate, and deactivate code snippets without ever editing your theme files.
  • Option B: Add to your child theme’s functions.php file.
    If you are comfortable editing theme files, add the code to your active child theme’s functions.php file. Never add custom code directly to a parent theme, because it will be lost when the theme is updated.

Step 2: Insert the snippet

  • If using Code Snippets:
  • If editing functions.php:

Step 3: Test the functionality

  1. Create a test discount rule that applies only when a specific payment method is selected (e.g., 5% off for “Credit Card” or “PayPal”).
  2. As a customer, go through the checkout process and select the discount‑eligible payment method.
  3. Complete the order up to the point where the order is placed but not yet paid (i.e., the order status is “Pending payment”).
  4. On the Order received page, click the Pay for order button.
  5. On the Pay for Order page, verify that only the original payment method is available. All other gateways should be hidden.
  6. Attempt to change the payment method – you should not see any option to do so.

If the Pay for Order page shows only the original payment method, the implementation is successful.


Customisation Options

The code snippet can be easily adapted for different requirements.

Allow switching only to a specific gateway

If you want to permit switching to a particular alternative payment method (e.g., from a failed credit card to bank transfer), modify the filter to return an array containing the original method plus any allowed alternatives:

Add a notice when the payment method is locked

To inform the customer why they cannot change the payment method, add a WooCommerce notice:

Apply the restriction only for specific discount rules

You may want to lock the payment method only when the order contains a discount from a particular rule. To achieve that, check for the presence of a specific coupon code or meta key associated with your dynamic pricing rule:

Redirect to cart instead of showing a locked Pay page

If you prefer to prevent access to the Pay for Order page altogether when a discount has been applied, redirect the customer back to the cart:

Have questions? Please submit a support request. We're always happy to help!

Advanced Dynamic Pricing
for WooCommerce
PRO

From $60
Buy
✓ 30-day money-back guarantee