How to indicate "New" sign for fresh product on liquid - shopify

I'm using liquid on Shopify. And new to liquid.
Now, I'm trying to indicate "new" sign for products within 7 days.
{% assign today_date = 'now' | date: '%s' %}
{% assign create_date = product.created_at | date: '%s' %}
{% assign dif = today_date - create_date %}
<div>Time diff is {{ dif }}</div>
{% if dif < 30000 %}
<div>New</div>
{% endif %}
It shows errors like this.
Time diff is 1607714358
Liquid error: comparison of String with 30000 failed
What should I do?
Thanks in advance.

All liquid operations (+,-,/,*/%) are done via filters.
So this one here {% assign dif = today_date - create_date %} is incorrect.
It should be like so {% assign dif = today_date | minus: create_date %}.
This is your only mistake in the code.
Final code should be:
{% assign today_date = 'now' | date: '%s' %}
{% assign create_date = product.created_at | date: '%s' %}
{% assign dif = today_date | minus: create_date %}
<div>Time diff is {{ dif }}</div>
{% if dif < 30000 %}
<div>New</div>
{% endif %}

Related

Execute dbt model only if var is not empty list

I have a dbt incremental model that looks pretty like this:
-- depends_on: {{ref('stg_table')}}
{% set dates_query %}
SELECT DISTINCT date FROM dates_table
{% if is_incremental() %}
WHERE date NOT IN (SELECT DISTINCT date FROM {{this}})
{% endif %}
{% endset %}
{% set dates_res = run_query(dates_query) %}
{% if execute %}
{# Return the first column #}
{% set dates_list = dates_res.columns[0].values() %}
{% else %}
{% set dates_list = [] %}
{% endif %}
{% if dates_list %}
with
{% for date in dates_list %}
prel_{{date | replace('-', '_')}} as (
SELECT smth FROM {{ref('stg_table')}}
WHERE some_date = cast('{{date}}' as date)
),
{% endfor %}
prel AS (
select * from prel_{{dates_list[0] | replace('-', '_')}}
{% for date in dates_list[1:] %}
union all
select * from prel_{{date | replace('-', '_')}}
{% endfor %}
)
SELECT some_transformations FROM prel
{% endif %}
But it fails with error, because it runs following statement in database:
create or replace view model__dbt_tmp
as (
-- depends_on: stg_table
);
So the question is how can I skip the model creation if dates list is empty?
Thanks :)
You need a valid query that has the right columns but returns zero rows. This should work:
{% if dates_list %}
with
{% for date in dates_list %}
prel_{{date | replace('-', '_')}} as (
SELECT smth FROM {{ref('stg_table')}}
WHERE some_date = cast('{{date}}' as date)
),
{% endfor %}
prel AS (
select * from prel_{{dates_list[0] | replace('-', '_')}}
{% for date in dates_list[1:] %}
union all
select * from prel_{{date | replace('-', '_')}}
{% endfor %}
)
{% else %}
prel AS (
SELECT smth FROM {{ref('stg_table')}}
WHERE 1=0
)
{% endif %}
SELECT some_transformations FROM prel
Separately, I would make other simplifications to your code. Jinja has a loop variable inside for loops, and flags called loop.first and loop.last that are only true on the first and last elements of an iterable. So your for loop can become:
prel AS (
{% for date in dates_list %}
{% if not loop.first %}union all{% endif %}
select * from prel_{{date | replace('-', '_')}}
{% endfor %}
)
But really I don't think you need to do all of this work with ctes and unioning. Your RDBMS probably supports the in operator with dates, and/or this could just be a join.

DBT set variable using macros

