电子商务就是为了客户便利。提供便利的运输方式是在线商店中客户体验的必然部分。默认情况下,有7种Magento 2运送方式

  1. Free Shipping
  2. Flat Rates
  3. Table Rates
  4. UPS
  5. USPS
  6. edEx
  7. DHL

您还可以学习如何轻松配置Magento 2运送方式

然而,随着电子商务普及程度的提高,越来越多的类型的企业选择了在线购物平台,特别是Magento,因为它的功能和性能。但是,这些默认的运输方式还不足以满足目前所有类型的企业。

因此,我想出了一种编程方法来在Magento 2商店中创建运输方式

考虑到业务需求而实施的定制运输方式是可行的选择。运输策略必须确保其在订单履行成本中所获得的收益不超过该订单所获利润。

理想的运输方式必须易于管理,快速且价格合理。并不是每个店主都能从默认的Magento 2送货方式中找到这样的送货方式。

在Magento 2商店中以编程方式创建适合您的业务需求并且可行的自定义运输方法,是拥有正确运输系统的最优化解决方案。

为什么我要强调优化运输策略呢?

以下统计信息解释了原因:

  1. 61%的购物者表示由于额外的费用(例如运费)而退出交易– 99firms
  2. 79%的美国消费者表示免费送货将使他们更有可能在网上购物。– Walkersands
  3. 46.5%的中小型企业表示,提供免费送货可增加利润。– MCM

在Magento 2中创建运输方式的步骤:

  1. app/code/Alwayly/CustomShipping/registration.php中创建registration.php文件, 并将以下代码添加到该文件中:
    ;<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Alwayly_CustomShipping',
        __DIR__
    );
    
  2. app/code/Alwayly/CustomShipping/etc/module.xml中创建module.xml文件,并将以下代码添加到该文件中:
    <?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="Alwayly_CustomShipping" setup_version="1.0.0">
        </module>
    </config>
    
  3. app/code/Alwayly/CustomShipping/etc/adminhtml/system.xml中创建system.xml文件, 并将以下代码添加到该文件中:
    <?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" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">
                <group id="custom" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Custom Shipping</label>
                    <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Enabled</label>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    </field>
                    <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Method Name</label>
                    </field>
                    <field id="price" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Price</label>
                        <validate>validate-number validate-zero-or-greater</validate>
                    </field>
                    <field id="handling_type" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Calculate Handling Fee</label>
                        <source_model>Magento\Shipping\Model\Source\HandlingType</source_model>
                    </field>
                    <field id="handling_fee" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Handling Fee</label>
                        <validate>validate-number validate-zero-or-greater</validate>
                    </field>
                    <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Sort Order</label>
                    </field>
                    <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Title</label>
                    </field>
                    <field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0">
                        <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" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Ship to Specific Countries</label>
                        <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                        <can_be_empty>1</can_be_empty>
                    </field>
                    <field id="showmethod" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Show Method if Not Applicable</label>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    </field>
                    <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Displayed Error Message</label>
                    </field>
                </group>
            </section>
        </system>
    </config>
    
  4. app/code/Alwayly/CustomShipping/etc/config.xml中创建config.xml文件,并将以下代码添加到该文件中:
    <?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>
                <custom>
                    <active>1</active>
                    <sallowspecific>0</sallowspecific>
                    <model>Alwayly\CustomShipping\Model\Carrier\Custom</model>
                    <name>Custom Shipping</name>
                    <price>15.00</price>
                    <title>Custom Method</title>
                    <type>I</type>
                    <specificerrmsg>This shipping method is not available right now.</specificerrmsg>
                </custom>
            </carriers>
        </default>
    </config>
    
  5. app/code/Alwayly/CustomShipping/Model/Carrier/Custom.php中创建Custom.php文件,并将以下代码添加到此文件中:
    <?php
    
    namespace Alwayly\CustomShipping\Model\Carrier;
    
    use Magento\Quote\Model\Quote\Address\RateRequest;
    use Magento\Shipping\Model\Rate\Result;
    use Magento\Shipping\Model\Carrier\AbstractCarrier;
    use Magento\Shipping\Model\Carrier\CarrierInterface;
    use Magento\Framework\App\Config\ScopeConfigInterface;
    use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
    use Psr\Log\LoggerInterface;
    use Magento\Shipping\Model\Rate\ResultFactory;
    use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
    
    class Custom extends AbstractCarrier implements CarrierInterface
    {
        protected $_code = 'custom';
        protected $rateResultFactory;
        protected $rateMethodFactory;
    
        public function __construct(
            ScopeConfigInterface $scopeConfig,
            ErrorFactory $rateErrorFactory,
            LoggerInterface $logger,
            ResultFactory $rateResultFactory,
            MethodFactory $rateMethodFactory,
            array $data = []
        ) {
            $this->rateResultFactory = $rateResultFactory;
            $this->rateMethodFactory = $rateMethodFactory;
            parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
        }
    
        public function getAllowedMethods()
        {
            return ['custom' => $this->getConfigData('name')];
        }
    
        public function collectRates(RateRequest $request)
        {
            if (!$this->getConfigFlag('active')) {
                return false;
            }
    
            /** @var \Magento\Shipping\Model\Rate\Result $result */
            $result = $this->rateResultFactory->create();
    
            /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
            $method = $this->rateMethodFactory->create();
    
            $method->setCarrier('custom');
            $method->setCarrierTitle($this->getConfigData('title'));
    
            $method->setMethod('custom');
            $method->setMethodTitle($this->getConfigData('name'));
    
            /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/
            $amount = $this->getConfigData('price');
    
            $method->setPrice($amount);
            $method->setCost($amount);
    
            $result->append($method);
    
            return $result;
        }
    }
    

这就是在Magento 2中创建自定义送货方式的全部操作。创建Magento 2自定义送货方式后,其配置将在后端显示:

定制运输方式配置

一旦配置并启用了自定义送货方式,它就会显示在前端:

前端中的自定义送货方式