0

I have tried many methods to get cart items inside plugin for "Magento\ConfigurableProduct\Block\Product\View\Type\Configurable" . I am trying to add additional cart item data to the "getJsonConfig" function.

No matter what I do, the cart always seems to be empty even though there are items in the cart.

<?php
namespace VENDORNAME\VENDORMODULE\Plugin\ConfigurableProduct\Product\View\Type;

use VENDORNAME\VENDORMODULE\Helper\Data as Helper;

class Configurable
{
    protected $_helper;
    protected $_jsonDecoder;
    protected $_jsonEncoder;
    protected $_customerSession;
    protected $_checkoutSession;
    protected $_cart;
    protected $_cartHelper;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        Helper $helper,
        \Magento\Framework\Json\DecoderInterface $jsonDecoder,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Checkout\Helper\Cart $cartHelper,
        array $data = []
    ) {
        $this->_helper = $helper;
        $this->_jsonDecoder = $jsonDecoder;
        $this->_jsonEncoder = $jsonEncoder;
        $this->_customerSession = $customerSession;
        $this->_checkoutSession = $checkoutSession;
        $this->_cart = $cart;
        $this->_cartHelper = $cartHelper;
    }

    protected function _getCartProducts() {
        $cartProducts = array();
        $cartItems = $this->_checkoutSession->getQuote()->getAllItems();
        if($cartItems && sizeof($cartItems)) {
            foreach($cartItems as $item) {
                if($item->getProductType() == 'simple' && $item->getParentItemId()) {
                    $cartProducts[$item->getProductId()] = $item->getQty();
                }
            }
        }
        return $cartProducts;
    }

    public function afterGetJsonConfig($subject, $result) {
        $config = $this->_jsonDecoder->decode($result);
        $config['cart'] = $this->_getCartProducts();
        return $this->_jsonEncoder->encode($config);
    }
}

Anyone have any ideas how to retrieve the cart items here? Or why it is missing? Do I need a $cartId variable or something to load the cart first? Is it possible this has something to do with caching?

I have no idea why the cart appears empty here.

Thanks in advance

0

You can getting Cart/Quote data using this

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

// get quote items 
$itemsCollection = $cart->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

// get quote items array
$items = $cart->getQuote()->getAllItems();

foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";            
}
| improve this answer | |
  • Nope, it is still always empty. No cart items. – Cmaclean066 Nov 4 '19 at 8:23
  • have you seen items in cart page ? – Pradip Garchar Nov 4 '19 at 8:34
  • Yes, items are in cart page and mini-cart – Cmaclean066 Nov 4 '19 at 8:35
  • then it should be work as per my answer – Pradip Garchar Nov 4 '19 at 9:25
  • also you check cart item count : <?php echo count($items); ?> – Pradip Garchar Nov 4 '19 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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