cancel
Showing results for 
Search instead for 
Did you mean: 

Get product attribute on shiping.phtml

SOLVED

Get product attribute on shiping.phtml

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;
 
?>
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Get product attribute on shiping.phtml

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;

 

View solution in original post

2 REPLIES 2

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!

Re: Get product attribute on shiping.phtml

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;