cancel
Showing results for 
Search instead for 
Did you mean: 

Checkout: refresh cart items after adding a product via ajax

Checkout: refresh cart items after adding a product via ajax

Hello,

i am creating a module to add a product to the checkout cart by sku.

 

Everything is alright execpt the fact that the cart item table is not getting refreshed like the minicart or the cart-summary.

 

Template: 

<form id="addtocart" data-role="tocart-form" action="<?php echo $block->getSubmitUrl() ?>" method="post">
	<?php echo $block->getBlockHtml('formkey')?>
    <div class="row">
        <div class="col-md-6">
            <div class="row">
                <div class="col-md-6">
                    <?= __('Sku');?>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <input type="text" name="product"  placeholder="<?= __('Sku');?>">
                </div>
                <div class="col-md-2">
                    <button class="btn btn-green" type="submit"><?= __('Add');?></button>
                </div>
            </div>
        </div>
    </div>
</form>
<script type="text/x-magento-init">
    {
        "#addtocart": {
            "catalogAddToCart": {
                "bindSubmit": true
            }
        }
    }
</script>

As u can see i am using the _bindSubmit from /vendor/magento/module-catalog/view/frontend/web/js to perform the ajax request.

 

Also i've created a controller which extends the Add Controller from /vendor/magento/module-checkout/Controller/Cart and is getting called to add the product to the cart.

            $this->cart->addProduct($product, $params);
            
            if (!empty($related)) {
                $this->cart->addProductsByIds(explode(',', $related));
            }

            $this->cart->save();

Is there any way to refresh the table of the products?

 

 

Thank you in advance!