1

In this case I'm using switchee to test if a product exists in a cartthrob order (could be 1 item or 100 in the order) and should a particular product be found then load a snippet.

The issue is the test continues running for all items in the cart, spitting out multiple snippets if the condition is met more then once, obviously. You could replace cartthrob data with channel:entries data and same behaviour persists.

What I suppose I'm asking is there someway with switchee or normal if syntax to exit when any condition is met?

I am already using stash which may be useful but at a bit of a loss ATM.

UPDATE#1

{exp:cartthrob:cart_items_info}
{switchee variable="{channel}" parse="inward"}
    {case value="event"}{!-- There is an event, look it up and show results --}
        <div class="upsell upsell-item">
            {stash:embed:upsell:emb_get_upsell stash:entryid="{entry_id}"}
        </div>
    {/case}

    {case default="yes"} {!-- If no event exists then add generic upsell snippet --}
        <div class="upsell upsell-item">
        <p>Need a generic products upsell here</p>
        {stash:embed:upsell:emb_generic_upsell}    
        </div>
    {/case}
{/switchee}
{/exp:cartthrob:cart_items_info}

Looking at that code, I can see why it's repeating and adding multiple, what I really want is to look through all :cart_items_info & if an event IS found, load the desired snippet and EXIT OR if no event exists then load generic snippet AND exit.

3
  • Could you add a sample of your code? Commented Mar 29, 2013 at 12:31
  • Yeah we'd really need to see the code in question to know how things are looping. By default Switchee will break on match however.
    – Ian Young
    Commented Mar 29, 2013 at 17:51
  • Code added to question. Commented Mar 30, 2013 at 6:52

1 Answer 1

1

There are several ways you could go about it really. The simplest would probably be to try a relatively new (and experimental) parameter in the car_items_info tag which filters the results based on a custom field:

{exp:cartthrob:cart_items_info search:field_name="value"}
    ...
{/exp:cartthrob:cart_items_info}

Where field_name is the name of your custom field and value is the search criteria. I don't know what you have in your Channel Fields but there may be something you can hook on to there. Just remember that this is still under development and may not be 100% reliable just yet.

Alternatively if you say you already have Stash installed then that will happily do the job:

{!-- Loop through the cart contents --}
{exp:cartthrob:cart_items_info}
    {exp:stash:set parse_tags="yes"}
        {exp:channel:entries entry_id="{entry_id}"}
            {!-- Grab the Channel Name as the handle & set the variable --}
            {stash:channel}{channel_name}{/stash:channel}
        {/exp:channel:entries}
    {/exp:stash:set}
{/exp:cartthrob:cart_items_info}

{!-- Later on in the template, outside of the Cartthrob cart_items_info tags --}
{if "{exp:stash:get name='channel'}" == 'event_channel_name' }
    It's an event!
{/if}

{!-- Or with Switchee --}
{switchee variable="{exp:stash:get name='channel'}"}
    {case value="event"}{!-- There is an event, look it up and show results --}
        It's an event!
    {/case}

    {case default="yes"} {!-- If no event exists then add generic upsell snippet --}
        It's NOT an event
    {/case}
{/switchee}

In your example code you have another inner Stash tag where you're pulling stuff in so you'd need to compensate for that but I'm giving the breakdown on how it can be implemented. The inner details you'll need to sort out as I don't know the Channel Fields or the rest of your template data.

Hope that helps.

1
  • The stash solution is the go, we do have seperate channels but they all share common fields and if multiples do exist it will trigger multiples anyway, stash vars addresses this in more logical way. Thx Ian. Commented Mar 31, 2013 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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