1

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?

  • $this->session->getQuote()->getAllVisibleItems() – Rk Rathod Aug 16 at 11:00
  • getData nor returing anything so $this->session->getQuote()->getAllVisibleItems() so how this return items??? – Navin Bhudiya Aug 16 at 11:06
  • print only $checkout_data or $checkout->getAllVisibleItems() and check – Rk Rathod Aug 16 at 11:21
  • not working i try all ways this issue only on product page not on other page... i just want to print some data on product page.... on other page all working – Navin Bhudiya Aug 16 at 11:23
2

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.

  • getData nor returing anything so $this->session->getQuote()->getAllVisibleItems() so how this return items??? please read question proper and answerer – Navin Bhudiya Aug 16 at 11:07
  • 1
    Just for temporery, do cacheable=false in your layout block and check output. – Rohan Hapani Aug 16 at 11:40
  • add this into answere it working fine after set cache false – Navin Bhudiya Aug 16 at 11:45
  • It's done......!! – Rohan Hapani Aug 16 at 11:48
  • 1
    Happy to help !! Happy coding :) You can now accept answer. So, it will helpful for other also. – Rohan Hapani Aug 16 at 11:49
0

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 />";            
    }

}
  • this is also not working – Navin Bhudiya Aug 16 at 11:29
  • any error in log file ?? – Rk Rathod Aug 16 at 11:29
  • no it just return array(4) { ["is_checkout_cart"]=> bool(true) ["store_id"]=> int(1) ["remote_ip"]=> string(15) "106.201.234.169" ["x_forwarded_for"]=> NULL } – Navin Bhudiya Aug 16 at 11:31
  • i think any other issue create this probleam – Rk Rathod Aug 16 at 11:34
  • issue only on product page not on other pages... same thins are working on other pages – Navin Bhudiya Aug 16 at 11:37

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.