/etc/di.xml

<?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\Checkout\Model\DefaultConfigProvider">
        <plugin name="AddAttPlug" type="Maijindou\Checkout\Plugin\ConfigProviderPlugin" />
    </type>
</config>
Maijindou\Checkout\Plugin\ConfigProviderPlugin

<?php


namespace Maijindou\Checkout\Plugin;
use Magento\Store\Model\ScopeInterface;

class ConfigProviderPlugin extends \Magento\Framework\Model\AbstractModel
{


    public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
    {

        $items = $result['totalsData']['items'];
        $weights = 0;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        for ($i = 0; $i < count($items); $i++) {
            $quoteId = $items[$i]['item_id'];
            $quote = $objectManager->get('\Magento\Quote\Model\Quote\Item')->load($quoteId);
            $productId = $quote->getProductId();
            $product = $objectManager->get('\Magento\Catalog\Model\Product')->load($productId);
            $we = $product->getWeight();
            $items[$i]['weight'] = round($we, 2);

            $weights = (float)$we * $items[$i]['qty'] + $weights;
        }
        $result['totalsData']['items'] = $items;
        $result['totalsData']['weight_unit'] = $this->getWeightNnit();
        $result['totalsData']['weight'] = $weights;
        return $result;
    }

    public function getWeightNnit()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $scope = $objectManager->get('\Magento\Framework\App\Config\ScopeConfigInterface');
        return $scope->getValue('general/locale/weight_unit', ScopeInterface::SCOPE_STORE);
    }
}

最后修改日期: 2021-03-24

作者