How to apply automatic discount for specific customer tags?

cjulo5949
Visitor
1 0 0

Hello, I think I'm trying to perform a pretty simple task but it is getting more and more difficult. We have a set segment of customers that we would like to offer 10% off all products in our store. We don't want to use an app as the app discounts are not propagating through to our shipping software. I also want this displayed on the product page not just at the cart at checkout, so that the customer can see this discount up front. I'm working on Woodstock Theme. In our price.liquid file here is the code I have: 

 

 

 

 

{%- comment -%}
  Renders a list of product's price (regular, sale)

  Accepts:
  - product: {Object} Product Liquid object (optional)
  - use_variant: {Boolean} Renders selected or first variant price instead of overall product pricing (optional)
  - show_badges: {Boolean} Renders 'Sale' and 'Sold Out' tags if the product matches the condition (optional)
  - show_in_card: {Boolean} Detect the price is shown in a product card or details page
  - price_class: {String} Adds a price class to the price element (optional)

  Usage:
  {% render 'price', product: product %}
{% endcomment -%}

<div {% if show_in_card %}class="price--in-card"{% else %}class="price--out-card"{% endif %}>
  <div class="price__container{% if compare_at_price > price and show_in_card %} break{% endif %}">
    <div class="price__regular">
      <span class="visually-hidden visually-hidden--inline">{{ 'products.product.price.regular_price' | t }}</span>
      <span class="price-item price-item--regular">
        {{ money_price }}
      </span>
    </div>
    <div class="price__sale{% if show_in_card %} flex-column{% endif %}">
      {%- unless product.price_varies == false and product.compare_at_price_varies %}
        <span class="visually-hidden visually-hidden--inline">{{ 'products.product.price.regular_price' | t }}</span>
        <span class="flex align-center">
          <s class="price-item price-item--regular">
            {% if settings.currency_code_enabled %}
              {{ compare_at_price | money_with_currency }}
            {% else %}
              {{ compare_at_price | money }}
            {% endif %}
          </s>
          
          {%- if show_badges and show_in_card and target.price < target.compare_at_price -%}
            <span class="price__badge price__badge--in-card lh1 color-foreground">
              {%- if settings.sale_badge_label == 'text' -%}
                {{ 'products.product.on_sale' | t }}
              {%- elsif settings.sale_badge_label == 'percent' -%}
                {{ target.price | minus: target.compare_at_price | times: 100.00 | divided_by: target.compare_at_price | round }}%
              {%- else -%}
                {%- if settings.currency_code_enabled -%}
                  -{{ target.compare_at_price | minus: target.price | money_with_currency }}  
                {%- else -%}
                  -{{ target.compare_at_price | minus: target.price | money }}  
                {%- endif -%}  
              {%- endif -%}  
            </span>
          {%- endif -%}
        </span>
      {%- endunless -%}
      <span class="visually-hidden visually-hidden--inline">{{ 'products.product.price.sale_price' | t }}</span>
      <span class="price-item price-item--sale price-item--last{% if show_in_card %} price-item--sale-in-card{% endif %}">
        <script>
          // Assume isDiscountedCustomer is passed from your Liquid context
          var isDiscountedCustomer = {{ isDiscountedCustomer }};
          var finalPrice = {{ money_price }};
          if (isDiscountedCustomer && {{ compare_at_price }} > {{ price }}) {
              finalPrice *= 0.9; // Apply 10% discount
          }
          document.write(finalPrice.toFixed(2));
        </script>
      </span>
    </div>
    {%- unless show_in_card and product.selected_or_first_available_variant.unit_price_measurement == nil -%}
    <small class="unit-price caption{% if product.selected_or_first_available_variant.unit_price_measurement == nil %} hidden{% endif %}">
      <span class="visually-hidden">{{ 'products.product.price.unit_price' | t }}</span>
      <span class="price-item price-item--last">
        <span>{{- product.selected_or_first_available_variant.unit_price | money -}}</span>
        <span aria-hidden="true">/</span>
        <span class="visually-hidden">&nbsp;{{ 'accessibility.unit_price_separator' | t }}&nbsp;</span>
        <span>
          {%- if product.selected_or_first_available_variant.unit_price_measurement.reference_value != 1 -%}
            {{- product.selected_or_first_available_variant.unit_price_measurement.reference_value -}}
          {%- endif -%}
          {{ product.selected_or_first_available_variant.unit_price_measurement.reference_unit }}
        </span>
      </span>
    </small>
    {%- endunless -%}
  </div>
  {%- if show_badges and show_in_card == false and target.price < target.compare_at_price -%}
    <span class="price__badge lh1 color-foreground">
      {%- if settings.sale_badge_label == 'text' -%}
        {{ 'products.product.on_sale' | t }}
      {%- elsif settings.sale_badge_label == 'percent' -%}
        {{ target.price | minus: target.compare_at_price | times: 100.00 | divided_by: target.compare_at_price | round }}%
      {%- else -%}
        {%- if settings.currency_code_enabled -%}
          -{{ target.compare_at_price | minus: target.price | money_with_currency }}  
        {%- else -%}
          -{{ target.compare_at_price | minus: target.price | money }}  
        {%- endif -%}  
      {%- endif -%}  
    </span>
  {%- endif -%}
