I need help in getting all items that are added in cart. I’m actually showing count of each product that’s added in cart. I’ve tried current_order but it’s acting weird, sometimes it’s correct but sometimes even if cart is empty it shows the product in current_order so it’s breaking if the cart updates. Can you guys please recommend me a way to get the cart items directly?
Updated
In View
<% count = count_in_cart(simple_current_order, product) %>
<span class="badge badge-notify rounded-bottom"><%=count%></span>
In Application Helper
def count_in_cart(current_order, product)
return 0 unless current_order
product = current_order.line_items.select{|item| item.variant_id ==product.id}.last
product.present? ? product.quantity : 0
end
It works like a charm in local, but on heroku it doesn't show the count. Can someone please guide what I'm doing wrong?
Spree version is 3.7.2
Looks like the things were behaving weird in view files due to maybe objects stored in memory.
I managed to fix it by creating a new API and calculating the products in that Controller.
@cart_items_order = simple_current_order if simple_current_order.present?
render json: @cart_items_order&.line_items
That worked like a charm.
For anyone who looks for this thing in future.
Thanks.