-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Closed
Labels
Component: CatalogIssue: Format is validGate 1 Passed. Automatic verification of issue format passedGate 1 Passed. Automatic verification of issue format passedbug report
Description
Preconditions
- Magento 2.1 CE, php 7.0
Steps to reproduce
- In custom module I added block, which should check if provided product is in cart.
- Block is added properly, although method doesn't work.
- When I try to check if my product is in cart I get info that cart is empty (but in fact it is not). That is my class:
namespace [vendor]\SkinHelper\Block\Catalog\Product;
use \Magento\Catalog\Api\Data\ProductInterface;
use \Magento\Checkout\Model\Session;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
/** Class provides method to check if given product is in cart
* @universal
*/
class IsInCart extends Template
{
/**
* @var \Magento\Checkout\Model\Session
*/
private $checkoutSession;
/**
* Di.
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
*/
public function __construct(
Session $checkoutSession,
Context $context,
array $data
)
{
parent::__construct($context, $data);
$this->checkoutSession = $checkoutSession;
}
/**
* Method check if provided product is in cart of current customer.
* Doesn't work dynamically.
* TODO: There is a bug actually in magento connected with session and full page cache
* TODO: More details: https://github.com/magento/magento2/issues/3294
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
*
* @return bool $inCart
*/
public function isInCart(ProductInterface $product)
{
$productId = $product->getId();
$cartItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
$itemsIds = array();
foreach ($cartItems as $cartItem) {
$itemsIds[] = $cartItem->getProduct()->getId();
}
return in_array($productId, $itemsIds);
}
/**
* Method counts items now available in cart.
* TODO: There is a bug actually in magento connected with session and full page cache
* TODO: More details: https://github.com/magento/magento2/issues/3294
*
* @return int $cartDataCount
*/
public function countItemsInCart()
{
$cartItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
$cartItemsCount = count($cartItems);
return $cartItemsCount;
}
- That's my call for these methods:
`isInCart($_product); ?>
helper([vendor]\SkinHelper\Helper\Test')->isInCart($_product) ?>`As you can see I was trying the same thing with helper (called from helper), but it failed.
- I was trying also the same way on
\Magento\Checkout\Model\Cartand it also didn't worked. - The strangest thing is that the same logic placed and called as controller (by url) works and returns correct output.
Expected result
- Of course, method in block called inside template returns correct result.
Actual result
- When I dump
$itemsIdsit is empty, so no elements can be retrieved. Products are present in minicart, as well as inquotetable as well. - Method works in controller but not inside block or helper called from template. What is even more strange the same method in helper called from controller works.
Metadata
Metadata
Assignees
Labels
Component: CatalogIssue: Format is validGate 1 Passed. Automatic verification of issue format passedGate 1 Passed. Automatic verification of issue format passedbug report