{% embed 'checkout.inc.twig' %}
{% endembed %}
It is within these two lines that we'll be pasting our custom block. If you already have some other code within the ''embed'' tags, add this additional block right before the closing ''{% endembed %}'' tag. Your own code will be overwriting the ''upsell_modal'' block - and as an initial start, will look like this:
{% embed 'checkout.inc.twig' %}
{% block upsell_modal %}
{% endblock %}
{% endembed %}
Inside this block tag you'll place your own custom logic and HTML. The most common use case is that of offering an link or form to add an extra product into the customers cart, unless they already have the product in their cart. That approach could look simply like this:
{% block upsell_modal %}
{% set showUpsell = true %}
{% for item in items %}
{% if item.code == "my-upsell" %}
{% set showUpsell = false %}
{% endif %}
{% endfor %}
{% if showUpsell %}
Special offer just for you!
As a limited time offer, we're offering you this unique product for only $12.95. That's $5 off our normal price!
{% endif %}
{% endblock %}
A few things happens within this code.
- Firstly, we set a variable called ''showUpsell'' to true
- Next, we loop through the items in the cart and look for any products with a code of ''my-upsell''. If one is present, we set the ''showUpsell'' variable to false.
- Finally, if ''showUpsell'' is still true, meaning the customer hasn't previously purchased the upsell - we display some HTML tempting the customer to our upsell product. If they have, and ''showUpsell'' is false - we don't output any HTML, leaving the block empty and so the modal won't display at all.
==== Important Notes ====
* If your store has link and form encryption enabled, any add to carts you include within the modal will also need to be encrypted. [[v:2.0:hmac_validation|Review the wiki page for the link/form encryption]] for details on doing that.
* Any images that you include within your markup need to be served securely. If you're not already using a remote custom template that you're caching, you can have the image cached manually as [[v:2.0:templates:intermediate-automagicache#caching_assets_manually|detailed here]].
===== Step 2: Enable Upsells =====
The next step is to enable this functionality within your FoxyCart checkout. To do that, you need to make use of the custom values configuration option. Within your store's FoxyCart administration - navigate to the "Configuration" page found under "TEMPLATES". Within the first group of options, enable the "Set your own custom values" option, and paste the following into the text area that appears:
{
"upsell": {
"enabled": true
}
}
{% if context == "checkout" %}
{% endif %}
You can modify this script as needed, updating the ''cookie_expires_in'' variable to change the number of days they shouldn't see the upsell for.
The script also supports passing a custom cookie length, in days, to the ''addUpsellCookie()'' function, so you could customise the number of days the customer shouldn't see the upsell modal for depending on what they interact with. If no parameter is passed, then the default value is used from the top of the script (30 days in the example code above). For example, if you wanted to just hide the modal if cancelled for the default amount, but hide for a year if the customer submits the form - you could edit the form submission event hook like this:
$('body').on("submit", '[data-fc-id="block-upsell-modal"] form', function() { addUpsellCookie(365); });
Finally, you can also comment out either of the event hooks that are triggered on closing the modal or submitting a form, depending on your needs. For example if you only wanted to prevent the upsell from showing again if the customer submits the form, you would comment out this line like this:
// $('body').on("mousedown", '[data-fc-id="block-upsell-modal"] [data-remodal-action="cancel"]', function() { addUpsellCookie(); });