I am creating a custom shipping module.So,I want to get the weight of order items individually to create shipping rule.How can we get the weight?

  • are you need in collectRates function or order object?
    – Abdul
    Mar 20 '18 at 5:10
  • can you please share your custom code in question?
    – Abdul
    Mar 20 '18 at 5:10

I got the result as follows...

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

$items = $cart->getQuote()->getAllItems();

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

echo $weight;

first you need to get "cartQuote" then find out all cartQuote items. and fetch one by one item and get weight of product and create rule as per your condition


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.