Shopify themes, liquid, logos, and UX
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
I currently use this code to check the stock of an item and then add the appropriate badge to give a message to the user about the availability of the stock. This works fine for normal Shopify products but does not work for bundled products. Ive tried messing around with the bundle variables but im pretty new to liquid and cant seem to get anything to work. Does anyone have a solution I can use ?
Thanks
{% liquid assign stock_level = '' if variant.inventory_quantity <= 0 assign stock_level = 'pre-order' else assign stock_level = 'in-stock' endif if inventory_display == 'hidden' assign inventory_quantity = '' endif assign is_available = false for variant in product.variants if variant.inventory_management if variant.inventory_quantity <= 0 and variant.inventory_policy == 'continue' assign is_available = true elsif variant.available assign is_available = true endif endif endfor %} {% capture stock_badge_classes %} product-stock-level product-stock-level--{{ stock_level }} {% if variant.incoming and inventory_transfer %} product-stock-level--incoming {% endif %} {% endcapture %} {% capture stock_badge_text %} {% if stock_level == 'pre-order' %} <div> <span style="color: red;">Available to order</span> </div> <div> <span style="color: red;">Contact us for ETA</span> </div> {% elsif stock_level == 'in-stock' %} <span style="color: green;">In stock</span> {% endif %} {% endcapture %} <div class="product-stock-level-wrapper" {{ attr }}> {% if variant.inventory_management %} <span class="{{ stock_badge_classes }}"> <span class="product-stock-level__text"> <div class="product-stock-level__badge-text"> {{ stock_badge_text }} </div> </span> </span> {% endif %} </div>
Hi @Jack_Bintel
Can you share your store url address? let us have a check
Hey @Jack_Bintel,
Hope you're doing fantastic.
If you're looking to make your stock-checking code work with bundled products in Shopify, you can use the following modified Liquid code. This handles both regular and bundled products by checking the inventory status of all products within a bundle.
Key changes in this code:
Before using this code, ensure your bundle products have the appropriate metafields set up. The bundle information (like products and variants) should be stored under a bundle namespace in the product metafields.
{% liquid assign stock_level = '' assign is_bundle = false assign all_products_available = true assign any_preorder = false # Check if product is a bundle if product.metafields.bundle assign is_bundle = true # Get bundle products assign bundle_products = product.metafields.bundle.products # Check availability of all products in bundle for bundle_item in bundle_products assign bundle_product = all_products[bundle_item.handle] assign bundle_variant = bundle_product.variants | where: "id", bundle_item.variant_id | first if bundle_variant.inventory_management if bundle_variant.inventory_quantity <= 0 if bundle_variant.inventory_policy == 'continue' assign any_preorder = true else assign all_products_available = false endif endif endif endfor # Set stock level based on bundle availability if all_products_available if any_preorder assign stock_level = 'pre-order' else assign stock_level = 'in-stock' endif else assign stock_level = 'out-of-stock' endif else # Regular product logic if variant.inventory_quantity <= 0 if variant.inventory_policy == 'continue' assign stock_level = 'pre-order' else assign stock_level = 'out-of-stock' endif else assign stock_level = 'in-stock' endif endif if inventory_display == 'hidden' assign inventory_quantity = '' endif %} {% capture stock_badge_classes %} product-stock-level product-stock-level--{{ stock_level }} {% if variant.incoming and inventory_transfer and stock_level != 'out-of-stock' %} product-stock-level--incoming {% endif %} {% endcapture %} {% capture stock_badge_text %} {% if stock_level == 'pre-order' %} <div> <span style="color: red;">Available to order</span> </div> <div> <span style="color: red;">Contact us for ETA</span> </div> {% elsif stock_level == 'in-stock' %} <span style="color: green;">In stock</span> {% elsif stock_level == 'out-of-stock' %} <span style="color: red;">Out of stock</span> {% endif %} {% endcapture %} <div class="product-stock-level-wrapper" {{ attr }}> <span class="{{ stock_badge_classes }}"> <span class="product-stock-level__text"> <div class="product-stock-level__badge-text"> {{ stock_badge_text }} </div> </span> </span> </div>
This solution assumes that:
If you need further explanation on setting up metafields or tweaking the logic, feel free to ask! 😊
Cheers!
Shubham | Untechnickle
Helping for free: hello@untechnickle.com
Don't forget to say thanks, it'll make my day - just send me an email!
Get Revize for Free | Let your shoppers edit orders post-purchase | Get Zero Support Tickets | #1 Order Editing + Upsell App