Disable Add to Cart button on Out of Stock products on Wishlist Page

One requirement of many stores is that they want to disable Add to Cart button on Out of Stock products on Wishlist Page. We are soon going to incorporate this feature in the app. Till that time, the below code can be used to achieve this.

1. Create a new snippet titled wishlist-enhancements. It creates the file snippets/wishlist-enhancements.liquid

2. Add the following code to it and save it.

{% unless template %}
<script type="text/javascript">
WishlistLoadedCallback=function(){
	if($("#bookmarks").has(".addsingleproduct").length>0)
	{
		if (shared_wishlist==0) wishlist_data_source=wishlist_item_data; else wishlist_data_source=wishlist_item_data_shared;
		
		$("#bookmarks .addsingleproduct").each(function()
		{
			var wp_disable_addbutton=0;	
			var wp_jsonURL=$(this).data('url')+".json";
			var current_button=$(this);
			var current_product_id=$(this).data('productid');
			var current_variant_id=$(this).data('variantid');
			var is_chosen_variant=$(this).data("is_chosen_variant");
			var variant_count=$(this).data("variant_count");
			if(((is_chosen_variant==0)&&(variant_count==1))||(is_chosen_variant==1))
			{
						wishlist_data_source.forEach(function(wp_temp_product)
						{
							wp_temp_product.variants.forEach(function(wp_temp_variant)
							{
								if(wp_temp_variant.id==current_variant_id)
								{
									if((wp_temp_variant.inventory_quantity==0)&&(wp_temp_variant.inventory_policy=="deny"))
									{
										wp_disable_addbutton=1;
										variantids.splice(variantids.indexOf(current_variant_id),1); //fix add all
									}
								}
							});
						});
			}
			
			if(wp_disable_addbutton==1) current_button.addClass("disabled").html("OUT OF STOCK");;
				
			
		});
	}	
};
</script>
{% endunless %}

3. Add the following code anywhere before closing </head> in layout/theme.liquid.

{% include 'wishlist-enhancements' %} 

Leave a Reply

Your email address will not be published. Required fields are marked *