</div>

 

 

 

 

Then here is the product-price.js I have: 

 

 

 

 

{%- comment -%}
  Renders a list of product's price (regular, sale)

  Accepts:
  - product: {Object} Product Liquid object (optional)
  - use_variant: {Boolean} Renders selected or first variant price instead of overall product pricing (optional)
  - show_badges: {Boolean} Renders 'Sale' and 'Sold Out' tags if the product matches the condition (optional)
  - show_in_card: {Boolean} Detect the price is shown in a product card or details page
  - price_class: {String} Adds a price class to the price element (optional)

  Usage:
  {% render 'price', product: product %}
{% endcomment -%}

<div {% if show_in_card %}class="price--in-card"{% else %}class="price--out-card"{% endif %}>
  <div class="price__container{% if compare_at_price > price and show_in_card %} break{% endif %}">
    <div class="price__regular">
      <span class="visually-hidden visually-hidden--inline">{{ 'products.product.price.regular_price' | t }}</span>
      <span class="price-item price-item--regular">
        {{ money_price }}
      </span>
    </div>
    <div class="price__sale{% if show_in_card %} flex-column{% endif %}">
      {%- unless product.price_varies == false and product.compare_at_price_varies %}
        <span class="visually-hidden visually-hidden--inline">{{ 'products.product.price.regular_price' | t }}</span>
        <span class="flex align-center">
          <s class="price-item price-item--regular">
            {% if settings.currency_code_enabled %}
              {{ compare_at_price | money_with_currency }}
            {% else %}
              {{ compare_at_price | money }}
            {% endif %}
          </s>
          
          {%- if show_badges and show_in_card and target.price < target.compare_at_price -%}
            <span class="price__badge price__badge--in-card lh1 color-foreground">
              {%- if settings.sale_badge_label == 'text' -%}
                {{ 'products.product.on_sale' | t }}
              {%- elsif settings.sale_badge_label == 'percent' -%}
                {{ target.price | minus: target.compare_at_price | times: 100.00 | divided_by: target.compare_at_price | round }}%
              {%- else -%}
                {%- if settings.currency_code_enabled -%}
                  -{{ target.compare_at_price | minus: target.price | money_with_currency }}  
                {%- else -%}
                  -{{ target.compare_at_price | minus: target.price | money }}  
                {%- endif -%}  
              {%- endif -%}  
            </span>
          {%- endif -%}
        </span>
      {%- endunless -%}
      <span class="visually-hidden visually-hidden--inline">{{ 'products.product.price.sale_price' | t }}</span>
      <span class="price-item price-item--sale price-item--last{% if show_in_card %} price-item--sale-in-card{% endif %}">
        <script>
          // Assume isDiscountedCustomer is passed from your Liquid context
          var isDiscountedCustomer = {{ isDiscountedCustomer }};
          var finalPrice = {{ money_price }};
          if (isDiscountedCustomer && {{ compare_at_price }} > {{ price }}) {
              finalPrice *= 0.9; // Apply 10% discount
          }
          document.write(finalPrice.toFixed(2));
        </script>
      </span>
    </div>
    {%- unless show_in_card and product.selected_or_first_available_variant.unit_price_measurement == nil -%}
    <small class="unit-price caption{% if product.selected_or_first_available_variant.unit_price_measurement == nil %} hidden{% endif %}">
      <span class="visually-hidden">{{ 'products.product.price.unit_price' | t }}</span>
      <span class="price-item price-item--last">
        <span>{{- product.selected_or_first_available_variant.unit_price | money -}}</span>
        <span aria-hidden="true">/</span>
        <span class="visually-hidden">&nbsp;{{ 'accessibility.unit_price_separator' | t }}&nbsp;</span>
        <span>
          {%- if product.selected_or_first_available_variant.unit_price_measurement.reference_value != 1 -%}
            {{- product.selected_or_first_available_variant.unit_price_measurement.reference_value -}}
          {%- endif -%}
          {{ product.selected_or_first_available_variant.unit_price_measurement.reference_unit }}
        </span>
      </span>
    </small>
    {%- endunless -%}
  </div>
  {%- if show_badges and show_in_card == false and target.price < target.compare_at_price -%}
    <span class="price__badge lh1 color-foreground">
      {%- if settings.sale_badge_label == 'text' -%}
        {{ 'products.product.on_sale' | t }}
      {%- elsif settings.sale_badge_label == 'percent' -%}
        {{ target.price | minus: target.compare_at_price | times: 100.00 | divided_by: target.compare_at_price | round }}%
      {%- else -%}
        {%- if settings.currency_code_enabled -%}
          -{{ target.compare_at_price | minus: target.price | money_with_currency }}  
        {%- else -%}
          -{{ target.compare_at_price | minus: target.price | money }}  
        {%- endif -%}  
      {%- endif -%}  
    </span>
  {%- endif -%}
</div>

 

 

 

 

The problem is when I put this code in, there is no price showing. All I am trying to do is specify if customer tag = "Discounted Customer" change price to *.9

 

Can anyone help me with this?

 

Replies 0 (0)