Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce: Add Custom HTML to checkout if coupon == Example123 WordPress

  • SOLVED

I want a function to display custom <HTML> code on checkout page based on what coupon customer uses.

For example: customers uses coupon code "Sarasblog10" , on checkout page we have a custom message like:

Sara is happy you bought this product =) <img />

I already have a function that does this, but here is for product category ID.



add_action( 'woocommerce_before_checkout_form' , product_checkout_custom_content' );
function product_checkout_custom_content() {
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if($term->term_id == 16){ //replace with your category ID
?>
HTML CODE HERE <?php
break;
}
break;
}
}
}

(This code was for a Woocommerce 1.X site , so this might not work on the newest 2.2.x)

So I need a snippet like this for coupons

Thanks!

Answers (1)

2014-10-08

Bob answers:

Please try this code by adding it in your theme's functions.php file


add_action( 'woocommerce_before_checkout_form' , 'product_checkout_custom_content' );

function product_checkout_custom_content() {

global $woocommerce;
$coupon_id = 'Sarasblog10';
$applied_coupon = $woocommerce->cart->applied_coupons;
if( $applied_coupon[0] == $coupon_id ) {
echo 'Sara is happy you bought this product =) <img />';
}

}


Bob comments:

If you have multiple coupon codes. you can use following code

Here the important line is <em>$msgs = array('coupon1'=>'message 1','coupon2'=>'message 2','coupon3'=>'message 3', 'testit' => 'message 4');</em>

the array keys should be your coupon codes in above example coupon1, coupon2
and your message should be value of array in above example message 1, message 2 etc


add_action( 'woocommerce_before_checkout_form' , 'product_checkout_custom_content' );

function product_checkout_custom_content() {

global $woocommerce;
$msgs = array('coupon1'=>'message 1','coupon2'=>'message 2','coupon3'=>'message 3', 'testit' => 'message 4');
$applied_coupon = $woocommerce->cart->applied_coupons;
if( array_key_exists($applied_coupon[0], $msgs) ) {
echo $msgs[$applied_coupon[0]];
}

}


FirstEntertainment comments:

Looks good Bob. Yes I need this function for more than 1 coupon.. like the inital code I would like to have it a more like this:



add_action( 'woocommerce_before_checkout_form' , 'coupon_checkout_custom_content' );

function coupon_checkout_custom_content() {

global $woocommerce;

/* remove this?? $coupon_id = 'Sarasblog10'; */

$applied_coupon = $woocommerce->cart->applied_coupons;

if( $applied_coupon[0] == "Sarasblog10" ) {

?> 100 Rows of HTML code, many divs and images etx <?

}
if( $applied_coupon[0] == "Adamscoupon20" ) {

?> 100 Rows of HTML code, many divs and images etx <?

}
if( $applied_coupon[0] == "coupon3" ) {

?> 100 Rows of HTML code, many divs and images etx <?

}

}

Seems like you answer a lot here Bob. Perhaps you want to work hourly for me? Please add me on skype: pete88pepp

=)


Bob comments:

Hi,

I am glad that it works for you :)

I have added you on skype.

Please close this question by voting.

Thanks & Regards,
Bob