1

Is it possible to show conditional HTML code or text based on the coupon code that has been entered?

This works fine if there is a coupon code:

{exp:cartthrob:coupon_info}
<div class="helper">"{coupon_code}" has been applied  <span class="label"><a href="{path=store/clear_coupon_codes}">remove coupon code?</a></span></div>
{/exp:cartthrob:coupon_info}

But say I want to add a class to an item or change some text:

No coupon code:

<ul>
<li>Subtotal {exp:cartthrob:cart_subtotal}</li>
<li>Shipping {exp:cartthrob:cart_shipping}</li>
<li>Discount {exp:cartthrob:cart_discount}</li>
<li>Total {exp:cartthrob:cart_total}</li>
</ul>

Coupon code == cash:

<ul>
<li>Subtotal {exp:cartthrob:cart_subtotal}</li>
<li>Discount {exp:cartthrob:cart_discount}</li>
<li class="cash">Cash {exp:cartthrob:cart_total}</li>
</ul>

I've tried a few things and can get them to work when there is a coupon code but not when the coupon code if empty.

1

Because the {coupon_code} can only be returned inside of the {exp:cartthrob:coupon_info} tags, we'll need to Stash that variable for use later on outside of the tags (unless you want to wrap everything in those tags).

So, install Stash and then try this:

{exp:stash:set name="coupon_used"}
    {exp:cartthrob:coupon_info}
        {coupon_code}
    {/exp:cartthrob:coupon_info}
{/exp:stash:set}

{!-- No Coupon Used (Default) --}
<ul>
    <li>Subtotal {exp:cartthrob:cart_subtotal}</li>
    <li>Shipping {exp:cartthrob:cart_shipping}</li>
    <li>Discount {exp:cartthrob:cart_discount}</li>
    <li>Total {exp:cartthrob:cart_total}</li>
</ul>

{!-- Coupon code == 'cash' --}
{if {exp:stash:coupon_used} == 'cash'}
    <ul>
        <li>Subtotal {exp:cartthrob:cart_subtotal}</li>
        <li>Discount {exp:cartthrob:cart_discount}</li>
        <li class="cash">Cash {exp:cartthrob:cart_total}</li>
    </ul>
{/if}

So here we're running the cartthrob tags once and stashing the value of the coupon code for use later on in the template. Then we're running a conditional check on that stashed variable to see if it returns true. If it does then we display the conditional content. All without using any PHP in templates.

Does that work for you?

1
  • Thanks Ian. I thought that might be the case. I will give the Stash approach a try – Mike Dexter Apr 20 '13 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.