Magento 2.3 – How to Get Quote Data on Product Detail Page

cartcustom-quotemagento2quote

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?

Best Answer

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.