0

How to get value from local storage of product weight and id in magento 2.

enter image description here

  • In which page you want to get? – Rohan Hapani Oct 22 '18 at 8:59
  • View cart or shopping cart page – Rohan Hapani – trilok kumar Oct 22 '18 at 9:04
  • Check my answer. – Rohan Hapani Oct 22 '18 at 10:01
0

You should use \Magento\Checkout\Model\Cart class to get the quote and your cart items details.


Using construct method :

protected $_cart;

public function __construct(
    ...
    \Magento\Checkout\Model\Cart $cartModel,
    ...
) {
    ...
    $this->_cart = $cartModel;
    ...
}

public function yourFunction()
{
    $items = $this->_cart->getQuote()->getAllItems();
    foreach($items as $item) {
        echo 'ID : '.$item->getProductId().'<br />';
        echo 'Weight : '.$item->getWeight().'<br />';
    }
}

using Object Manager :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

$itemsCollection = $cart->getQuote()->getItemsCollection(); // get quote items collection

$itemsVisible = $cart->getQuote()->getAllVisibleItems(); // get array of all items what can be display directly

$items = $cart->getQuote()->getAllItems(); // get quote items array

foreach($items as $item) {
    echo 'ID : '.$item->getProductId().'<br />';
    echo 'Weight : '.$item->getWeight().'<br />';
}
  • Thanks for comment Rohan Hapani can see image i have uploaded i need to display product weight there above subtotal – trilok kumar Oct 22 '18 at 10:06
  • are you able to get product weight on cart page ? – Shashank Kumrawat Oct 22 '18 at 11:41
  • By this code, it will be get value. But, he want to get in minicart. – Rohan Hapani Oct 22 '18 at 11:51

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.