Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minicart no update with custom add to cart #3287

Closed
quienti opened this issue Feb 4, 2016 · 9 comments
Closed

Minicart no update with custom add to cart #3287

quienti opened this issue Feb 4, 2016 · 9 comments

Comments

@quienti
Copy link
Contributor

quienti commented Feb 4, 2016

Hi!

I created my custom module for add a product to my cart, if I use my controller with this form :

<form action="http://*********/test/cart/add/uenc/*******/product/471/" method="post" id="product_addtocart_form" novalidate="novalidate">

My product is in my cart, but minicart is not update, and change page not resolve this problem.

But, if I change my form link with magento controller like this :

<form action="http://*********/checkout/cart/add/uenc/*******/product/471/" method="post" id="product_addtocart_form" novalidate="novalidate">

It's work fine

In my controller, I have just change this

namespace Magento\Checkout\Controller\Cart
to this
namespace Test\Test\Controller\Cart;

@ishakhsuvarov
Copy link
Contributor

@quienti Thank you for your question.
Please try creating a sections.xml file in the etc folder of your module and configure it according to your needs, for example:

<action name="test/cart/add">
    <section name="cart"/>
</action>

This would reload customer's minicart on each POST request to your controller. Basically, action node indicates controller path and section node defines which client-side content should be updated.
Caches have to be flushed for this change to apply.
You may use Magento/Checkout/etc/frontend/sections.xml for a reference.

@quienti
Copy link
Contributor Author

quienti commented Feb 4, 2016

Whoaa!
Awesome ! Thanks you !

@quienti quienti closed this as completed Feb 4, 2016
@pankaj-pareek
Copy link

@ishakhsuvarov - sections.xml will only work for Post request ? As in GET request it is not working. any solution for this.

@ishakhsuvarov
Copy link
Contributor

@pankaj-pareek It is designed only to be used on a state-changing requests, which GET isn't.

@orlangur
Copy link
Contributor

orlangur commented Aug 3, 2017

@pankaj-pareek POST and PUT to be clear. Any code changing system state in GET request violates CQRS and needs to be reworked.

@pankaj-pareek
Copy link

Thanks for clarification. I will make changes in the flow to add products programmatically using post request.

@lano-vargas
Copy link

lano-vargas commented Sep 24, 2018

Magetno 2.2.2 Xml Session doesn't work e.g
<action name="test/cart/add"> <section name="cart"/> </action>
. It works only with required e.g
<script> require([ 'Magento_Customer/js/customer-data' ], function (customerData) { setTimeout(function(){ customerData.reload(['cart']); }, 1000); }); </script>

is there a bug that it doesn't reload with sections.xml?

@orlangur
Copy link
Contributor

@lano-vargas section should be enough if you access this action with post/put/delete http method.

@lano-vargas
Copy link

@orlangur Thank you.

I am injecting the \Magento\Checkout\Model\Cart then:

    .....
    try {

        $_product = $this->productRepository->getById($productId);

        $this->_cart->addProduct($_product, $params);

        $this->_cart->save();

        $this->_messageManager->addSuccessMessage(__('You added '.$_product->getName().' to your shopping cart.'));

        // Perhaps here reload cart customer data E.G $this->_cart->reload('cart')

    }catch (\Exception $e){
        $this->_messageManager->addError($e->getMessage());
    }
   .....

Magento doesn't happen to have a method to reload customer data? E.G
$this->_cart->reload('cart') similar to requireJS

magento-engcom-team pushed a commit that referenced this issue Oct 12, 2018
[performance] performance improvements
### Tasks:
* MAGETWO-91388: Enable caching of AJAX requests on Varnish/Fastly
* MAGETWO-94671: Product Export fails
* MAGETWO-95412: [2.3] Add missed dependencies for catalogsearch_fulltext indexer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants