boldsupport
8/1/2017 - 7:40 PM

bold-csp-metafield-variant.liquid

{%- comment -%}
BOLD-CSP-METAFIELD-VARIANT.LIQUID
Last updated: 2017-July-14
NOTE: Tag matching between customer tags and the shop metafield is case INSENSITIVE
FLAGS:
  base_product:      {product object}                          Base product object for the variant, used to check/set several properties. If not supplied, defaults to bold_product. If no bold_product is set, defaults to product
  hide_action:       'skip' | 'none'  (default)                Forces a 'continue' to skip hidden variants when 'skip' is set
  customer_tag:      {string} | null (default)                 If set, forces bold-variant to calculate as though the customer had the specified tag
{%- endcomment -%}
{%- comment -%}
============================================================================================================
    ASSIGN LIQUID VARIABLES
============================================================================================================
{%- endcomment -%}
{%- assign bold_csp_variant = bold-csp-metafield-variant | default: bold_variant | default: variant | default: item.variant -%}
{%- assign bold_csp_variant_base_product = base_product | default: bold_product | default: product | default: item.product | default: item -%}
{%- assign bold_csp_customer_tag = customer_tag | default: csp_customer_tag -%}
{%- assign bold_csp_variant_id = bold_csp_variant.id -%}
{%- unless bold_csp_customer_tag -%}
  {%- assign bold_csp_customer_tag = 'default' -%}
  {%- if customer.tags and shop.metafields.shop_csp_tag_group.shop_csp_tag -%}
    {%- assign all_shop_tags = shop.metafields.shop_csp_tag_group.shop_csp_tag | append: ',default' | split: ',' -%}
    {%- assign customer_tags = customer.tags | join: ',' | split: ',' -%}
    {%- for customer_tag in customer_tags -%}
      {%- if all_shop_tags contains customer_tag -%}
        {%- assign bold_csp_customer_tag = customer_tag -%}
      {%- endif -%}
    {%- endfor -%}
  {%- endif -%}
  {%- unless customer_tag -%}
    {%- assign csp_customer_tag = bold_csp_customer_tag -%}
  {%- endunless -%}
{%- endunless -%}
{%- assign csp_metafield_namespace = 'csp' | append: bold_csp_variant_id -%}
{%- assign csp_metafield_key = bold_csp_customer_tag | append: '~' | append: '1' -%}
{%- assign metafield_data = bold_csp_variant_base_product.metafields[csp_metafield_namespace][csp_metafield_key] | split: ',' -%}
{%- unless metafield_data == blank -%}
  {%- assign price_type = metafield_data[0] -%}
  {%- assign discount_value = metafield_data[1] | times: 1 -%}
  {%- assign cents = metafield_data[2] -%}
  {%- assign taxable = metafield_data[3] -%}
  {%- assign use_discount_compare_at = metafield_data[4] -%}
  {%- if use_discount_compare_at == '1' and bold_csp_variant.compare_at_price > 0 -%}
    {%- assign new_price = bold_csp_variant.compare_at_price -%}
  {%- else -%}
    {%- assign new_price = bold_variant_price | default: bold_csp_variant.price -%}
  {%- endif -%}
  {%- case price_type -%}
    {%- when '%' -%}
      {%- assign new_price = new_price | times: discount_value | round -%}
    {%- when '$' -%}
      {%- assign new_price = new_price | minus: discount_value | round -%}
    {%- when '-' -%}
      {%- assign new_price = discount_value | round -%}
  {%- endcase -%}
  {%- if new_price <= 0 -%}
    {%- assign new_price = bold_variant_price | default: bold_csp_variant.price -%}
  {%- endif -%}
  {%- if cents != '' -%}
    {%- assign vanity_price = new_price | times: 0.01 | floor | times: 100 | plus: cents -%}
    {%- assign new_price = vanity_price -%}
  {%- endif -%}
  {%- assign bold_variant_price = new_price -%}
  {%- if taxable == '1' -%}
    {%- assign bold_variant_taxable = true -%}
  {%- else -%}
    {%- assign bold_variant_taxable = false -%}
  {%- endif -%}
{%- endunless -%}