How to detect have ReCharge’s subscription product in Shopify cart
You may want to detect have ReCharge’s subscription product in cart to do something. This article provides the code to help you to do that.
ReCharge is the leading subscriptions payments platform designed for Shopify merchants to set up and manage dynamic recurring billing across web and mobile. |
This is the Shopify liquid code
1 2 3 4 5 6 7 8 9 10 11 | {% assign has_subscription_product = false %} {% for item in cart.items %} {% if has_subscription_product %}{% break %}{% endif %} {% for p in item.properties %}{% if p.first == "shipping_interval_frequency" %}{% assign has_subscription_product = true %}{% break %}{% endif %}{% endfor %} {% endfor %} {% if has_subscription_product %} //Has subscription product in cart {% else %} //No have subscription product in cart {% endif %} |
This is the Javascript code with jQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $.getJSON('/cart', function (results) { var has_subscription_product = false; $.each(results.items, function (i, item) { if( !has_subscription_product ){ if(item.properties!== null){ $.each(item.properties, function (j, property) { if(j=='shipping_interval_frequency'){ has_subscription_product = true; } }); } } }); if( has_subscription_product ){ //Has subscription product in cart }else{ //No have subscription product in cart } }); |
I hope it’s helpful for you.
Happy coding!