Magento 2 Produktindividuelle Versandkosten Titelbild

Our web agency was tasked with setting the shipping costs individually for each product in a Magento 2 online shop. More specifically, each product needed to be assigned a specific weight depending on a particular attribute. This weight could then be used to determine the final total shipping costs with the help of table-rate shipping. One way to achieve this, for example, is by writing a small plugin.

 

 

First, declare the relevant model in the module's di.xml and point it to the model that needs to be created. In our case, we want to modify the model:

 

Magento\Quote\Model\Quote\Item

 

modify it. In the di.xml, this would look for example like this:

 

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Quote\Model\Quote\Item">

        <plugin name="IRGENDEINNAME"

          type="VENDORNAME\MODULENAME\Plugin\Quote\ShippingWeightQuoteItem" sortOrder="2" />

    </type>

</config>

 

The target model can, of course, be located elsewhere. Here, however, it is the file.

 

app/code/VENDORNAME/MODULENAME/Plugin/Quote/ShippingWeightQuoteItem.php

 

with the following content (also as an example):

 

<?php

namespace Econcess\ModifyShipping\Plugin\Quote;

class ShippingWeightQuoteItem

{   

                public function afterSetProduct(\Magento\Quote\Model\Quote\Item $subject, $result)

    {

                               $mod_weight = 0;

                               $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

                               $product_versand = $objectManager->create('Magento\Catalog\Model\Product')->load($subject->getProduct()->getId());

                               $versand_id = $product_versand->getData('IRGENDEINATTRIBUT');

                               if ($versand_id == 44) {

                               } elseif ($versand_id == 33) {

                                               $mod_weight = 1;

                               } elseif ($versand_id == 22 || $versand_id == 11) {

                                               $mod_weight = 2;

                               } elseif ($versand_id == 88) {

                                               $mod_weight = 3;

                               } elseif ($versand_id == 99) {

                                               $mod_weight = 4;

                               } elseif ($versand_id == 77) {

                                               $mod_weight = 5;

                               } elseif ($versand_id == 66) {

                                               $mod_weight = 6;

                               } else {

                                               $mod_weight = $product_versand->getData('weight');

                               }

                               $result->setWeight($mod_weight);

                               return $result;

    }

}

 

This changes the weights and ensures that the shop's shipping costs are calculated correctly.

 

Observed at our web agency in a Magento 2 CE 2.3.5 online shop.