1

I'd like to use the Shortlist plugin to allow visitors to create product wishlists - using Craft Commerce.

Currently, I have setup the 'add to wishlist' function, but I am not sure how to then display the items that a user has add to their wishlist.

The code below, is the example given on the plugin docs for how to loop through the items a user has added, but this does not produce the required results for commerce products.

{% if shortlistLists is empty %}
You have no lists at the moment.
{% else %}
    {% for list in lists %}
    <h3>{{ list.title }}</h3>

    {% if list.items is empty %}
    No items in this list
    {% else %}
    <ul>
        {% for item in list.items %}
            <li>{{ item.title }} (<a href="{{ item.removeActionUrl }}">Remove</a>)</li>
        {% endfor %}
    </ul>
    {% endif %}
    {% endfor %}
{% endif %}

The docs for the plugin are available here - https://squarebit.co.uk/software/craft/shortlist/usage/quickstart

If anyone could point me in the right direction as to how to successfully use the plugin in conjunction with Craft Commerce, that would be much appreciated.

2

You can definitely use Shortlist for this. Here's some code to get you started. Note in this I allow the favouriting of both products and pages/articles.

Code to let user add the favourites -

    {% set item = craft.shortlist.item(id) %}
    <span class="favStar"><a class="shortlist_action reset-a" title="Add To Favourites" href="{{ item.addActionUrl }}"><i class="favStar__icon icon-"></i></a></span>

And to display the favourites/wishlist - first we prepare by separating products from regular elements:

        {# Prepare the output data #}
        {% set lists = craft.shortlist.lists %}
        {% set favouriteArticleIDs = [] %}
        {% set favouriteProductIDs = [] %}

        {% if lists|length %}
            {# We just store everything in one list... #}
            {% for list in lists %}
                {% for item in list.items %}
                    {% set favId = item.elementId %}
                    {% if item.element.elementType == 'Commerce_Product' %}
                        {% set favouriteProductIDs = favouriteProductIDs|merge([favId]) %}                          
                    {% else %}
                        {% set favouriteArticleIDs = favouriteArticleIDs|merge([favId]) %}
                    {% endif %}                
                 {% endfor %}
            {% endfor %}
        {% endif %}

...then to actually fetch & display the results we do this:

            {% if not favouriteProductIDs|length %}

                <strong>No favourite products?  Why not add some?</strong>

            {% else %}

                {% set products = craft.commerce.products.id(favouriteProductIDs).order('title').with('variants') %}                    

                {% import "_macros/productPreviewsLib" as productPreviewsLib %}

                {{
                    productPreviewsLib.productPreviews(products,
                    {
                        'display' : 'Table',
                        'showUnavailables' : true,
                    })
                }}

            {% endif %}

(The macro just does {% for product in products %} etc)

That should get you started!

|improve this answer|||||
  • Jeremy, is Shortlist working on an up to date Craft install? Docs show 1.0.2 as the last version in 2015 which was prior to Craft 2.5. I'm wondering if it's been updated but just not documented on the changelog as I see references to a version 1.1?? – shorn Feb 2 '17 at 23:47
  • I'm using Shortlist 1.0.4 which I believe it the latest release, with the latest Craft and Commerce releases, no issues. – Jeremy Daalder Feb 3 '17 at 0:52
  • Cool, thanks! It's currently the only plugin out there that seems like it fits my need, but from the site it looks a little neglected! – shorn Feb 3 '17 at 8:38
1

Digging up an old question here but in order to display the title (and other field data) for Commerce products you need to use {{ item.element.title }}

Thanks to James Greig for that tip.

|improve this answer|||||

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.