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.

2 Answers 2

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 Commented Apr 20, 2013 at 10:26
1

This can now be done using EE's layout variables - which is probably a better solution for two reasons:

  • layout variables are part of EE core
  • the Stash add-on is no longer supported

The approach is similar, but you need to split the processing across two layout templates: layout variables are instantiated upon the transition from the template they are defined in to the next template in the layout sequence.

So the solution becomes something like this:

{layout='group/next_template_in_layout'}
{layout:set name='coupon_code'}
    {exp:cartthrob:coupon_info}
        {coupon_code}
    {/exp:cartthrob:coupon_info}
{/layout:set}

And then in 'group/next_template_in_layout':

{!-- 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 {layout: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>
{/if}

HTH 🐾

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.