my goal is to get the last 2 dates from the tables and run insert_overwrite to load incremental on a large table. I am trying to set a variable inside the model by calling on the macros I wrote. The SQL query is in BigQuery.
I get an error message.
'None' has no attribute 'table'
inside model
{% set dates = get_last_two_dates('window_start',source('raw.event','tmp')) %}
macros
{% macro get_last_two_dates(target_column_name, target_table = this) %}
{% set query %}
select string_agg(format('%T',target_date),',') target_date_string
from (
SELECT distinct date({{ target_column_name }}) target_date
FROM {{ target_table }}
order by 1 desc
LIMIT 2
) a
{% endset %}
{% set max_value = run_query(query).columns[0][0] %}
{% do return(max_value) %}
{% endmacro %}
Thanks in advance. let me know if you have any other questions.
You probably need to wrap {% set max_value ... %} with an {% if execute %} block:
{% macro get_last_two_dates(target_column_name, target_table = this) %}
{% set query %}
select string_agg(format('%T',target_date),',') target_date_string
from (
SELECT distinct date({{ target_column_name }}) target_date
FROM {{ target_table }}
order by 1 desc
LIMIT 2
) a
{% endset %}
{% if execute %}
{% set max_value = run_query(query).columns[0][0] %}
{% else %}
{% set max_value = "" %}
{% endif %}
{% do return(max_value) %}
{% endmacro %}
The reason for this is that your macro actually gets run twice -- once when dbt is scanning all of the models to build the DAG, and a second time when the model is actually run. execute is only true for this second pass.

How can filter orders made after a certain date in liquid/ Shopify?

{% for orders in checkout.customer.orders %}
//count the orders
{% endfor %}
I need to count orders made only after a certain date?
How can I do this in Liquid / Shopify?
All Orders have a created_at date that you can output in various formats using the Liquid date filter — you would loop thru the Orders as above and compare that with whatever the "threshold date" in question is, using unix-format dates for comparison:
{% assign ordersThresholdUnix = '2019-01-01' | date: '%s' %}
{% assign ordersCount = 0 %}
{% for orders in checkout.customer.orders %}
{% assign orderDateUnix = order.created_at | date: '%s' %}
{% if orderDateUnix > ordersThresholdUnix %}
{% assign ordersCount = 0 %}
{% endif %}
{% endfor %}
You can then output {{ ordersCount }}.
Note: that I don't think Shopify will allow you to paginate further back than 50 Orders.

Extracting string and comparing with date

I have a string "products_2016-05-09" where 2016-05-09 is date appended in the string. I want to extract this date. If the date is minus 1 day I want to display string "products". How can I do this in liquid syntax?
To extract the date from the string, use the remove and split filters:
{% assign pdate = string | remove: "products_" %}
{% assign pdate = pdate | split: '-' %}
To check if that product date (pdate) is within 24 hours (86400 seconds) back, use something like this:
{% assign today = "now" | date: "%s" %}
{% assign yesterday = today | minus: 86400 %}
{% if pdate[0] == yesterday | date: "%Y" and pdate[1] == yesterday | date: "%m" and pdate[2] == yesterday | date: "%d" %}
Display string "products"
{% endif %}
Note: This only check if the product date is yesterday (24 hours ago from now) for a more accurate time verification, you need to do more arithmetics. You could also do all of this on the front-end using JavaScript.
The below code worked for me:
{% assign var =  {{custom_attribute.${producttype}}} %}
{% assign words = var | split: '_' %}
{% assign yestDate = 'now' | date: "%s" | minus: 86400 | date: "%F" %}
{% assign varDate = words[1] %}
{% if varDate | convert: "date"  == yestDate %}
Dynamic String {{words[0]}}
{% else %}
sorry!
{% endif %}
 

Less than condition in if loop not working - Liquid HTML

In liquid html greater than condition works well in if loop
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% if var1 > 5 %}
test text
{% endif %}
This condition works. But i want to check for less than condition
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% if var1 < 5 %}
test text
{% endif %}
This code breaks the liquid html syntax in < so this condition not working.
Note: this happens on blueshift email template. Not sure about others.
It probably is an app specific bug. Try this instead
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% unless var1 >= 5 %}
test text
{% endunless %}