简体   繁体   中英

Is there any alternative of PHP In_array() in Shopify?

I tried lots of things but didn't get what exactly I am looking for.

Below is the example of what type of array I am looking for in Shopify,

$array['bag'] = 2; $array['shoes'] = 3; $array['xyz'] = 6;

Here is the sample of what and how I am looking for my array variable in shopify.

Where

bag, shoes, xyz

are product type

and 2,3,6

are number of products added for specific product type.

I know it's easy in PHP, but don't know how to do in Shopify liquid code.

As per Shopify documentation , you cannot initialize arrays. However, you can use split filter to create one dimensional array. You cannot create associative arrays using this. However, as a workaround, use 2 arrays of same length where same index in both arrays point to related key and value as of associated array. Example code

  {% assign product_type = "type-1|type-2|type-3" | split: '|' %}
  {% assign product_count = "1|2|3" | split: '|' %}


    {% for p_type in product_type %}
        {{ p_type }}
        {{ product_count[forloop.index0] }}
    {% endfor %}

Expected output

Product Type   Count
type-1           1
type-2           2
type-3           3

For your particular scenario explained in comments, have a look at below code and code comments. I have used checkout object for sample code. You may adjust according to your need.

// declare 2 vars to create strings - that will be converted to arrays later
{% assign product_type = "" %}
{% assign product_count = "" %}

// iterate over line_items in checkout to build product_type string
{% for line_tem in checkout.line_items %}
  // if product_type exists , then skip -- unique product types
  {% if product_type contains line_tem.product.type%}
  {% else %}
    {% assign product_type = product_type | append: '#' | append: line_tem.product.type %}   
  {% endif %}

{% endfor %}

// remove first extra hash and convert to array
{% assign product_type = product_type | remove_first: "#" | split: '#' %}


// iterate over unique product type array generated earlier
{% for product_type_item in product_type %}
// set product count for this product type to zero initially
{% assign total_count = 0 %}
// iterate over all lin items and +1 if same product type
  {% for line_tem in checkout.line_items %}
    {% if product_type_item == line_tem.product.type%}
      {% assign total_count = total_count | plus: 1 %} 
    {% endif %}
  {% endfor %}
  // append count to product count string
  {% assign product_count = product_count | append: '#' | append: total_count %}
{% endfor %}

// remove first extra hash and convert to array
{% assign product_count = product_count | remove_first: "#" | split: '#'%}

{{-product_type-}}

{{-product_count-}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM