How to get all the products starting with title 'A' in Shopify - shopify

Is there a way to get all the products starting with title A or B or C in Shopify without using collection.

Unfortunately this might be not possible.
Ideally this would work but it doesn't:
{% assign my_products = collections['all'].products | where: "title.first", "A" %}
{% for product in my_products %}
{{ product.title }}
{% endfor %}

Related

Is there an alternate way to display line item prices on a Shopify packing slip?

By default, Shopify's packing slip uses
{% if item.sku == line_item.sku %}
{% assign final_price = line_item.final_price %}
{% endif %}
{% if final_price %}
{{ final_price | money }}
{% endif %}
to display a line item's price on a packing slip. I have a client who needs to use the same sku for multiple products, and this has resulted in the wrong line item prices being displayed on some packing slips. Does anyone know of a way to display the line-item price without referencing an item's sku? I have already tried using product and variant id, title, etc. Variant barcode looks like it may work, but this client does not have barcodes for their products.
These are the liquid objects available to you.
https://help.shopify.com/en/manual/shipping/setting-up-and-managing-your-shipping/packing-slips-variable-list
From that list I'm not sure if you can return the line_item.final_price value here.
But for the condition, i'd run it on variant_title
{% if item.variant_title == line_item.variant_title %}
{% assign final_price = line_item.final_price %}
{% endif %}
{% if final_price %}
{{ final_price | money }}
{% endif %}

On Shopify, check if a given product is not on any collection

Within the product page, I would like to hide a certain element if the current product isn't associated with any collection. Is there any way to check it with Liquid ?
The object product.collections is an array containing all collections where the product is listed.
So you may use this:
{% if product.collections.size < 1 %}
Do something
{% endif %}
Documentation about arrays here:
https://shopify.dev/api/liquid/filters#size
{% if 0 == product.collections | size %}
!! put your code here !!
{% endif %}
Or
{% for _ in product.collections %}
{% else %}
!! put your code here !!
{% endif %}

Looping through metafields in shopify

I need to display n number of images for a product in shopify.
I have stored number of images in a metafields and created a loop for it.
Then each image's name is stored in a metafield, which i am trying to get with help of loop.
{% assign earrings = product.metafields.earrings %}
{% for i in (1..earrings.total-earrings) %}
{% assign earring = 'product.metafields.earring-' | append:i %}
{{ earring.name }}
{% endfor %}
This loop is giving me values for earring like:
product.metafields.earring-1
product.metafields.earring-2
but when i am trying to read value of metafield earring.name, i am not getting any output. I think because product.metafields.earring-1 is a string.
Is there any possible way to loop through metafields like this and get values?
Just in case it's helpful for someone.
Here's the updated code:
{% assign earrings = product.metafields.earrings %}
{% for i in (1..earrings.total-earrings) %}
{% assign dummy = 'earring-' | append:i %}
{% assign earring = product.metafields[dummy] %}
{{ earring.name }}
{% endfor %}

Show item quantity in shopping cart for a product when on the collections page

I'm in need of some help, what I'd like to do is best described as follows:
When on the collections page
If Product X is added 2 times to the cart
Then add the number 2 next to that product
So the customer knows what products are already inside the cart
So basically, if taking a look at the collections page, I want to be able to see what products are already added into the cart.
I've tried this:
<!-- From 'product-grid-item.liquid' of the default Supply theme -->
{% assign count = 0 %}
{% for item in cart.items %}
{% if product.variants.first.id == item.id %}
{% assign count = count | plus: 1 %}
{% endif %}
{% endfor %}
{% if count > 0 %}<span>{{ count }}</span>{% endif %}
But it simply returns a 1 for Product X. It should return a 2 since it's added twice.
What am I doing wrong?
Here's the working code:
{% assign count = 0 %}
{% for item in cart.items %}
{% if product.id == item.product.id %}
{% assign count = count | plus: item.quantity %}
{% endif %}
{% endfor %}
{% if count > 0 %}<span>{{ count }}</span>{% endif %}
This is because cart.items returns the line items. So it will only return once per unique item id. If you want to count the total item quantity for any item id you need to look at line_item.quantity. Reference docs.shopify.com/themes/liquid-documentation/objects/cart

Remove variants from variants array on product in Liquid

Good evening! I am trying to remove variants from the variant array on a product using pure Liquid(Shopify templating language). I would only like to use javascript as a last resort.
Below is where I am so far. Anything that is the variant in the if check needs to be removed from currentProduct.variants.
{% assign currentProduct = product %}
{% for variant in currentProduct.variants %}
{% include 'shappify-variant-is-csp' %}
{% if csp_variant != 1 %}
//need to remove the object that meets this if statement
{% endif %}
{% endfor %}
I'm pretty sure you're going to need to use some javascript to achieve this. Take a look at this article on the Shopify wiki: How do I remove sold out variants from my options drop-downs.
Modifying the code in that article for your situation, you'll want something like this:
{% for variant in product.variants %}
{% include 'shappify-variant-is-csp' %}
{% if csp_variant != 1 %}
jQuery('.single-option-selector option').filter(function() { return jQuery(this).html() === {{ variant.title | json }}; }).remove();
{% endif %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');