cancel
Showing results for 
Search instead for 
Did you mean: 

Ajax cart update won't work second time

SOLVED

Ajax cart update won't work second time

I have created a custom module to display a form to update custom options on Cart. The code works when I created a button, however with Ajax it doesn't work second time. It seems it's not updating custom options in back end. Only prices are updated. So Ajax call works only once, second time it does not work. Is there any suggestions to update custom options values?

public function execute()
    {

        $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->addInfo("execute");
          /** @var \Magento\Framework\Controller\Result\Json $resultJson */
      
        if (!$this->_formKeyValidator->validate($this->getRequest())) {
         //   return $this->resultRedirectFactory->create()->setPath('*/*/');
        }
   
        $updateAction = (string)$this->getRequest()->getParam('update_cart_action');
    

        switch ($updateAction) {
            case 'empty_cart':
                $this->_emptyShoppingCart();
       
            break;
            case 'update_qty':
                $this->_updateShoppingCart();
             
            break;
            case 'update_ajax_options':     
      
            $this->_updateShoppingCart();
            return $this->_getTotalsHtml();
            break;     
            default:
                $this->_updateShoppingCart();
             
        }
        return $this->_goBack();
    }
 

 protected function _getTotalsHtml()
    {

        $response2 = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
        $layout = $response2->addHandle('checkout_cart_index')->getLayout();
        $response['cartcontent'] = $this->_view->getLayout()->getBlock('checkout.cart.form')->toHtml();
        $response['checkout_method'] = $this->_view->getLayout()->getBlock('checkout.cart.methods.bottom')->toHtml();
        $response['page_messages'] = $this->_view->getLayout()->getBlock('checkout.cart.validationmessages')->toHtml();
       // $response['cart_item'] = $this->_view->getLayout()->getBlock('checkout.cart.item')->toHtml();
        $response['content'] = $layout->renderNonCachedElement('checkout.cart.empty');
        $cartQuote= $this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals();
        $response['subtotaldata'] = $this->_objectManager->create('Magento\Checkout\Model\DefaultConfigProvider')->getConfig();
        $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultJson->setData($response);
        
        
        $this->_checkoutSession->getQuote()->collectTotals()->save();
        
        return $resultJson;

    
}
    protected function  _updateAjaxOptions(){
        $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->addInfo("_updateAjaxOptions");
        $this->_updateShoppingCart();
       
        return;
 
    }
 protected function _updateShoppingCart()
    {

        $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->addInfo("_updateShoppingCart");
  
        try {
            $cartData = $this->getRequest()->getParam('cart');
   
            $this->_checkoutSession->setMyValue($cartData);

            if (is_array($cartData)) {
                $filter = new \Zend_Filter_LocalizedToNormalized(
                    ['locale' => $this->_objectManager->get(
                        \Magento\Framework\Locale\ResolverInterface::class
                    )->getLocale()]
                );
                foreach ($cartData as $index => $data) {
                    if (isset($data['qty'])) {
                        $cartData[$index]['qty'] = $filter->filter(trim($data['qty']));
                    }
                }
                if (!$this->cart->getCustomerSession()->getCustomerId() && $this->cart->getQuote()->getCustomerId()) {
                    $this->cart->getQuote()->setCustomerId(null);
                }
        
                $cartData = $this->cart->suggestItemsQty($cartData);
                $this->cart->updateItems($cartData)->save();

                $this->_reloadCart();

                $this->_checkoutSession->setMyValue($cartData);
                
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError(
                $this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($e->getMessage())
            );

        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('We can\'t update the shopping cart.'));
            $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
   
        }
    
    }



 Screen Shot 2018-04-13 at 10.23.28.png

2 REPLIES

Re: Checkoutsession not updated after Ajax call

Is there anyone can help?

Re: Checkoutsession not updated after Ajax call

Sorry this one became a duplicate as was reported as spam this morning.

It's solved in https://community.magento.com/t5/Magento-2-x-Programming/Ajax-does-not-work-second-time/m-p/91935#M4...