Is there a way to detect a product attribute on shiping.phtml file, without using Object Manager?
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$items = $cart->getQuote()->getAllItems();
$has_appliance = false;
foreach($items as $item):
if ($item['product']['specialattribute'] == 2222):
$has_appliance = true;
break;
endif;
endforeach;
?>
Solved! Go to Solution.
I believe you should not use object manager to get object of cart model.
/** * @var \Magento\Checkout\Model\Cart
*/ private $cartFactory;
/**
* Constructor
*
* @param \Magento\Checkout\Model\Cart $cartFactory
*/
public function __construct(
\Magento\Checkout\Model\Cart $cartFactory
)
{
$this->cartFactory = $cartFactory;
}
then use factory object into your function and call your function into shipping.phtml using helper or viewModel.
$cart = $this->cartFactory->create(); $items = $cart->getQuote()->getAllItems(); return $items;
Hi @tvgarden,
You can use following way.
echo $_item->getProduct()->getCustomAttributeName(); in your case: echo $_item->getProduct()->getSpecialattribute();
I hope it will help you!
I believe you should not use object manager to get object of cart model.
/** * @var \Magento\Checkout\Model\Cart
*/ private $cartFactory;
/**
* Constructor
*
* @param \Magento\Checkout\Model\Cart $cartFactory
*/
public function __construct(
\Magento\Checkout\Model\Cart $cartFactory
)
{
$this->cartFactory = $cartFactory;
}
then use factory object into your function and call your function into shipping.phtml using helper or viewModel.
$cart = $this->cartFactory->create(); $items = $cart->getQuote()->getAllItems(); return $items;