-1

How we can add our own section data after ajax add to cart and use it display in pupup.

I want to add my data here in the response. https://prnt.sc/q9v5tq

and when add to cart done i want to show it in popup.

in checkout/cart/add response is null.

minicart and other section used data from section load.

1 Answer 1

0

app/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="ajaxcart/cart/showPopup">
      <section name="cart"/>
   </action>
</config>

you need to create a controller named showPopup at path Controller/Cart/showPopup.php

namespace Extrembler\Ajaxcart\Controller\Cart;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Cart as CustomerCart;

class ShowPopup extends \Magento\Checkout\Controller\Cart\Add{
    protected $_coreRegistry = null;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        CustomerCart $cart,
        ProductRepositoryInterface $productRepository,
        \Magento\Framework\Registry $registry
    ) {
        parent::__construct(
            $context,
            $scopeConfig,
            $checkoutSession,
            $storeManager,
            $formKeyValidator,
            $cart,
            $productRepository
        );
        $this->_coreRegistry = $registry;
    }


    public function execute()
    {
        $params = $this->getRequest()->getParams();
        $htmlPopup = 'I am from section of extrembler';
        $result['html_popup'] = $htmlPopup;

        return $this->getResponse()->representJson(
            $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result)
        );
    }
}
6
  • only this code will enough for this?
    – MageLerner
    Dec 13, 2019 at 6:11
  • can you please post full answer please
    – MageLerner
    Dec 13, 2019 at 6:13
  • @MageLerner you need to create a controller named showPopup at path Controller/Cart/showPopup.php Dec 13, 2019 at 6:15
  • 1
    @MageLerner Check updated full answer. Dec 13, 2019 at 6:19
  • How will my action run.I have added this code but not working
    – MageLerner
    Dec 13, 2019 at 6:33

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.