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

How to Add BOGO (Buy One Get One) Offer for the Chosen Shipping Method in WooCommerce?

It is crucial to provide different BOGO “Buy one Get Offers” based on different conditions to optimize your promotional marketing strategy. Wwith such promotions, you can encourage your customers to link BOGO deals to shipping methods to avoid the stumbling block of shipping costs. This guide helps you to tie a BOGO offer such as providing free gifts to specific shipping methods so that online store owners can influence customers to use a specified shipping method.

Solution: Add BOGO (Buy One Get One) Offer for the Chosen Shipping Method in WooCommerce

The code snippet given below will help to offer a free gift when the specified shipping methods defined in the array is selected by the customers.

add_action('woocommerce_cart_calculate_fees', 'ts_add_free_product_based_on_shipping', 20, 1);

function ts_add_free_product_based_on_shipping($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    $chosen_shipping_method_id = WC()->session->get('chosen_shipping_methods')[0];
    $chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];

    // Set your free product ID
    $free_product_id = 470;

    // Only add the free product for specific shipping methods
    $allowed_shipping_methods = array('free_shipping', 'flat_rate'); // Add your allowed shipping methods here

    if (in_array($chosen_shipping_method, $allowed_shipping_methods)) {
        $product_already_in_cart = wc_find_product_in_cart($free_product_id);

        if (!$product_already_in_cart) {
            WC()->cart->add_to_cart($free_product_id, 1);
        }
    } else {
        // Check if the free product is in the cart
        $cart_item_key = wc_find_product_in_cart($free_product_id);
        if ($cart_item_key) {
            // Remove the free product from the cart
            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;
}

Output

When either of the shipping methods as defined in the code is selected, only then the free product gets added to the cart as shown in the image. In case of changing the chosen shipping method, will remove the free product that was added.

How to Add BOGO (Buy One Get One) Offer for the Chosen Shipping Method in WooCommerce? - Tyche Softwares

In case, if any other shipping method other than the specified shipping method is chosen, then the free product doesn’t get added as shown below.

How to Add BOGO (Buy One Get One) Offer for the Chosen Shipping Method in WooCommerce? - Tyche Softwares

Start using promotions more effectively in your store by addig BOGO deals to different parameters. Just like connecting promotions based on shipping methods, you can also add a bogo buy one get one offer based on the chosen payment method in WooCommerce.

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

Share It:

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