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):
- Retrieve the current date and the date of the next incoming stock transfer.
- Calculate the difference in weeks between the current date and the next incoming date.
- Determine if the product is in stock based on the current inventory and incoming stock.
- Display appropriate messages based on the stock status.
Proposed Modifications:
- Iterate through each incoming stock batch to collect the dates and quantities.
- Calculate the combined inventory considering both the current inventory and total incoming quantities.
- Determine the earliest incoming date from the collected dates.
- 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.
- Check if there’s an earliest incoming date.
- If the combined inventory is positive, display an “In stock” message.
Code Implementation (Summary):
- Initialize variables for current date, incoming dates, incoming quantities, and combined inventory.
- Iterate through incoming stock batches to collect dates and quantities.
- Calculate combined inventory considering current inventory and total incoming quantities.
- Determine the earliest incoming date.
- 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 %} Ready to ship in approximately {{ weeks_until_ship | round }} weeks ({{formatted_incoming_date}}). {% else %} In stock and ready to ship now. More incoming stock due by {{ formatted_incoming_date }}. {% endif %} {% elsif product.variants.first.inventory_quantity > 0 %} In stock and ready to ship now. {% else %} Made to order. Current lead time 10-14 weeks.</s "