有许多运输运营商的现有扩展可供您选择利用您的Magento 2站点,但您自己的送货方式?添加自定义送货方式,这是一种直接的方法,只需要少数文件以实现– five, to be exact.
让’s create a ‘Customer Pickup’用于为客户提供本地仓库的客户的运输方式选项选择其订单而不是支付运费。
我们的新送货方法将获得自己的扩展,与其他自定义扩展一样,它将驻留在我们网站的供应商文件夹中’s app\code\
folder. We will be using the following path for our new extension:app\code\ClassyLlama\CustomerPickup
For the remainder of this article, all file and path references will be in relation to the 顾客挑选
folder.
We start by creating the two files that are required for all custom extensions: registration.php
and module.xml
. I will assume you are familiar enough with Magento 2 development to know what these two files are and why they are required, so I won’在这里深入了解。如果您是在Magento 2中创建扩展的新手,则DevDocs’ PHP开发人员指南 is a must-read.
首先,我们必须告诉Magento关于我们的新送货方式’s existence; this is accomplished with the 系统.xml
and 配置.xml
files. The 系统.xml
file will define the configuration settings available for our shipping method in the Magento 2 admin (in STORES > Configuration > SALES > Shipping Methods
) and the 配置.xml
file will set default values for those settings.
玛托托预期的运输运营商有一些配置设置,因此应包括在所有运输方法中:
积极的
–这种布尔值表示此送货方法是否处于活动状态。模型
–送货方式的路径’s model.标题
–运输的名称“carrier”将显示在前端。sallowspecific..
–此布尔值表示此送货方式是否应适用于所有国家或仅指定的送货方式。排序
–此参数告诉Magento,其中可用的运输方法列表此方法将显示。我们还包括一些其他设置:
价格
–该运输方式将具有0.00美元的价格。姓名
–该送货方法的名称要显示给客户。showmethod
–即使不适用,是否展示了此送货方式。我们的延伸’s 系统.xml
file has a default structure that should be followed:
<system>
<section>
<group>
<section>
node must have an ID
value of "载体s"
.<group>
node must have an ID
value equal to our new shipping code, in this case 顾客挑选
.通过以这种方式构建XML文件,Magento知道在后端中的配置设置(在这种情况下使用其他装运方法)以及如何在调用时检索它们。
etc\adminhtml\system.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?XML. 版本="1.0"?> <! - / ** * @category ClassyLlama * @copyright 版权所有(c)2017年Classy Llama Studios,LLC */ - > <配置 XMLNS.:XSI.="http://www.w3.org/2001/XMLSchema-instance" XSI.:非普遍存在地沉思地区=“URN:magento:magento:mage:magento_config:etc / system_file.xsd”> <系统> <部分 ID="载体s" 翻译="标签" 类型=“文本” 排序="320" showindefault.="1" showinwebsite.="1" showinstore.="1"> <团体 ID=“customerpickup” 翻译="标签" 类型=“文本” 排序="0" showindefault.="1" showinwebsite.="1" showinstore.="1"> <标签>顾客 捡起</标签> <场地 ID="积极的" 翻译="标签" 类型=“选择” 排序="1" showindefault.="1" showinwebsite.="1" showinstore.="0"> <标签>启用</标签> <source_model..>玛托托\配置\模型\配置\资源\是</source_model..> </场地> <场地 ID="标题" 翻译="标签" 类型=“文本” 排序="2" showindefault.="1" showinwebsite.="1" showinstore.="1"> <标签>标题</标签> </场地> <场地 ID="姓名" 翻译="标签" 类型=“文本” 排序="3" showindefault.="1" showinwebsite.="1" showinstore.="1"> <标签>方法 姓名</标签> </场地> <场地 ID="价格" 翻译="标签" 类型=“文本” 排序="5" showindefault.="1" showinwebsite.="1" showinstore.="0"> <标签>价格</标签> <证实>证实-数字 证实-零-或者-较大</证实> </场地> <场地 ID="排序" 翻译="标签" 类型=“文本” 排序="100." showindefault.="1" showinwebsite.="1" showinstore.="0"> <标签>种类 命令</标签> </场地> <场地 ID=“showmethod” 翻译="标签" 类型=“选择” 排序="92" showindefault.="1" showinwebsite.="1" showinstore.="0"> <标签>展示 方法 如果 不是 适用的</标签> <source_model..>玛托托\配置\模型\配置\资源\是</source_model..> </场地> </团体> </部分> </系统> </配置> |
我们的延伸’s 配置.xml
(the file that defines the default values for the settings defined in 系统.xml
) also has layout rules that must be followed:
<default>
with a child node of <carriers>
在 a child node named with the shipping method code we’ve created, in this case <customerpickup>
.etc\config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?XML. 版本="1.0"?> <! - / ** * @category ClassyLlama * @copyright 版权所有(c)2017年Classy Llama Studios,LLC */ - > <配置 XMLNS.:XSI.="http://www.w3.org/2001/XMLSchema-instance" XSI.:非普遍存在地沉思地区=“urn:magento:magento:mage:magento_store:etc / config.xsd”> <默认> <载体s> <顾客挑选> <积极的>1</积极的> <模型>Classyllama.\顾客挑选\模型\载体\顾客挑选</模型> <姓名>顾客 捡起</姓名> <价格>0.00</价格> <标题>自由 船运</标题> <sallowspecific..>0</sallowspecific..> <排序>100.</排序> </顾客挑选> </载体s> </默认> </配置> |
此时,我们’ve创建了扩展,但它不起作用’t yet do anything for us. To get the extension working for us, we need to create the model at app\code\classyllama \ custompickup \ model \ carrier \ custompickup.php
.
创建自定义送货方式时有一些特定的项目值得注意’s class:
\Magento\Shipping\Model\Carrier\AbstractCarrier
and implement \Magento\Shipping\Model\Carrier\CarrierInterface
.getalaldedmethods.
and 集光电
.
getalaldedmethods.
:
集光电
returns either:
错误的
, which effectively disables the shipping method, excluding it from a list of shipping options.\Magento\Shipping\Model\Rate\Result
instantiated via a 玛托托工厂.
结果
, it can have as many, or as few, methods appended to it as is applicable; each appended method is a factory-generated instance of \Magento\Quote\Model\Quote\Address\RateResult\Method
. This model represents a shipping method and has its details set within it (e.g. Carrier code and title, method code and title, Price). In our example below, we are only attaching a single method, but the process can be repeated as many times as necessary.$_code
variable in the class must be set to the unique shipping code chosen for our custom shipping method, in this case 顾客挑选
.$这个 - >getConfigData
is used to retrieve the configuration settings that are defined in the extension’s 系统.xml
file (e.g. $this->getConfigData(‘name’)将返回用于该方法的值设置’s 姓名
).app/code/ClassyLlama/CustomerPickup/Model/Carrier/CustomerPickup.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100. 101 102 103 104 105 106 107 |
<?PHP. / ** * @category ClassyLlama * @copyright 版权所有(c)2017年Classy Llama Studios,LLC */ 姓名space Classyllama.\顾客挑选\模型\载体; 采用 玛托托\引用\模型\引用\地址\重奏; 采用 玛托托\船运\模型\速度\结果; 班级 顾客挑选 延伸 \玛托托\船运\模型\载体\抽象载波 实施 \玛托托\船运\模型\载体\CARRACTINTINTFAFE. { / ** *常量定义方法的运输代码 */ const shipping_code. = 'customerpickup'; / ** * @var string */ 保护 $_代码 = 自己::shipping_code.; / ** * @var \ magento \ shipping \ model \ rate \结果表格 */ 保护 $rateresultfactory.; / ** * @var \ magento \ quote \ model \ quote \ address \ rateresult \ derforeFactory */ 保护 $速度methodFactory.; / ** * @param \ magento \ framework \ app \ config \ scopeconfiginterface $ scopeconfig * @param \ magento \ quote \ model \ quote \ address \ rateresult \ ErrorFactory $ RateerRorFactory * @param \ psr \ log \ loggerInterface $ logger * @param \ magento \ shipping \ model \ rate \结果\结果,请切换 * @param \ magento \ quote \ model \ quote \ address \ rateresult \ dettomfactory $ taremethodFactory * @param数组$数据 */ 民众 功能 __构造( \玛托托\框架\应用程序\配置\scopeconfiginterface. $scopeconfig., \玛托托\引用\模型\引用\地址\rateresult.\ErrorFactory. $速度erRorfactory., \PSR.\日志\日志gerInterface. $记录器, \玛托托\船运\模型\速度\结果表格 $rateresultfactory., \玛托托\引用\模型\引用\地址\rateresult.\方法切法 $速度methodFactory., 大批 $数据 = [] ) { $这个->rateresultfactory. = $rateresultfactory.; $这个->速度methodFactory. = $速度methodFactory.; 父母::__构造($scopeconfig., $速度erRorfactory., $记录器, $数据); } / ** * @return array */ 民众 功能 getalaldedmethods.() { 返回 [自己::shipping_code. => $这个->getconfigdata.('姓名')]; } / ** * @param terehequest $请求 * @return bool |结果 */ 民众 功能 集光电(重奏 $要求) { 如果 (!$这个->getactiveflag.()) { //在配置中禁用此送货方式 返回 错误的; } //检查订单是否有资格获得客户皮卡 //在此添加逻辑以确定此订单是否有资格获得此送货方式 // e.g.整个订单有库存吗?客户是否有本地地址? //如果订单不符合条件,则返回false //否则,继续构建和返回$结果,其中包括我们的送货方式的详细信息 / ** @var \ magento \ shipping \ model \ rate \结果$结果* / $结果 = $这个->rateresultfactory.->创造(); / ** @var \ magento \ quote \ model \ quote \ address \ rateresult \ method $方法* / $方法 = $这个->速度methodFactory.->创造(); $方法->setCarrier.(自己::shipping_code.); //从配置中获取标题,如system.xml中定义 $方法->setcarriertitle.($这个->getconfigdata.('标题')); $方法->setmethod.(自己::shipping_code.); //从配置中获取标题,如system.xml中定义 $方法->setmethodtitle.($这个->getconfigdata.('姓名')); //从配置中获取价格,如system.xml所定义的 $数量 = $这个->getconfigdata.('价格'); $方法->setprice.($数量); $方法->setcost.($数量); $结果->附加($方法); 返回 $结果; } } |
我们的新送货方式可以定制,但我们需要它。例如,我们可以检查客户中的任何项目’订单正在下载发货,如果是的话,不提供‘Customer Pickup’作为可用的送货方式;我们还可以禁用我们的运输方式,以便从特定区域以外放置的订单。
在启用新的扩展和清除缓存后,我们可以访问我们的网站’s admin section (e.g. //mystore.xyz/backend/admin
) and head to the Shipping Methods section at STORES > Configuration > SALES > Shipping Methods
. We should now see a page similar to the one below, with our new shipping method’s default configuration settings we defined earlier in the 系统.xml
file.
要测试我们的新送货方式,我们只需前往我们的商店’S网站喜欢客户,创建一个合格的订单(唐’t忘记了您以前在模型中放置的规则),并前往购物车。您可以在下面的屏幕截图中看到,我们的新送货方式可供我们选择。
If we proceed to checkout, we see in the screenshot below that we again have the option of 客户提货
as our shipping method.
我们现在已经完成了自定义运输方法的创建,配置和测试,允许我们当地客户节省运输成本,并在方便的情况下在我们的仓库中选择合格订单。
本教程可用于量身定制您的在线商店’S运输逻辑符合您和您的业务的适合,而不是与母语率模型相关,可能不适合每种情况。定制运输方法可以是一个强大的工具;由于创建过程很简单,因此在Magento工具腰带中为一个方便的工具!
5 Comments
伟大的帖子,它’s been very useful!
但是,如果客户选择拾取他的订单,他应该’在结账时需要填补他的送货地址。
订单确认信息应通知拾取地址(可以在Admin上设置)作为递送地址。
有没有办法做到这一点?
桂汉,
你带来了一个好点。 Savvy用户实际上可以通过在不可出区中使用送货地址来规避销售税,但是选择在人员中选择拾取订单的选项,这意味着订单应该征税。
我想象解决方案仍然需要客户选择送货地址,因为这是必要显示运输方法,但如果他们要选择“Customer Pickup”运输方式,继续支付步骤将用纳税人取代其送货地址(例如,商人’S地址)。但是,详细介绍了这种特定博客文章的范围。
我创建了一个自定义送货模块。我的自定义运输模块正常工作。它显示我填写在admin上的价格或结帐页面的价格。
现在,我需要根据用户填写或选择在Magento 2中可用运输的PIN码来更改运输价格。
你能帮我帮我吗?
neeraj.,
听起来您正在寻找自定义表率解决方案,可以根据客户输入选择运费。这需要创建一个新的数据库表,以使用识别代码/引脚(以及可选地填充/更新表的接口),将新输入字段添加到结帐过程中,以及运输扩展中的一些逻辑以匹配两个起来。
这是可以实现的,但涵盖这些步骤超出了这个特殊博客文章的范围。
好帖子,理查德。它’s very informative.