Menu Close
    Hire us Request quote Reading list Switch to dark mode

    How to update cart after adding product programmatically magento2

    Today we are discussing how to update mini cart after adding product in cart.

    In magento 2 when we add a product programmatically then mini cart is not updated automatically, we have to update it by the code.

    Firstly, we have to write code for adding product in cart programmatically.

    Here is a link in which we discuss about how to add product programmatically.

    Add Product in cart programmatically

    In the above blog we are using controller named: Index controller with AddProducttocart action.

    now for the cart updation we have to create a file at path:


    Add following code in this file.

    Here, in section.xml we have to define,

    action name: here in action name we define modulename_controller_action name, after which we want to update cart.

    section name: is which section you want to update, here we want to update cart section.

    and these sections are realated to customer that’s why we define Magento_Customer:etc/sections.xsd in the config tag.

    After adding this file your cart will automatically get update after hello/index/addProducttocart controller execution.

    Hope, it will help you.

    Thank you.

    . . .

    Comment (1)

    Add Your Comment

  • Hoang Trinh
    Hi, thank you for a very short workaround.
    Do you know how to do this with an API, instead of an action in controller?
    For example, if I use an API like /V1/badgemakerapi/addToCart to add product to cart (it works properly). How can I update that mini cart?
  • Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again