1

Having a issue trying to figure out how I can cache the majority of a site page but keep the opening/closing tag of a module uncached? (ie. add_to_cart_form) Once the page is cached, the add to cart doesn't work as the forms XID hash value has to be dynamic every page load. I'm sure it has something to do with the parse stage, but I have had no luck thus far.

The basics of what I'm trying to do:

{exp:stash:cache name="builder" context="@URI" parse_stage="both"}
    {exp:channel:entries
        channel="sheds"
        disable="categories|category_fields|member_data|pagination|trackbacks"
        dynamic="no"
        url_title="{segment_2}"
    }

    ...

    {stash:nocache}
        {exp:cartthrob:add_to_cart_form
            id="atc-form"
            allow_user_shipping="yes"
            entry_id="{exp:entry_data:entry_id url_title="{segment_2}" channel="sheds" site_id="1"}"
            return="cart/index"
            secure_action="yes"
            secure_return="yes"
        }
    {/stash:nocache}

    ...

    {stash:nocache}
        {/exp:cartthrob:add_to_cart_form}
    {/stash:nocache}

    {/exp:channel:entries}
{/exp:stash:cache} 
1

I don't think there's any way to nocache just the opening and closing tags -- you'd have to enclose all of {exp:cart throb:add_to_cart_form} … {/exp:cartthrob:add_to_cart_form} inside of {stash:nocache} … {/stash:nocache}.

So, there's an alternative approach that might work for you:

In my experience, typically when an EE tag generates an HTML form, the form output is a completely boilerplate, predictable chunk of HTML, but with a changing XID hash, e.g.: something like:

<form action="blah" method="post">
  <div class="hiddenFields">
    <input type="hidden" name="ACT" value="999">
    <input type="hidden" name="XID" value="absbfjhsfjbsdfksd18748">
  </div>

 ...

</form>

When you look at the HTML source output from your templates, if you see that {exp:cart throb:add_to_cart_form} outputs a totally predictable pattern of HTML, then you may simply not use {exp:cart throb:add_to_cart_form} at all. Instead, in its place, you can add the actual form HTML to your template--but with one special EE tag, {XID_HASH}, that generates a new XID.

You'd end-up with something like:

<form action="blah" method="post">
  <div class="hiddenFields">
    <input type="hidden" name="ACT" value="999">

    {stash:nocache}
     <input type="hidden" name="XID" value="{XID_HASH}">
    {/stash:nocache}

  </div>

 ...

</form>

So, then, with all of the "final" form HTML directly in your template, you'd need to nocache only the XID tag (or, as I show above, the HTML input that includes that {XID_HASH} tag).

Added -- one potential gotcha: one warning about using the manual approach -- with the hidden input with the ACT value, that value could conceivably be different between on different installs of EE, if you've separately installed (in this case) CartThrob. For example, on your dev machine, the ACT ID could be 50, but it could be 55 on your production machine. I try to make sure to never separately install add-ons on development / stage / production copies of a site. But if you reuse this code from one project to another, you might have an unavoidable change to make. And, worse case, you'd have to change that ACT value when moving the template code between environments / projects.

1
  • I'll give this a shot, I didn't realize {XID_HASH} was a variable. I was looking on Devot:ee for something similar yesterday thinking that might work.
    – Siebird
    Apr 9 '14 at 12:43
0

I think the issue here is that the entry_id param isn't being rendered. Have you tried something like this:

{stash:nocache}
  {exp:cartthrob:add_to_cart_form 
    entry_id="{/stash:nocache}{product:entry_id}{stash:nocache}" 
  }
{/stash:nocache}

Hinted at by the legend himself: http://devot-ee.com/add-ons/support/stash/viewthread/12576#40677

The {product:entry_id} variable is coming in from a get_list and gets rendered onto the stash variable, something like this:

{exp:cartthrob:add_to_cart_form 
  entry_id="2918" 
}

It looks like it's working on my local, I'll update when I inevitably break it.

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.