- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2019
09:24 AM
10-11-2019
09:24 AM
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.
Labels:
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019
12:34 AM
10-15-2019
12:34 AM
I believe you should not use object manager to get object of cart model.
- Create your custom helper or ViewModel to achieve this.
- inject factory object into constructor
/** * @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;
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2019
08:17 AM
10-13-2019
08:17 AM
Re: Get product attribute on shiping.phtml
Hi @tvgarden,
You can use following way.
echo $_item->getProduct()->getCustomAttributeName(); in your case: echo $_item->getProduct()->getSpecialattribute();
I hope it will help you!
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019
12:34 AM
10-15-2019
12:34 AM
I believe you should not use object manager to get object of cart model.
- Create your custom helper or ViewModel to achieve this.
- inject factory object into constructor
/** * @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;