Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add a Product to the Cart and Offer Discount Based on the Chosen Payment Method in WooCommerce?

Whether you’re running promotions or BOGO offers, every touchpoint is crucial for delighting customers with enticing deals. In this post, we’ll explore how to encourage customers to use a preferred payment method by dynamically adjusting the cart contents and also offering a discount based on the chosen payment method.

Solution: Add a Product to the Cart and Offer a Discount Based on the Chosen Payment Method

The code helps to automatically include a product in the cart with a 50% discount applied to the original product price when a specific payment method is chosen.

add_action('woocommerce_before_calculate_totals', 'ts_add_free_product_and_apply_discount', 20, 1);

function ts_add_free_product_and_apply_discount($cart) {
    // Avoid running this function in admin and ensure AJAX compatibility
    if (is_admin() && !defined('DOING_AJAX')) return;

    // Define your targeted payment method
    $targeted_payment_method = 'cheque';
    // Define the free product ID and discount settings
    $free_product_id = 911; // Replace with your actual free product ID
    $discount_percentage = 50; // Replace with your desired discount percentage

    // Get the chosen payment method
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    // Fetch the original price of the free product
    $free_product = wc_get_product($free_product_id);
    if (!$free_product) return; // Exit if the product is not found
    $original_price = $free_product->get_price();

    // Check if the chosen payment method is the targeted one
    if ($targeted_payment_method === $chosen_payment_method) {
        // Check if the free product is not already in the cart
        if (!wc_find_product_in_cart($free_product_id)) {
            // Add the free product to the cart
            WC()->cart->add_to_cart($free_product_id, 1);
        }

        // Apply the discount to the free product and any other relevant products
        foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
            $product_id = $cart_item['product_id'];

            // Apply discount to the free product
            if ($product_id == $free_product_id) {
                // Check if discount has already been applied
                if (!isset($cart_item['discount_applied'])) {
                    $discount_amount = $original_price * $discount_percentage / 100;
                    $new_price = $original_price - $discount_amount;
                    // Set the new price for the product in the cart
                    $cart_item['data']->set_price($new_price);
                    // Mark the discount as applied
                    $cart_item['discount_applied'] = true;
                }
            }
        }
    } else {
        // Revert the discount if the payment method is not targeted
        foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
            $product_id = $cart_item['product_id'];
            if ($product_id == $free_product_id) {
                // Revert the price to the original
                $cart_item['data']->set_price($original_price);
                // Remove the discount applied flag
                unset($cart_item['discount_applied']);
            }
        }
        
        // Check if the free product is in the cart and remove it if necessary
        $cart_item_key = wc_find_product_in_cart($free_product_id);
        if ($cart_item_key) {
            WC()->cart->remove_cart_item($cart_item_key);
        }
    }
}

// Helper function to find a product in the cart
function wc_find_product_in_cart($product_id) {
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        if ($product_id == $cart_item['product_id']) {
            return $cart_item_key;
        }
    }
    return false;
}

// Ensure that payment method change triggers a recalculation of the checkout
add_action('woocommerce_review_order_before_payment', 'ts_refresh_payment_methods');

function ts_refresh_payment_methods() {
    ?>
    <script type="text/javascript">
    (function ($) {
        $(document.body).on('change', 'input[name^="payment_method"]', function () {
            $('body').trigger('update_checkout');
        });
    })(jQuery);
    </script>
    <?php
}
flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.


Output

When the customer proceeds to the checkout page and selects the pre-defined payment method ‘Cheque’, the specified product in the code will be added to the cart with a 50% discount applied to its original price.

adding a product and offering discounts based on payment method in WooCommerce

To unlock new dimensions of customer satistisfaction you need to go beyond the standard BOGO offers. Discover how BOGO offers can also be implemented based on ‘N’ number of conditions that will help you to automatically add a product to WooCommerce cart.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x