Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Prevent customer's from purchasing duplicates

Solved

Prevent customer's from purchasing duplicates

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 5 (5)

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!

magecomp
Shopify Partner
452 31 47

Hello @ModIT 

 

https://apps.shopify.com/purchase-limit? Our app has future limit product qty based on days/months. 
This is the only app available on app store, having future limit restriction.

Helping voluntarily. Please like and accept the solution if it helps. Thanks!
Our Bestseller Shopify Apps    |      Mobile App Builder by MageComp    |      Shoplock Hide Pages/Collections

Need a developer?  Just visit MageComp website