• Hi guys,

    I’ve written a custom plugin which adds variations to the cart. This happens in the ‘init’ hook. It’s working perfectly except that the cart in the header of the page displays every item I add via this method at double its cost.

    e.g. If I already have an item in cart which was $50 and then use this method to add an item for $20, the cart in the header shows 2 items – $90.
    It’s correctly showing the number of items but incorrectly doubling the $value of the item I added using add_to_cart.

    It seems to be getting updated via JavaScript, because I notice briefly when the page loads that the amount shown is correct, then it quickly changes. If I disable JavaScript, it shows the correct amount and doesn’t change.
    I’m guessing it’s something to do with WooCommerce fragments but I have no idea where to find the issue…

    Does anyone know what I should be doing in ‘init’ to stop this happening or somewhere else?

    thanks
    Aaron

    https://www.ads-software.com/plugins/woocommerce/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Hey Aaron,

    I think the solution would be to not hook into init. Maybe try hooking into somewhere much later. Also look at the methods you are using to add a product to the cart and then look at what core does and see if you are doing it wrong.

    Thread Starter aaronlp

    (@aaronlp)

    Hi Caleb, thanks for your reply, I appreciate it, although I think your suggestions are just asking me to perform trial and error as opposed to a solution or basis for your reasoning.

    Do you have any more info as to why this shouldn’t be done in the init hook?
    I have checked WC core and am adding the item to the cart in the same way they do it. The cart also looks prefectly fine in all other places except the nav header so I’m more inclined to think this is some cart fragments weirdness…

    Thanks
    Aaron

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    I think your suggestions are just asking me to perform trial and error as opposed to a solution or basis for your reasoning.

    Yep, that would be the point :). I can’t help everybody by coding what they need. Purpose of the forums it to help guide in the right direction.

    I’m more inclined to think this is some cart fragments weirdness

    It could very well be cart fragments weirdness. Could you post your system status report? https://www.ads-software.com/support/topic/before-posting-gather-the-following-details

    Try using the Storefront theme and see if the same errors occur.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add_to_cart makes basket/cart total double’ is closed to new replies.