Skip to content

Total cart Weight how to show order summary in magento 2.4.6

How can We achieve this?

enter image description here

We get total weight of products of cart in Magento 2 by MagentoCheckoutModelCart class

With objectManager

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$items = $cart->getQuote()->getAllItems();

$weight = 0;
foreach($items as $item) {
   $weight += ($item->getWeight() * $item->getQty()) ;        
}

echo $weight;

With Factory Method

  protected $_cart;

  public function __construct(
  ...
  MagentoCheckoutModelCart $cartModel,
  ...
 ) {
    ...
    $this->_cart = $cartModel;
    ...
  }

  public function getTotalWeight()
  {
      $items = $this->_cart->getQuote()->getAllItems();

     $weight = 0;
     foreach($items as $item) {
        $weight += ($item->getWeight() * $item->getQty()) ;        
     }

   return $weight;
  }