1
    class Productinfo extends \Magento\Framework\View\Element\Template
    {    
       protected $checkoutSession;             
       public function __construct(
          \Magento\Backend\Block\Template\Context $context,  
          \Magento\Checkout\Model\SessionFactory $checkoutSession,  
           array $data = []
       )
     {
          $this->checkoutSession = $checkoutSession;           
          parent::__construct($context, $data);
      }

     /*
      * Checkout Session Data
     */
      public function getCheckoutSession()
      {
         return $this->checkoutSession->create();
      }
    }   

This is block file function when I use the this function it gives data of last quoteId not current when cache is on.Please help me with this

1

3 Answers 3

2

Try to use this below code :

Method 1 :

protected $checkoutCartBlock;

public function __construct(
  \Magento\Backend\Block\Template\Context $context,  
  \Magento\Checkout\Block\Cart $checkoutCartBlock,  
   array $data = []
)
{
  $this->checkoutCartBlock = $checkoutCartBlock;           
  parent::__construct($context, $data);
}

/*
* Checkout Quote Data
*/
public function getCheckoutSession()
{
 return $this->checkoutCartBlock->getQuote();
}

Method 2 :

protected $checkoutSession;

public function __construct(
  \Magento\Backend\Block\Template\Context $context,  
  \Magento\Checkout\Model\SessionFactory $checkoutSession  
   array $data = []
)
{
  $this->checkoutSession = $checkoutSession;           
  parent::__construct($context, $data);
}

/*
* Checkout Quote Data
*/
public function getCheckoutSession()
{
 return $this->checkoutSession->create()->getQuote();
}

To get quote id : $block->getCheckoutSession()->getId();

Remove generated and clean cache.

2
  • Tried both but have to clear the cache every time to get current quote id
    – Tanmay
    Oct 26, 2020 at 10:11
  • This things happen every time .We can't refresh the cache every time that is the problem.
    – Tanmay
    Oct 26, 2020 at 15:07
0

Try this

protected $cart;

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

public function getCartItems()
{
    $cart_data = $this->cart->getQuote()->getAllVisibleItems();
    foreach ($cart_data as $item) {
        print_r($item);
    }
}

Call getCartItems() function into phtml file.

5
  • Tried but have to clear the cache every time to get current quote id
    – Tanmay
    Oct 26, 2020 at 10:11
  • try this print_r($item->debug()); this show all data.
    – Msquare
    Oct 26, 2020 at 10:19
  • It shows data but the cached data of previous quote no current quote.This is something related to FPC
    – Tanmay
    Oct 26, 2020 at 11:04
  • have checked ? above code always return current Quote data.
    – Msquare
    Oct 26, 2020 at 11:15
  • This get data of previous from cache.
    – Tanmay
    Oct 26, 2020 at 15:07
0

if it is a custom block and you are facing an issue with cache then try something like this

<block class="Something\Something\Something\Class" name="blockname" template="Something_Class::something.phtml" cacheable="false" />

A single cacheable="false" will disable Full Page Caching for the whole page, making the pages sourcing from that layout file extremely slow! Check https://inviqa.com/blog/how-full-page-cache-works-magento-2

3

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.