5

I'm working on a task that need an API endpoint for adding product to cart in Magento 2.

My code do the job "add product to cart" properly (it add product to cart and when I go to .../checkout/cart I can see it.

$params = array('product' => $cardId, 'qty' => 1);

$product = $this->productRepository->getById($cardId);
$this->cart->addProduct($product, $params);
$this->cart->save();

But I need to update the mini cart also. I searched google and this is the best solution that I found:

Create file etc/frontend/sections.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="rest/V1/badgemakerapi/addToCart">
        <section name="cart"/>
    </action>
</config>

In the tutorial, the part

name="rest/V1/badgemakerapi/addToCart"

is actually

name="frontname/controller/action"

but because I don't have controller, I have to change it to API route

I noticed that in Checkout module, magento 2 core team does the same thing:

    <action name="rest/*/V1/carts/*/payment-information">
        <section name="cart"/>
        <section name="checkout-data"/>
        <section name="last-ordered-items"/>
    </action>
    <action name="rest/*/V1/guest-carts/*/payment-information">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
    <action name="rest/*/V1/guest-carts/*/selected-payment-method">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
    <action name="rest/*/V1/carts/*/selected-payment-method">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>

The problem is I can not update mini cart using this method.

Anyone has any idea?

Thank you very much.

2
  • In this case, are you making the addToCart API call from the client's web page, and you want to update the minicart badge and items without reloading the page? Or is the issue that even after reloading the page, the minicart still isn't updated?
    – thaddeusmt
    Commented Jun 13, 2017 at 1:20
  • @thaddeusmt: It's the issue even after I manually reload the page, the mini cart is still the same. I think it will be considered a bug in testing phase. Thank you.
    – 0xh8h
    Commented Jun 13, 2017 at 2:58

1 Answer 1

1

I am now running into this same issue. It appears to be related to the way the cart is cached in the browser's "local storage". I don't actually have a solution yet, unfortunately, but clearing the local storage in Chome refreshed my minicart.

There are some open bugs in Magento's GitHub:

It looks like a long standing bug. Hopefully they will fix it soon.

Here are some related SE questions I'm working from right now:

My (current) solution is to request that the minicart component refresh by default on page load. This way if the user updates the cart via an API request somewhere else, when they reload the Magento site the cart refreshes.

Specifically, I override the minicart.js file and modified it to force the customerData to refresh in the initialize method every page load:

initialize: function () {
    var self = this,
        cartData = customerData.get('cart');
    ....
    // always clear minicart local storage cache, since we might be updating it via API call
    customerData.reload(['cart'], false);
    // if (cartData().website_id !== window.checkout.websiteId) {
    //     customerData.reload(['cart'], false);
    // }
    return this._super();
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.