WooCommerce: Buy Now Buttons with Coupon Added Automatically

Jon @ WP Templates Technical, Tutorial

Landing pages. They are super focused on selling a specific product.

If you need one setting up we can help. If you already have one but want to add some purchasing CTAs, read on!

The problem is getting the buy now button to do everything in one click. On a standard install of WordPress and WooCommerce there would be a few extra steps to put 1 or a group of products in the cart, then a coupon code might need to be added, then you have to go to the checkout page.

Let’s make it all happen in 1 click 🙂

NOTE: A backup is always a good idea when changing or adding code. Safety first!

First, let’s make the shortcode that creates the buy now button. Add this to your functions.php file. Make sure that you have a child theme activated otherwise this will get removed in the next update!

// Add BUY NOW button
function buy_now_straight_to_cart($atts) {
  // Get details from the shortcode
  $atts = shortcode_atts(array('text' => 'BUY NOW', 'coupon' => 'NOCOUPON', 'coupon_id' => '0', 'class' => '', 'add_items' => '819,886'), $atts, 'buy_now_straight_to_cart');
    return '<form method="post" action="">
    <input type="hidden" id="coupon" name="coupon" value="'.$atts['coupon'].'"/>
    <input type="hidden" id="coupon_id" name="coupon_id" value="'.$atts['coupon_id'].'"/>
    <input type="hidden" id="add_items" name="add_items" value="'.$atts['add_items'].'"/>
        <button type="submit" class="'.$atts['class'].'" name="add_buy_now_straight_to_cart">'.$atts['text'].'</button>
    </form>';
}
add_shortcode('buy_now_straight_to_cart', 'buy_now_straight_to_cart');

What does this do?

This creates a form with some variables to pass through about coupons, the coupon id and the items that you want to add to the cart.

Basic shortcode is:

[buy_now_straight_to_cart]

options available are:

  • text – this is what the button text will be
  • coupon – this is the coupon name i.e. 10off
  • coupon_id – this is the id of the coupon. You can find this by going to edit the coupon, then in the URL you’ll see ?post=123. The coupon id, in this case, would be 123.
  • class – any CSS classes you want to add to the button.
  • add_items – this is the ids (use a comma to separate the items) of the items in your WooCommerce store. Find the ids by going into the product post and checking the URL, or if you put restrictions on the coupons for specific items, you’ll see the ids when you add the restricted products.

This is the button sorted!

Add the items to the cart and apply the coupon

Add this code to your functions.php

// Add items to the cart
function add_buy_now_straight_to_cart() {
  global $woocommerce;
    if ( isset($_POST['add_buy_now_straight_to_cart']) ){

    	// Grab the variables
    $coupon = $_POST['coupon'];
    $coupon_id = $_POST['coupon_id'];
    $add_items = $_POST['add_items'];

    // Add the items to the cart
    $add_item_array = explode(',',$add_items);
    foreach($add_item_array as $item) {
      $woocommerce->cart->add_to_cart($item);
    }
    
    // Add in the discount
    apply_matched_coupon($coupon_id, $coupon);
    
        wc_add_notice( __('Cart has been updated'), 'notice' );
        wp_redirect( wc_get_checkout_url() );
        exit();
    }
}
add_action( 'template_redirect', 'add_buy_now_straight_to_cart' );

// Add the coupon
function apply_matched_coupon($coupon_id = false, $coupon = false) {
    global $woocommerce;

    // Check to see if the coupon we are adding is already applied
  if($coupon_id && in_array($coupon_id, $woocommerce->cart->get_applied_coupons())){
        $woocommerce->cart->remove_coupon($coupon_id);

        // Re calculate the totals
    $woocommerce->cart->calculate_totals();
    }
 
 	// Add the coupon 
 	if($coupon) {
 		$woocommerce->cart->add_discount($coupon);
 	}
}
add_action( 'woocommerce_before_cart', 'apply_matched_coupon' );

Basically, this loops through the item ids, adding each one.

Then it moves on to adding the coupon.

It does a few checks to stop errors etc but nothing too complex.

That’s it!

If you need help, feel free to get in touch with support!