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 */ 

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
        $items[] = $item->__toArray();
  • Do you need this in some controller or your are on cart phtml? – Shoaib Munir Jul 10 '19 at 11:49
  • @ShoaibMunir i need it in Checkout/Model/DefaultConfigProvider.php – summu Jul 10 '19 at 11:50
  • Or tell me the sql approach , i can get the item id from $item – summu Jul 10 '19 at 11:59
  • +1 for answer acceptance :) – Shoaib Munir Jul 10 '19 at 12:37

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()];
|improve this answer|||||
  • but i am already in another loop.. foreach ($totals->getItems() as $item) { $item->setData("base_old_price",$item->getPrice()); $items[] = $item->__toArray(); }, cant i just use a sql query appraoch by extracting item id from $item->getItemID() – summu Jul 10 '19 at 12:02
  • updated question as well – summu Jul 10 '19 at 12:04
  • Updated answer as well :) – Shoaib Munir Jul 10 '19 at 12:05
  • 1
    from $totals->getItems() you cannot get product price, even you cannot get product id, that's why you need cart helper – Shoaib Munir Jul 10 '19 at 12:07
  • 1
    hey buddy it worked, minor mistake, why you deleted your answer, just wanted to upvote and accept it – summu Jul 10 '19 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.