Why does my item count in cart show one more than actual?

Hello,

I have a small problem, from my homepage when I add to cart the product, in the cart at the top the item number is wrong. Whatever the number of products you add, the item number in the cart is always N+1 and if you delete every product in the cart, the item number stays at 1.

This problem occurs only when adding from the “featured product” section in my homepage.

www.brightnuve.com

Thanks

That’s interesting. Try these two things and let me know the results:

  1. Delete the section and add it again.

  2. Try using your website on another browser.

Indeed. Tried both, without result. Thx anyway

@Tiago10 ,I think there is some code glitch with AJAX on the home page. After refreshing the page it’ll display the correct count.

Could you share the JS/liquid code of the side cart drawer?

Do you have any apps that could be affecting it?

Or, have you made some changes in the past to your code?

Yes I noticed it.
Sure here you have :

{%- liquid
assign subtotal = cart.total_price | plus: 0
assign original_subtotal = cart.original_total_price | plus: 0
assign cart_items_size = cart.item_count
assign cart_discount_size = cart.cart_level_discount_applications.size
assign continue_shopping_link = routes.all_products_collection_url
assign show_continue_shopping = settings.show_continue_shopping
assign show_empty_cart_buttons = settings.show_empty_cart_buttons
assign empty_cart_buttons = settings.empty_cart_buttons | default: ‘main-menu’

if settings.continue_shopping_link != blank
assign continue_shopping_link = settings.continue_shopping_link
endif
-%}

{% if settings.cart_custom_message_text != blank %}
{{ settings.cart_custom_message_text }}
{% endif %}

{{ 'cart.general.title' | t }}{{ 'cart.label.items_with_count' | t: count: cart_items_size }}

{%- render 'icon-close-small' -%}
{%- liquid assign free_shipping_text = block.settings.message | default: settings.message assign is_enable = false

if block.settings.message != blank
assign is_enable = true
endif

if settings.show_free_shipping_message and settings.free_shipping_limit != blank and free_shipping_text != blank
assign is_enable = true
endif
-%}

{%- if is_enable -%}
{%- liquid
assign limit = settings.free_shipping_limit | plus: 0
assign limit_currency = limit | times: 100
assign subtotal_without_currency = cart.total_price | plus: 0 | divided_by: 100
-%}

{%- capture left_to_spend -%}
{{ limit_currency | minus: cart.total_price | money_without_trailing_zeros }}
{%- endcapture -%}

{%- liquid
assign free_shipping_message = free_shipping_text | replace: ‘||amount||’, left_to_spend
assign qualified_shipping_message = ‘cart.general.qualified_shipping_message’ | t
assign class_message = ‘’

if subtotal_without_currency >= limit
if qualified_shipping_message != blank
assign class_message = ’ is-success’
assign is_default_message_hidden = ’ is-hidden’
else
assign class_message = ’ is-hidden’
endif
elsif subtotal_without_currency == 0
assign class_message = ’ is-hidden’
endif

assign percent = limit | minus: subtotal_without_currency | times: 100 | divided_by: limit
assign percent = 100 | minus: percent
-%}

{{ free_shipping_message }}

{%- if qualified_shipping_message != blank -%}
{{ qualified_shipping_message }}
{%- endif -%}

{%- unless is_top -%}

{%- endunless -%}

{%- endif -%}

{{- 'cart.general.limit_error' | t -}}

{%- render 'icon-close-small' -%}
{%- render 'cart-line-items' -%}
{{- 'cart.general.empty' | t -}}
{%- if show_continue_shopping and show_empty_cart_buttons == false -%} {{ 'cart.general.continue_shopping' | t }} {%- endif -%}

{%- if show_empty_cart_buttons and linklists[empty_cart_buttons].links.size > 0 -%}
{%- for link in linklists[empty_cart_buttons].links limit: 3 -%}
{{ link.title | strip_html | escape }}
{%- endfor -%}
{%- endif -%}

{%- if settings.enable_cart_notes or settings.enable_gift_notes -%}
{%- endif -%}

{%- if settings.enable_cart_notes -%}

{{- 'cart.general.cart_notes_label' | t -}}
{{ 'cart.general.customer_note' | t }} {{- cart.note -}}
{%- endif -%}

{%- if settings.enable_gift_notes -%}

{{- 'cart.general.gift_notes_label' | t -}}
{%- assign gift_note_attribute = 'cart.general.gift_note_attribute' | t -%} {{ 'cart.general.note' | t }} {{ cart.attributes[gift_note_attribute] }}
{%- endif -%}

{%- if settings.enable_cart_notes or settings.enable_gift_notes -%}

{%- endif -%}
{{- 'cart.general.subtotal_items' | t -}} {%- if original_subtotal == 0 -%} {{ 'products.product.free' | t }} {%- else -%} {{- original_subtotal | money -}} {%- endif -%}
{%- if cart.cart_level_discount_applications.size > 0 -%} {%- for discount in cart.cart_level_discount_applications -%}
{{- discount.title -}} -{{- discount.total_allocated_amount | money -}}
{%- endfor -%} {%- endif -%}
{%- if settings.enable_accept_terms -%} {{ settings.accept_terms_text }} {%- endif -%}

{{- 'cart.general.checkout' | t -}} {%- if subtotal == 0 -%} {{ 'products.product.free' | t }} {%- else -%} {{- subtotal | money -}} {%- endif -%}

