Shopify get lowest price product if available in product having same title - shopify

I have duplicate products which offer same size or different size products with same title but vendors is different. I want to display products which ever cheaper in my website. So my website will always display products from one vendor only ( Which ever cheaper ). In product details page i want to check this logic to find the same title products from different vendors. I tried the below code . When i given the handle statically then it is showing other vendor products finely. I want to make it dynamic. Can anybody help me on this.
Now i want to display the lowest price product if available.
Product is having different sku,handle, price but title is same.
Please help me to show lowest price product.
{% assign some_handle = product.handle %}
{% assign custom_product = all_products[some_handle] %}
{{ product.title }} is {{ product.price | money }}, but {{ custom_product.title }} is {{ custom_product.price | money }}
i tried this code in product_template.liquid file but it is not working as expected.

Related

How To display title, image etc. from product meta field of type product?

I'm trying to display the products I saved in a meta field on my product detail page like this:
<h2>Cross Selling goes here</h2>
{{ product.metafields.custom.styled_with }}
... but all I get it is:
["GID://SHOPIFY/PRODUCT/7978216456473","GID://SHOPIFY/PRODUCT/7978217177369","GID://SHOPIFY/PRODUCT/7978217537817","GID://SHOPIFY/PRODUCT/7978217570585"]
How can I render these products? I want to access the product title, image etc.
I'm using the Dawn Theme.
Do I need to run a query or something?
Thanks in advance!
I tried this line of code
{{ product.metafields.custom.styled_with }}
and I expected that I would have access to the product information directly (product title etc.)
I solved this by adding .value
{% for recommended_product in product.metafields.custom.styled_with.value %}
{{ recommended_product.title }}
{% endfor %}

Shopify check how many orders a product has

Is possible to check how many orders/purchases a product has?
For example if a product has 3 orders/purchases, I want to hide it.
{% for product in collection.products %}
// if ( product sells != 3 )
<h2 class="title">{{ 'product.title' | t }}</h2>
{% endfor %}
In the product details, you can add a Metafield resource to it that is a number.
Edit that metafield so that it contains a 3 for example
in your theme, when you are examing the product details to decide whether to render the product or not, check the value of the metafield resource assigned to the product.
profit.

Sort by best selling in shopify liquid

I want to sort collection products by 'best-selling' but it's not work when i test .
i don't know why. Below is my code
{% assign products = collection.products | sort: 'best-selling' %}
When i sort it with 'price', it work, but with best-selling it's not.I hope someone can help me how to do it
The sort function is a generic sort for array. You can sort by any field of the element in the array. In case of products you can write
{% assign products = collection.products | sort: 'title' %}
because the title (or price, etc) is an attribute of the product.
A product doesn't have any attribute that specify how many units have been sold.
You can change the default sorting method of your collection to 'Best Selling' so that products are sorted that way by default.
Also, you were probably confused by the sort_by function which is quite different
{{ collection.url | sort_by: 'best-selling' }}
gives you the link to the collection page using the given sort function.

Shopify - Product Customization with Additional Cost - Cart Removal Issue

I'm working on a Shopify store that wants engraving added to their products for a $25 up-charge. I have set it up so that a checkbox on the product page triggers an engraving dropdown where the user can customize the engraving (type style, text, and symbols) and all of those values are passed as line item properties. The checkbox also adds an additional engraving product to the cart that costs $25 to account for the up-charge and has the same exact line item properties. I realize that both products having the same exact line item properties is a perfect way for them to be linked to ensure that if the original product is removed from the cart, the linked engraving product will be removed as well. But I can't for the life of me figure out the way to accomplish this.
I wrote this Liquid code which effectively removes the engraving product from the cart if it is the only item in the cart.
{% if cart.item_count == 1 %}
{% for item in cart.items %}
{% if item.title == 'Custom Engraving' %}
<script>
$.post('/cart/change.js', { quantity: 0, id: {{ item.id }} });
$("#cart_form").submit();
</script>
{% endif %}
{% endfor %}
{% elsif cart.item_count > 1 %}
Please help with the code that supposed to go in here
{% endif %}
But what if there are 2 engraved jewelry products in the cart? If a user removes one of them, they will be left with 2 $25 engraving products and only one jewlery product. I need to automatically remove the associated engraving product for any jewelry product that is removed from the cart. Please help!
Note: A user seems to have a way to do this based on a couple forum answers I found. This one (https://ecommerce.shopify.com/c/ecommerce-design/t/conditional-logic-for-variants-based-on-line-item-properties-264417) he says " In the cart itself, the only thing you need to do is ensure that if the product itself is removed, you also remove the customization product. No biggy. That is a simple pattern and you can jazz it up too. Group a product with a customization through the properties for example, ensuring the two are "glued" together." That's exactly what I need! But unfortunately he doesn't share the simple pattern he's speaking of. He seems to have answered it on stack overflow as well (Chang the product price when custom checkbox is selected in Shopify) and again doesn't reveal the simple pattern he refers to.
Thanks in advance!
Short Answer:
Make the customization products different by adding a hidden property to make them unique.
Longer Answer:
When adding the product to the cart, generate a unique value - using the current date/time (via Date.now()) is usually a good choice. When adding the product to the cart, make sure this property is added as well (probably via hidden input).
Make sure that the name of extra property is prepended with an underscore - so something like '_uniqueID' - Shopify's checkout page knows to hide any line-item properties that have an underscore as their first character
Finally, make sure your theme knows to not display underscored properties in the cart. My favourite way to do this is by adding the following line at the beginning of your properties-printing loop (usually written as 'for p in item.properties')
{% if p.first.first == '_' %}{% continue %}{% endif %}
Now you have a unique ID field as a line-item property that only you can see, and can use that field to synchronize quantities, etc. Huzzah!

Shopify: ajax-cart-template isn't showing tags for all products, It is only showing tags for last product

I am trying to limit the amount that people can purchase of a product. I am using tags to do this on the cart, collection and product pages. But for some reason it doesn't work on the ajax-cart-template page.
In the {{#items}} I just
{% endraw %}
{{ product.tags }}
{% raw %}
It works fine with one products, but when I add a second it will only show the uppermost products tags. And then repate the tags for the second product with the same tags.