Magento 2.4.4 -为 Package 添加皮重,以便进行运输计算

goucqfw6  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(97)

问题是,我们有很多项目是非常小的2- 50克和一个盒子+ Package 重100- 200克
包裹往往是最重的部分
如果我们将商品重量(在产品中)设置为商品的实际重量,那么当人们订购很多东西时,盒子的重量会使总重量超过所选择的邮资方法。
如果我们广告的箱子重量的项目,然后它使邮费成本非常昂贵,并意味着人们不作出购买。
我如何为每种 Package 类型设置箱重?
是否有一个扩展,或者是否有一种方法来添加一些逻辑,以添加一个皮重的方块之前,Magento计算的重量包.这似乎很奇怪,这不是Magento的功能,因为这似乎是每个人都在网上销售项目将不得不做.
我想我需要一些东西来检查订单项目的重量
如果物品总重量小于1 KG,则添加(小 Package 盒输入的重量)如果物品总重量小于2GK但大于1 KG,则添加(中 Package 盒输入的重量)如果物品总重量大于2KG,则添加(大 Package 盒输入的重量)
希望能够输入包裹的 Package 基本重量

mf98qq94

mf98qq941#

此流程是在假设包裹重量和类型信息是管理员和运费是固定的情况下设置的。
步骤1:-创建etc/数据库架构. xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="sales_order" resource="default" engine="innodb" comment="sales_order">
        <column xsi:type="int" name="box_weight" default="0" comment="Box Weight" />
        <column xsi:type="varchar" name="box_type" nullable="false" length="255" comment="Box Type" />
    </table>
    <table name="sales_order_grid" resource="default" engine="innodb" comment="sales_order">
        <column xsi:type="int" name="box_weight" default="0" comment="Box Weight" />
        <column xsi:type="varchar" name="box_type" nullable="false" length="255" comment="Box Type" />
    </table>
</schema>

步骤2:创建模型/资源模型/订单/网格/集合. php文件

<?php

namespace Sunarc\Custom\Model\ResourceModel\Order\Grid;
 
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as CoreFetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as CoreEntityFactory;
use Magento\Framework\Event\ManagerInterface as CoreEventManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as CoreSalesGrid;
use Psr\Log\LoggerInterface as Logger;
 

class Collection extends CoreSalesGrid
{
    /**
     * @param CoreEntityFactory $entityFactory
     * @param Logger            $logger       
     * @param CoreFetchStrategy $fetchStrategy
     * @param CoreEventManager  $eventManager 
     * @param string            $mainTable    
     * @param string            $resourceModel
     */
    public function __construct(
        CoreEntityFactory $entityFactory,
        Logger $logger,
        CoreFetchStrategy $fetchStrategy,
        CoreEventManager $eventManager,
        $mainTable = 'sales_order_grid',
        $resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }
 
    protected function _renderFiltersBefore()
    {
        $joinTable = $this->getTable('sales_order');
        $this->getSelect()->joinLeft(
            $joinTable,
            'main_table.entity_id = sales_order.entity_id',
            ['box_weight','box_type']
        );
        parent::_renderFiltersBefore();
    }
}

第3步:现在,创建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">
    <preference for="Magento\Sales\Controller\Order\History" type="Sunarc\Custom\Controller\Order\History" />

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="sales_order_grid_data_source" xsi:type="string">Sunarc\Custom\Model\ResourceModel\Order\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <type name="Sunarc\Custom\Model\ResourceModel\Order\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">sales_order_grid</argument>
            <argument name="resourceModel" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>
        </arguments>
    </type>

</config>

步骤4:创建视图/管理html/用户界面组件/销售订单网格. xml

<?xml version="1.0" encoding="UTF-8"?>

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">

        <column name="box_weight">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Parcel Weight</item>
                </item>
            </argument>
        </column>

        <column name="box_type">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Box Type</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

步骤5:-创建etc/事件. xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_before">
        <observer name="weight" instance="Sunarc\Custom\Observer\Weight" />
    </event>
</config>

步骤6:创建Observer/Weight. php并编写自定义逻辑

<?php

namespace Sunarc\Custom\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteFactory;

class Weight implements ObserverInterface
{

    public function __construct(QuoteFactory $quoteFactory)
    {
        $this->quoteFactory = $quoteFactory;
    }
    /**
     * Below is the method that will fire whenever the event runs!
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {

        //WRITE YOUR CUSTOM LOGIC HERE
        $order = $observer->getEvent()->getOrder();

        if ($order->getData('weight') == 1) {
            $order->setData('box_weight', 5); // Assumed wight of small parcel
            $order->setData('box_type', 'Small');
        } elseif ($order->getData('weight') > 1 && $order->getData('weight') <= 2) {
            $order->setData('box_weight', 7); // Assumed wight of Medium parcel
            $order->setData('box_type', 'Medium');
        } else {
            $order->setData('box_weight', 10);  // Assumed wight of Large parcel
            $order->setData('box_type', 'Large');
        }
    }
}

相关问题