3

enter image description here

I want to display total products weight

(products weight* items quantity) in chekcout page shipping method lable beside.

1

You have in your module, in Model/Carrier the collectRates method with the parameter \Magento\Shipping\Model\Rate\Result $request and you can use this (int)$request->getPackageWeight().

I updated my answer

Below you have the module: You can see the screenshot here.

You need to create the module in app/code/VendorName/Shipping:

app/code/VendorName/Shipping/registration.php file:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_Shipping',
    __DIR__
);

If you want to to have the app/code/VendorName/Shipping/composer.json file:

{
    "name": "vendorname/module-shipping",
    "description": "",
    "type": "magento2-module",
    "license": "proprietary",
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "VendorName\\Shipping\\": ""
        }
    }
}

app/code/VendorName/Shipping/etc/module.xml file:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendorName_Shipping" setup_version="1.0.0"/>

app/code/VendorName/Shipping/etc/config.xml file:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <carriers>
            <shippingname>
                <model>VendorName\Shipping\Model\Carrier\ShippingName</model>
                <active>1</active>
                <title>ShippingName</title>
                <name>ShippingName</name>
                <price>0.00</price>
                <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
                <sallowspecific>0</sallowspecific>
            </shippingname>
        </carriers>
    </default>

app/code/VendorName/Shipping/etc/adminhtml/system.xml file:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="carriers" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label">
            <group id="shippingname" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
                <label>ShippingName</label>
                <field id="active" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="select">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="name" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text">
                    <label>Method Name</label>
                </field>
                <field id="price" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="text">
                    <label>Price</label>
                    <validate>validate-number validate-zero-or-greater</validate>
                </field>
                <field id="sort_order" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="40" translate="label" type="text">
                    <label>Sort Order</label>
                </field>
                <field id="title" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="50" translate="label" type="text">
                    <label>Title</label>
                </field>
                <field id="sallowspecific" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="60" translate="label" type="select">
                    <label>Ship to Applicable Countries</label>
                    <frontend_class>shipping-applicable-country</frontend_class>
                    <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model>
                </field>
                <field id="specificcountry" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="70" translate="label" type="multiselect">
                    <label>Ship to Specific Countries</label>
                    <can_be_empty>1</can_be_empty>
                    <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                </field>
                <field id="specificerrmsg" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="80" translate="label" type="textarea">
                    <label>Displayed Error Message</label>
                </field>
            </group>
        </section>
    </system>
</config>
  • What you have in Model ? – St3phan Sep 26 '18 at 9:14
  • I will create an example for you – St3phan Sep 26 '18 at 9:15
  • Please see my updated question, i have update exact position where i need to display – Chala Chalapathi Sep 26 '18 at 9:21
  • Your screenshot is not the default design by Magento 2.1.7, it's very hard for me, but I can put the weight below of shipping method name. – St3phan Sep 26 '18 at 19:00
  • Hi St3phan, its fine if its there alos you can provide me code please . – Chala Chalapathi Sep 27 '18 at 4:31
0

First override the shipping method box on checkout page

Then you can get total weight of products of cart by \Magento\Checkout\Model\Cart class

With objectManager

$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;

With Factory Method

protected $_cart;

public function __construct(
    ...
    \Magento\Checkout\Model\Cart $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;
}
  • Thanks for comment, Please see my question, i have update exact position where i need to display – Chala Chalapathi Sep 26 '18 at 9:19

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.