Question

I followed this tutorial (https://magenticians.com/create-magento-2-shipping-module-method/) that showed me how to create a custom shipping method, but I still have one problem, i need to take into account the weight of the product to see if I can enable such shipping method, so i came here to see if anyone as any ideas how i can do that.

Thanks in advance

Was it helpful?

Solution

Try this code in your Carrier (collectRates function)

  $qty = 0;
    if ($request->getAllItems())
    {
        foreach ($request->getAllItems() as $item)
        {
            if ($item->getProduct()->isVirtual())
            {
                continue;
            }

            if ($item->getHasChildren())
            {
                foreach ($item->getChildren() as $child)
                {
                    if (! $child->getProduct()->isVirtual())
                    {
                        $qty += $child->getQty();
                    }
                }
            } else
            {

                $qty += $item->getQty();
            }
        }
    }
$final_qty = $qty;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top