How do I retrieve a specific order in Shopify Liquid? - shopify

The problem
I'm creating a proxy app page for a store in Shopify and would like to get a specific order based on the order id using liquid code.
The proxy page will have an url like this: /tools/integrator/order/{id} where {id} is the order id. The proxy page will create liquid code and a mix of other information from a custom database.
I would like to get the specific order with something like:
<p>
Created: customer.orders[xyz].created_at
</p>
where the xyz number is the order id, not the array index. The orders array doesn't seem to have an option to get an order by the order id but only by the index.
My current Non optimised solution
The only way I've found is to create a for loop and compare the id of each order with the id that I need to get:
{% for order in customer.orders %}
{% if order.id == 4225044174 %}
<p>Order found!</p>
{% endif %}
{% endfor %}
How can I get the order by order id without a loop?

Related

Create Multiple Tables in BigQuery Using dbt for-loop

I am trying to create individual tables inside a single dataset in BigQuery using a for-loop in dbt, going through a list of accounts, with no success so far.
A little bit of context - I am using Stitch to fetch data from Facebook Ads and to push it to our BigQuery warehouse. Then, based on the model below, create new separate table for each account with aggregated/modelled data.
The declaration of the variables looks like:
-- table that contains list of accounts
{% set account_data = ref('bq_acct_list') %}
{% set accounts = get_column_values(table=account_data, column='bq_name_suffix') %}
And the query that the tables have to created based on is:
SELECT
DATE_TRUNC(DATE(date_start), DAY) date,
account_id,
account_name,
ROUND(SUM(spend), 2) ad_spend
FROM `{{ target.project }}.{{account}}.ads_insights`
GROUP BY 1, 2, 3
What is missing (I think) is the wrapper of the query + the for-loop itself. Can anyone help me fill in the blanks?
dbt operates under a paradigm of one model (i.e. a .sql file in your models/ directory) is represented by one object (table/view) in your data warehouse — at the moment there's no way around that.
If you need to maintain separate tables per account I'd consider:
Wrapping up the logic into a macro:
-- macros/account_transform.sql
{% macro account_transform(account) %}
SELECT
DATE_TRUNC(DATE(date_start), DAY) date,
account_id,
account_name,
ROUND(SUM(spend), 2) ad_spend
FROM `{{ target.project }}.{{ account }}.ads_insights`
GROUP BY 1, 2, 3
{% endmacro %}
Create a separate model for each account, and call the macro in each model:
-- models/my_first_account.sql
{{ account_transform('my_first_account') }}
-- models/my_second_account.sql
{{ account_transform('my_second_account') }}
Depending on your exact use-case, you might also consider creating a master table for all accounts, by unioning them together. That way, you only have to create one model. Check out the article on "Unioning together identically-structured sources" for some techniques for this approach.

sql calculated column when find words

I am trying to accomplish the following.
There is a calculated field I need to create that sums the rows when it finds a specific word. Something like this:
SELECT sum(
case
when product.description like'%trans%'
or product.description like '%transitions%'
then sales
else null
end
) as transitions
How can I search for multiple words in a column using the conditional case to sum only the rows that have these words?
First, your particular example is poor because '%trans%' matches '%transitions%'. So, this is sufficient for your example:
SELECT sum(case when product.description like '%trans%'
then sales
end) as transitions
You can search for other things using or:
SELECT sum(case when product.description like '%trans%' or
product.description like '%cis%'
then sales
end) as transitions
Note that if you are looking for words in product.description then perhaps a text field is not the right structure (that is, perhaps you should be storing each word separately). Alternatively, you might want to look into full-text search.

Get Filtered Data From HubDB from Multiple Select Column

I have a Table in HubDB as follows...
I have Filter data by Gender by Following Code Snippet
{% for row in hubdb_table_rows(675094, 'gender=1') %}
But, Now i want to filter Data by Multiple Filed. I'm stuck here.
I want data which has M5 value in Multiple Filed.
You can add multiple limits to your query with an &.
I haven't tested it but it should be something like:
{% for row in hubdb_table_rows(675094, 'gender=1&multiple_feed__contains=M5') %}
You can add multiple limits to your query with an &.
I haven't tested it but it should be something like:
{% for row in hubdb_table_rows(675094, 'gender=1&multiple_feed=M5') %}

Filtration in product page shopify

Right now filtration by tag is done by AND..
(mysite.com/collections/product_sub/x+y)
what should i do so that filtration is done by OR i.e suppose
product A has tag x
product B has tag y
product C has tag z
now if user select x and y tags then all product should be shown having any of both tag
any help is greatly appreciated
Its not possible in shopify. Shopify will only display the products that has both x and y tags. It will not combine results.
If you want to really achieve this, You need to use ajax. So when someone will select mysite.com/collections/product_sub/x , Your ajax script will retrieve all products from that url.
When user will select another tag which makes the url like mysite.com/collections/product_sub/x+y , ajax script will retrieve all products from mysite.com/collections/product_sub/y and combine in product list ( make sure it remove duplicates if there any )

How can I unwind multiple connections from with?

I'm 99% complete with my query but just have to optimize it now. In the query below the number of Media's having relation WITH for tag target and tag is calculation twice. Once in the first match when I do count(r) and secondly at WHERE media-[:WITH]->(target) WITH COUNT(*). I would like to optimize the query to pass the counts from the first match down and correlate it with each tag so that I do not have to calculate this value twice. How can I pass 2 collections and unwind them? Or if there is any other way to save on this duplicate calculation I'd love to hear it.
Sample console: http://console.neo4j.org/?id=26vqyk
MATCH (target:Tag {name:'tagName'})<-[:WITH]-(:Media)-[r:WITH]->(tag:Tag)
WITH count(r) as counts, tag, target
ORDER BY counts DESC LIMIT 100
WITH collect(tag) as tags, target
UNWIND tags as tag
MATCH (m:Media)-[r:WITH]->(tag)
WITH count(r) as totes, collect(m) as medias, target, tag.name as tag
UNWIND medias as media
MATCH media
WHERE media-[:WITH]->(target)
WITH COUNT(*) as both, totes, tag
RETURN tag, both, totes, both/(totes*1.0)*100
ORDER BY both DESC LIMIT 100
Why do you collect the tags first and then unwind immediately after?
Esp. as you never use tags again?
Some idea:
MATCH (target:Tag {name:'tagName'})<-[r1:WITH]-(:Media)-[r2:WITH]->(tag:Tag)
WITH count(distinct r2) as tagCounts, count(distinct r1) as mediaCounts, tag, target
ORDER BY mediaCounts DESC LIMIT 100
MATCH (m:Media)-[r:WITH]->(tag)
WITH count(r) as totes, size((m)-[:WITH]->(target)) as both,
target, tag.name as tag, mediaCounts, tagCounts
....