{%- if settings.enable_additional_buttons and additional_checkout_buttons -%}

{{- content_for_additional_checkout_buttons -}}
{%- endif -%}

{{- 'cart.general.shipping_at_checkout' | t -}}

None of that. It must come from the original theme code.

There also a liquid file called " cart-line-items ", it might be this one glitched.

Here you have :

{%- assign pair_products = ‘’ -%}
{%- assign cart_products = ‘’ -%}

{%- for line_item in cart.items -%}

{{- line_item.product.title | strip_html -}}

{%- unless line_item.product.has_only_default_variant -%} {%- for option in line_item.options_with_values -%}

{{- option.name -}}: {{- option.value -}}

{%- endfor -%} {%- endunless -%}

{%- if line_item.selling_plan_allocation -%}

{{ line_item.selling_plan_allocation.selling_plan.name }}

{%- endif -%}

{%- assign property_size = line_item.properties | size -%}
{%- if property_size > 0 -%}
{%- for p in line_item.properties -%}
{%- assign property_first_char = p.first | slice: 0 -%}
{%- if p.last != blank and property_first_char != ‘_’ -%}

{{ p.first }}: {%- if p.last contains '/uploads/' -%} {{ p.last | split: '/' | last }} {%- else -%} {{ p.last }} {%- endif -%}

{%- endif -%} {%- endfor -%} {%- endif -%}

{%- assign discounted = false -%} {%- assign sale = false -%}

{%- if line_item.original_price > line_item.final_price -%}
{%- assign discounted = true -%}
{%- endif -%}

{%- if line_item.variant.compare_at_price > line_item.price -%}
{%- assign sale = true -%}
{%- endif -%}

{%- if sale or discounted -%}

{%- if line_item.final_price == 0 -%}
{{ ‘products.product.free’ | t }}
{%- else -%}
{{- line_item.final_price | money_with_currency -}}
{%- endif -%}

{%- else -%}
{%- if line_item.final_price == 0 -%}
{{ ‘products.product.free’ | t }}
{%- else -%}
{{- line_item.final_price | money_with_currency -}}
{%- endif -%}
{%- endif -%}

{%- if sale and discounted == false -%}
{{- line_item.variant.compare_at_price | money_with_currency -}}
{%- endif -%}

{%- if discounted -%}
{{- line_item.original_price | money_with_currency -}}
{%- endif -%}

{%- if line_item.unit_price -%}
{%- capture unit_price_separator -%}
/{{ ‘general.accessibility.unit_price_separator’ | t }} 
{%- endcapture -%}

{%- capture unit_price_base_unit -%}
{%- if line_item.unit_price_measurement.reference_value != 1 -%}
{{- line_item.unit_price_measurement.reference_value -}}
{%- endif -%}
{{- line_item.unit_price_measurement.reference_unit -}}
{%- endcapture -%}

{{ 'products.product.unit_price_label' | t }} {% if line_item.unit_price == 0 %}{{ 'products.product.free' | t }}{% else %}{{ line_item.unit_price | money_with_currency }}{% endif %} {{ unit_price_separator }} {{ unit_price_base_unit }} {%- endif -%}

{{ 'general.accessibility.decrease' | t }} {%- render 'icon-toggle-minus' -%}

{%- assign quantity = line_item.quantity -%}

{%- if line_item.variant.inventory_policy == ‘deny’ and line_item.variant.inventory_management != nil and line_item.variant.inventory_quantity < quantity -%}
{%- assign quantity = line_item.variant.inventory_quantity -%}
{%- endif -%}


{{ ‘general.accessibility.increase’ | t }}
{%- render ‘icon-toggle-plus’ -%}

{{ 'cart.general.remove' | t }}

{%- if line_item.original_price > line_item.final_price -%}
{%- for discount in line_item.line_level_discount_allocations -%}

{%- render 'icon-tags' -%} {%- assign discount_price = discount.amount | money_with_currency -%}

{{ ‘cart.general.discount_label’ | t: amount: discount_price, title: discount.discount_application.title }}

{%- endfor -%} {%- endif -%}

{%- assign cart_products = cart_products | append: line_item.product.id | append: ‘,’ -%}
{%- assign upsell = line_item.product.metafields.theme.upsell -%}

{%- if upsell.value != nil and upsell.type == ‘product_reference’ -%}
{%- assign pair_products = pair_products | append: forloop.index0 | append: ‘,’ -%}
{%- endif -%}

{%- endfor -%}

{%- if pair_products != ‘’ -%}
{%- liquid
assign upsell_products = ‘’
assign pair_products = pair_products | split: ‘,’
assign cart_products = cart_products | prepend: ‘,’

for index in pair_products
assign arr_index = index | plus: 0
assign upsell_product = cart.items[arr_index].product.metafields.theme.upsell.value

for variant in upsell_product.variants
if variant.available
assign upsell_product_variant = variant.id
break
endif
endfor

if cart_products contains upsell_product.id or upsell_product.available == false
continue
endif

unless upsell_products contains upsell_product.handle
assign upsell_products = upsell_products | append: upsell_product.handle | append: ‘_’ | append: upsell_product_variant | append: ‘,’
endunless
endfor
-%}

{%- endif -%}