Menu Close

Get Support From ShopWired Close

All systems fully operational

Subscribe To Updates
Prefer To Live Chat? Chat directly with ShopWired support Available from 9.00am to 6.00pm Monday to Friday Quickest response time
Send A Message
Response within 24 hours

Menu Close

Menu

Detecting whether an item is in the basket

There maybe times where you want to detect whether a particular item is in a visitor's shopping basket so that you can 'do' something on the page accordingly.

For example, you may have a promotion running on a particular product and be using a voucher code to allow customers to access the promotion (like a 10% off voucher code). You may want to advertise the voucher code at checkout to ensure that customers use the voucher code, but only want to display the code if the applicable product is in the customer's basket.

To do so, you'll need to know how to detect whether the item is in the basket or not.

In this help guide, we're going to set a variable called product_in_basket which can then be used elsewhere on the page to perform a different function.

Firstly, you'll need to obtain the unique ID of the product that you want to detect. You can get the ID of the product either through the export system or by clicking to edit the product through the management system and then getting the long number from the URL in your browser's address bar.

You'll need to open the checkout_basket.twig file using the page editor and on the next line, after the {% block page_content %} code at the top of the page, enter the code below.

{% set product_in_basket = false %}
{% for item in basket.items %}
    {% if item.product.id == X %}
        {% set product_in_basket = true %}
    {% endif %}
{% endfor %}

You'll need to replace x with the unique ID of the product that you want to detect.

You can now use the variable product_in_basket to detect if the item is in the basket and perform a particular action on the site, e.g.

{% if product_in_basket %}
    ...
{% endif %}