We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Request for Liquid Code Modification to Accurately Display Product Availability

Request for Liquid Code Modification to Accurately Display Product Availability

BackckountryRec
Visitor
2 0 0
 
Objective: Modify the stock status display logic on the product page in Shopify to accurately reflect the availability of stock considering incoming stock transfers.
 

Current Logic (Summary):

  1. Retrieve the current date and the date of the next incoming stock transfer.
  2. Calculate the difference in weeks between the current date and the next incoming date.
  3. Determine if the product is in stock based on the current inventory and incoming stock.
  4. Display appropriate messages based on the stock status.

Proposed Modifications:

  1. Iterate through each incoming stock batch to collect the dates and quantities.
  2. Calculate the combined inventory considering both the current inventory and total incoming quantities.
  3. Determine the earliest incoming date from the collected dates.
  4. If the combined inventory is zero or negative:
    • Check if there's an earliest incoming date.
      • If yes, display a "Ready to ship" message with the earliest incoming date.
      • If no, display a "Made to order" message with the standard lead time.
  5. If the combined inventory is positive, display an "In stock" message.

Code Implementation (Summary):

  1. Initialize variables for current date, incoming dates, incoming quantities, and combined inventory.
  2. Iterate through incoming stock batches to collect dates and quantities.
  3. Calculate combined inventory considering current inventory and total incoming quantities.
  4. Determine the earliest incoming date.
  5. Based on combined inventory and incoming dates:
    • Display appropriate stock status messages.

The below code is working pretty good, just doesn't take into account if the very next stock transfer is spoken for and to look ahead to which stock transfer the customer will be able to reserve product on. I have attached the HTML for reference

 

" {% assign current_date = 'now' | date: '%s' %} {% assign incoming_date = product.variants.first.next_incoming_date | date: '%s' %} {% assign seconds_in_week = 604800 %} {% if incoming_date != blank %} {% assign difference_in_seconds = incoming_date | minus: current_date %} {% assign weeks_until_ship = difference_in_seconds | divided_by: seconds_in_week | plus: 1 %} {% assign formatted_incoming_date = product.variants.first.next_incoming_date | date: "%B %d, %Y" %} {% assign combined_inventory = product.variants.first.inventory_quantity %} {%- comment -%} Loop through incoming stock batches to calculate the total incoming quantity. {%- endcomment -%} {% for stock_batch in product.variants.first.incoming_stock_batches %} {% assign combined_inventory = combined_inventory | plus: stock_batch.quantity %} {% endfor %} {% if combined_inventory <= 0 %} <span style="font-family: 'Lato', sans-serif; font-size: 16px;">Ready to ship in approximately {{ weeks_until_ship | round }} weeks ({{formatted_incoming_date}}).</span> {% else %} <span style="font-family: 'Lato', sans-serif; font-size: 16px; color: #228B22;">In stock and ready to ship now. More incoming stock due by {{ formatted_incoming_date }}.</span> {% endif %} {% elsif product.variants.first.inventory_quantity > 0 %} <span style="font-family: 'Lato', sans-serif; font-size: 16px; color: #228B22;">In stock and ready to ship now.</span> {% else %} <span style="font-family: 'Lato', sans-serif; font-size: 16px;">Made to order. Current lead time 10-14 weeks.</s "
Reply 1 (1)

BackckountryRec
Visitor
2 0 0

Sorry here is the updated code we have been using

 

"

{% assign current_stock_level = product.variants.first.inventory_quantity %}
{% assign backordered_quantity = 0 %}

{% if current_stock_level < 0 %}
{% assign backordered_quantity = current_stock_level | abs %}
{% endif %}

{% assign cumulative_stock = current_stock_level %}
{% assign next_available_date = blank %}
{% assign found_stock = false %}

{% for stock in product.variants.first.incoming_stock %}
{% if backordered_quantity > 0 %}
{% assign cumulative_stock = cumulative_stock | plus: stock.quantity %}
{% if cumulative_stock >= 0 %}
{% assign next_available_date = stock.date %}
{% assign found_stock = true %}
{% break %}
{% else %}
{% assign backordered_quantity = backordered_quantity | minus: stock.quantity %}
{% endif %}
{% endif %}
{% endfor %}

{% if found_stock %}
{% assign formatted_incoming_date = next_available_date | date: "%B %d, %Y" %}
<span style="font-family: 'Lato', sans-serif; font-size: 16px;">Backordered. Estimated restock date: {{ formatted_incoming_date }}.</span>
{% elsif current_stock_level > 0 %}
<span style="font-family: 'Lato', sans-serif; font-size: 16px; color: #228B22;">In stock and ready to ship now.</span>
{% else %}
<span style="font-family: 'Lato', sans-serif; font-size: 16px;">Currently out of stock. Check back soon for updates.</span>
{% endif %}"