"RuffRuff 予約販売" supports the reduction of opportunity losses that occur when products are out of stock, new products are not on sale, or are on backorder.
It's incredibly easy to create pre-orders on your Shopify store, allowing you to register an unlimited number of pre-order products and accept unlimited pre-orders. You can also restrict people from ordering pre-order items and regular items at the same time, or send notifications when the item will be back in stock if it is not yet determined.
There are many unique features that are not found in other pre-order sales apps, such as the "Pay Later" feature that allows you to collect payment before the product is shipped.
My name is Ogasawara and I am CTO at Tsun Inc.!
Recently, I have been developing apps and themes for Shopify.
In this blog, we will share information useful for Shopify development.
I would be happy if this would be helpful for Shopify engineers!
Why did you decide to make it?
When working with an array in Shopify, I wanted to exclude part of it (like JavaScript's Array filter operation), but after researching, I found that Liquid does not have a process for excluding part of an array and creating a new array.
What you created
- Processing to remove specific elements from an array in Liquid
Target audience
- Basic knowledge about Liquid
Implementation
Prepare the target array
First, prepare the array before removing any elements. This time, as an example, we will create an array containing four strings: apple, orange, lemon, and banana. (Convert the string to an array with the split filter.)
{%- liquid
# Array of ['apple', 'orange', 'lemon', 'banana']
assign fruits = 'apple,orange,lemon,banana' | split: ','
-%}
Prepare an array of elements you want to remove
Similarly, create an array with the elements to be removed from the array created above. This time we will remove the orange and banana.
{%- liquid
# Array of ['orange', 'banana']
assign exclude_fruits = 'orange,banana' | split: ','
-%}
Remove any element from the target array
To remove specific elements from an array, first iterate through the array one by one using a for loop. Then, check whether each element is included in the elements to be removed (orange and lemon), and if it is not included, add it to a string. (Since you cannot add it directly to an array, add it to a string first.) Finally, by creating an array from the string using split, you can create an array that does not contain the specified elements from the target array.
{%- liquid
# Array of ['apple', 'orange', 'lemon']
assign fruits = 'apple,orange,lemon,banana' | split: ','
# Array of ['orange', 'banana']
assign exclude_fruits = 'orange,banana' | split: ','
# Prepare the string that will form the array
assign new_fruits_string = ''
for fruit in fruits
if exclude_fruits contains fruit
continue
endif
if new_fruits_string == blank
assign new_fruits_string = fruit
else
assign new_fruits_string = new_fruits_string | append: ',' | append: fruit
endif
endfor
# Generate an array with arbitrary elements removed
# Array of ['apple', 'lemon']
assign new_fruits = new_fruits_string | split: ','
-%}
Summary
This time, we introduced how to remove specific elements from an array using Shopify Liquid!
It's a small topic, but I think it is a process that comes up fairly often when writing Liquid, so I would be happy if it was helpful.
Closing
Tsun Inc. uses Shopify to build stores and develop apps.
If you would like to build a store or develop an app, please contact Tsun Inc.