Question

I need to get the product price from \Magento\Quote\Model\Cart\Totals\Item $item.

I tried this but it doesn't work

/** @var  \Magento\Quote\Model\Cart\Totals\Item $item */ 
$item->getProduct()->getPrice()

as getProduct() is not a function for $item. I don't need $item->getPrice() or $item->getBasePrice() .

How to get the product price.

I am already in another loop

foreach ($totals->getItems() as $item) {
        $item->getItemId() // use this id to get product price from sql query
        $item->setData("base_old_price",$item->getPrice());
        $items[] = $item->__toArray();
    }
Was it helpful?

Solution

You need cart helper for this.

add this code

protected $cartHelper;

public function __construct(
    ...
    \Magento\Checkout\Helper\Cart $cartHelper
    ...
)
{
    ...
    $this->cartHelper = $cartHelper;
    ...
}

After this you can get items and product prices from this code:

Before your loop you can get product prices against items

$itemsToPrice = array();
$cartItems = $this->cartHelper->getQuote()->getAllVisibleItems();
foreach($cartItems as $item){
    $itemsToPrice[$item->getId()] = $item->getProduct()->getPrice();
}

After that in your current loop you can get product price like this:

$price = $itemToPrice[$item->getId()];
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top