i want to get this quote data on product page but that not return any data ... only return below array instead of all quote data

On other page it working fine issue only on product page.

array(4) 
{

    ["is_checkout_cart"]=>
    bool(true)
    ["store_id"]=>
    int(1)
    ["remote_ip"]=>
    string(15) "106.201.234.169"
    ["x_forwarded_for"]=>
    NULL
}

Here is my code

<?php
namespace Vendor\Base\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
use Magento\Checkout\Model\Session;
class Data extends AbstractHelper {
    protected $scopeConfig;
    protected $session;
    protected $priceHelper;
    public function __construct(Context $context,Session $session,\Magento\Framework\Pricing\Helper\Data $priceHelper) {
        parent::__construct($context);
        $this->scopeConfig = $context->getScopeConfig();
        $this->session=$session;
        $this->priceHelper=$priceHelper;
    }
    public function getCheckoutData(){
        $data_array=array();
        $checkout_data=$this->session->getQuote();
        var_dump($checkout_data->getData());exit;

    }
}

anyone have idea whats wrong in my code?

有帮助吗?

解决方案

Try to use this below code :

protected $_checkoutSession;

public function __construct (
    \Magento\Checkout\Model\Session $_checkoutSession
    ) {
    $this->_checkoutSession = $_checkoutSession;
}

public function execute()
{
  $cartData = $this->_checkoutSession->getQuote()->getAllVisibleItems();
  echo count($cartData); //Return count value of quote object
}

For temporary :

Do cacheable=false in your layout file and check it.

Still, I do not recommend to use this.

其他提示

Try This :-

Using \Magento\Checkout\Model\Cart :-

protected $cart;

public function __construct (
    \Magento\Checkout\Model\Cart $cart
    ) {
    $this->cart = $cart;
}

public function execute()
{
    // retrieve quote items collection
    $itemsCollection = $this->cart->getQuote()->getItemsCollection();

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

    // retrieve quote items array
    $items = $this->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 />";            
    }

}
许可以下: CC-BY-SA归因
scroll top