New Shopify Certification now available: Liquid Storefronts for Theme Developers

Prevent customer's from purchasing duplicates

Solved
ModIT
Tourist
6 1 5

Hi there, I'm looking to try and prevent customers from adding products that they've previously purchased to their basket in order to prevent duplicate purchases.

 

Is this doable in pure liquid or will it require js.

This is what I've tried so far, bare in mind my knowledge of liquid is extremely basic:

                <button {% for order in customer.orders %} 
    			{% for item in order.line_items %} 
      			{% if item.product_id == product.id %}
        		disabled 
      			{%endif%}
    			{%endfor%}
 				{%endfor%}

  				{% for item in cart.items %} 
    			{% if item.product_id == product.id %}
      			disabled 
    			{%endif%}
  				{%endfor%}
>
                  <span class="engoj-addcart-text">{{settings.addcart_name}}</span>
                </button>

 

Accepted Solution (1)
ModIT
Tourist
6 1 5

This is an accepted solution.

Hi, it was actually quite a simple fix in the end. Here's the snippet of code that will allow you to apply a class or html property based on whether a product is already owned by a customer.

{% for order in customer.orders %}
    {% if order.fulfillment_status == "fulfilled" %}
        {% for item in order.line_items %}
            {% if item.product_id == product.id %}
                {% assign btnDisabled = true %}
                {% assign productOwned = true %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endfor %}

<button 
    type="submit" 
    class="
    {%if btnDisabled == true %}
      btn-disable
    {% endif %}"
    
    {% if btnDisabled == true %}
        disabled
    {% endif %}
>
<span class="enter-your-styling-here">
    {% if productOwned -%}
        ALREADY OWNED
    {%- elsif btnDisabled -%}
        DISABLED
    {%- else -%}
        ADD TO CART
    {%- endif %}
</span>
</button>

 

This gets all orders on a customer's account, then checks if the order is "fulfilled". If the order *is* fulfilled, it then checks if any of the product id's in the order match the product id of the current product they're viewing and if it does match, it assigns "btnDisabled = true" and "productOwned" = true.

These variables can then be used to apply classes to your button element and even change the current text within the button. (note the span element in the above snippet)

disbled = a html property that disables a button element
btnDisabled = a class used to style the button when its disabled (grey it out and change cursor)

Of course these buttons don't have any styling classes or anything in them, that would be handled by your current theme / setup, you would add this code to that.

View solution in original post

Replies 4 (4)
ShelBGold
Visitor
1 0 0

I'm looking for a solution to that same issue. Would love more insight into a solution for this to relay to my internal team!

ModIT
Tourist
6 1 5

This is an accepted solution.

Hi, it was actually quite a simple fix in the end. Here's the snippet of code that will allow you to apply a class or html property based on whether a product is already owned by a customer.

{% for order in customer.orders %}
    {% if order.fulfillment_status == "fulfilled" %}
        {% for item in order.line_items %}
            {% if item.product_id == product.id %}
                {% assign btnDisabled = true %}
                {% assign productOwned = true %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endfor %}

<button 
    type="submit" 
    class="
    {%if btnDisabled == true %}
      btn-disable
    {% endif %}"
    
    {% if btnDisabled == true %}
        disabled
    {% endif %}
>
<span class="enter-your-styling-here">
    {% if productOwned -%}
        ALREADY OWNED
    {%- elsif btnDisabled -%}
        DISABLED
    {%- else -%}
        ADD TO CART
    {%- endif %}
</span>
</button>

 

This gets all orders on a customer's account, then checks if the order is "fulfilled". If the order *is* fulfilled, it then checks if any of the product id's in the order match the product id of the current product they're viewing and if it does match, it assigns "btnDisabled = true" and "productOwned" = true.

These variables can then be used to apply classes to your button element and even change the current text within the button. (note the span element in the above snippet)

disbled = a html property that disables a button element
btnDisabled = a class used to style the button when its disabled (grey it out and change cursor)

Of course these buttons don't have any styling classes or anything in them, that would be handled by your current theme / setup, you would add this code to that.

RebeccaRoss
Tourist
3 0 2

How do I do this? Where? 

Wtfabric
Tourist
3 0 0

Would love more information on where to put the code you have provided so that I could implement this in my store. Thank you so much for your time and assistance with this!