您可以选择在Magento 2网站上使用许多现有运输扩展程序,但是您自己的运输方式又如何呢?添加这样的自定义送货方式是一种简单的方法,只需少量文件即可实施– five, to be exact.
让’s 创建 a ‘Customer 拾起’一种运输方式,用于为我们仓库本地的客户提供选择提货的方式,而不是选择支付运费。
我们的新送货方式将获得自己的扩展名,并且与其他自定义扩展名一样,它将驻留在我们网站的供应商文件夹中’s app\code\
folder. We will be using the following path for our new extension:app\code\ClassyLlama\CustomerPickup
For the remainder of 这个 文章, all file and path references will be 在 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开发人员指南 是必读的。
首先,我们必须告诉Magento我们新的送货方式’s existence; 这个 is accomplished with the 系统.xml
and 配置.xml
files. The 系统.xml
file will define the 配置uration settings available for our shipping 方法 在 the Magento 2 admin (in STORES > 设定档uration > SALES > 运输 方法s
) and the 配置.xml
file will set 默认 values for those settings.
Magento期望有某些运输承运商的配置设置,因此应包含在所有运输方式中:
活性
–此布尔值指示此传送方法是否处于活动状态。模型
–运送方式的路径’s 模型.标题
– The 名称 of the shipping “carrier”将显示在前端。特定的
–此布尔值指示此送货方式应适用于所有国家还是仅适用于指定的国家/地区。排序
–该参数告诉Magento该方法将在其中显示可用运输方法的列表。我们还包括一些其他设置:
价钱
–这种运输方式的价格为$ 0.00。名称
– The 名称 of 这个 shipping 方法 to be displayed to the customer.showmethod
–是否在不适用的情况下也显示此运输方式。我们的扩展’s 系统.xml
file has a 默认 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, 在 这个 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"?> <!- / ** * @类别 ClassyLlama * @版权 复制right (c) 2017 优雅的骆马Studios, LLC * / -> <配置 XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" si:noNamespaceSchemaLocation=“ urn:magento:module:Magento_Config:etc / 系统_file.xsd”> <系统> <部分 ID="载体s" 翻译="标签" 类型=“文本” 排序="320" showInDefault="1" showInWebsite="1" showInStore="1"> <组 ID=“客户提货” 翻译="标签" 类型=“文本” 排序="0" showInDefault="1" showInWebsite="1" showInStore="1"> <标签>顾客 拾起</标签> <领域 ID="活性" 翻译="标签" 类型=“选择” 排序="1" showInDefault="1" showInWebsite="1" showInStore="0"> <标签>已启用</标签> <source_model>Magento\设定档\模型\设定档\资源\是的</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=“显示方法” 翻译="标签" 类型=“选择” 排序="92" showInDefault="1" showInWebsite="1" showInStore="0"> <标签>节目 方法 如果 不 适用于</标签> <source_model>Magento\设定档\模型\设定档\资源\是的</source_model> </领域> </组> </部分> </系统> </配置> |
我们的扩展’s 配置.xml
(the file that defines the 默认 values for the settings defined 在 系统.xml
) also has layout rules that must be followed:
<default>
with a child node of <carriers>
在 a child node 名称d with the shipping 方法 code we’ve 创建d, 在 这个 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"?> <!- / ** * @类别 ClassyLlama * @版权 复制right (c) 2017 优雅的骆马Studios, LLC * / -> <配置 XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" si:noNamespaceSchemaLocation=“ ur:magento:模块:Magento_Store:etc / 配置.xsd”> <默认> <载体s> <客户提货> <活性>1</活性> <模型>优雅的骆马\客户提货\模型\载体\客户提货</模型> <名称>顾客 拾起</名称> <价钱>0.00</价钱> <标题>自由 运输</标题> <特定的>0</特定的> <排序>100</排序> </客户提货> </载体s> </默认> </配置> |
至此,我们’创建了扩展名,但没有’t yet do anything for us. To get the extension working for us, we need to 创建 the 模型 at app\code\ClassyLlama \ 客户提货 \ 模型 \ 载体 \ 客户提货.php
.
创建我们的自定义送货方式时,需要注意一些具体事项’s 类:
\Magento\Shipping\Model\Carrier\AbstractCarrier
and implement \Magento\Shipping\Model\Carrier\CarrierInterface
.getAllowedMethods
and collectRates
.
getAllowedMethods
:
collectRates
返回s either:
假
, which effectively disables the shipping 方法, excluding it from a list of shipping options.\Magento\Shipping\Model\Rate\Result
在 stantiated via a Magento工厂.
结果
, it can have as many, 要么 as few, 方法s 附加ed to it as is applicable; each 附加ed 方法 is a factory-generated 在 stance of \Magento\Quote\Model\Quote\Address\RateResult\Method
. This 模型 represents a shipping 方法 and has its details set within it (e.g. 载体 code and 标题, 方法 code and 标题, 价钱). In our example below, we are 上ly attaching a single 方法, but the process can be repeated as many times as necessary.$_code
variable 在 the 类 must be set to the unique shipping code chosen for our custom shipping 方法, 在 这个 case 客户提货
.$ 这个->getConfigData
is 使用d to retrieve the 配置uration settings that are defined 在 the extension’s 系统.xml
file (e.g. $ 这个->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 / ** * @类别 ClassyLlama * @版权 复制right (c) 2017 优雅的骆马Studios, LLC * / 名称space 优雅的骆马\客户提货\模型\载体; 使用 Magento\引用\模型\引用\地址\率Request; 使用 Magento\运输\模型\率\结果; 类 客户提货 延伸 \Magento\运输\模型\载体\抽象载体 实施 \Magento\运输\模型\载体\载体接口 { / ** *常量定义方法的运输代码 * / const SHIPPING_CODE = “客户提货”; / ** * @var字符串 * / 受保护的 $_码 = 自::SHIPPING_CODE; / ** * @var \ Magento \ 运输 \ 模型 \ 率 \ 结果工厂 * / 受保护的 $rateResultFactory; / ** * @var \ Magento \ 引用 \ 模型 \ 引用 \ 地址 \ 率Result \ 方法工厂 * / 受保护的 $rateMethodFactory; / ** * @param \ Magento \ 构架 \ 应用程式 \ 设定档 \ ScopeConfigInterface $ scopeConfig * @param \ Magento \ 引用 \ 模型 \ 引用 \ 地址 \ 率Result \ 错误工厂 $ rateErrorFactory * @param \ 压敏电阻 \ 日志记录 \ 记录器接口 $ 记录器 * @param \ Magento \ 运输 \ 模型 \ 率 \ 结果工厂 $ rateResultFactory * @参数\ Magento \ 引用 \ 模型 \ 引用 \ 地址 \ 率Result \ 方法工厂 $ rateMethodFactory * @参数数组$ 数据 * / 上市 功能 __构造( \Magento\构架\应用程式\设定档\ScopeConfigInterface $scopeConfig, \Magento\引用\模型\引用\地址\率Result\错误工厂 $rateErrorFactory, \压敏电阻\日志记录\记录器接口 $记录器, \Magento\运输\模型\率\结果工厂 $rateResultFactory, \Magento\引用\模型\引用\地址\率Result\方法工厂 $rateMethodFactory, 数组 $数据 = [] ) { $这个->rateResultFactory = $rateResultFactory; $这个->rateMethodFactory = $rateMethodFactory; 父母::__构造($scopeConfig, $rateErrorFactory, $记录器, $数据); } / ** * @返回数组 * / 上市 功能 getAllowedMethods() { 返回 [自::SHIPPING_CODE => $这个->getConfigData('名称')]; } / ** * @param 率Request $ 请求 * @return bool |结果 * / 上市 功能 collectRates(率Request $请求) { 如果 (!$这个->getActiveFlag()) { //此运送方式在配置中被禁用 返回 假; } //检查订单是否符合客户提货条件 //在此处添加逻辑以确定此订单是否符合此送货方式 //例如整个订单都有库存吗?客户有当地地址吗? //如果订单不符合条件,则返回false //否则,继续构建并返回$ 结果,其中包括我们运送方式的详细信息 / ** @var \ Magento \ 运输 \ 模型 \ 率 \ 结果 $ 结果 * / $结果 = $这个->rateResultFactory->创建(); / ** @var \ Magento \ 引用 \ 模型 \ 引用 \ 地址 \ 率Result \ 方法 $ 方法 * / $方法 = $这个->rateMethodFactory->创建(); $方法->setCarrier(自::SHIPPING_CODE); //从system.xml中定义的配置中获取标题 $方法->setCarrierTitle($这个->getConfigData('标题')); $方法->setMethod(自::SHIPPING_CODE); //从system.xml中定义的配置中获取标题 $方法->setMethodTitle($这个->getConfigData('名称')); //从配置中获取价格,如system.xml中所定义 $量 = $这个->getConfigData('价钱'); $方法->setPrice($量); $方法->setCost($量); $结果->附加($方法); 返回 $结果; } } |
我们可以定制新的运输方式,但我们需要如此。例如,我们可以检查客户中是否有任何物品’的订单正在下运,如果是,则不提供‘Customer 拾起’作为可用的运输方式;对于从特定区域之外下达的订单,我们也可以禁用我们的送货方式。
启用新的扩展程序并清除缓存后,我们可以访问我们的网站’s admin 部分 (e.g. //mystore.xyz/backend/admin
) and head to the 运输 方法s 部分 at STORES > 设定档uration > SALES > 运输 方法s
. We should now see a page similar to the 上e below, with our new shipping 方法’s 默认 配置uration settings we defined earlier 在 the 系统.xml
file.
要测试我们的新送货方式,我们只需要前往我们的商店’的网站,就像客户会创建一个合格订单(唐’不要忘记您先前在模型中制定的规则),然后前往购物车。如您在以下屏幕截图中所见,我们可以选择我们的新送货方式。
If we proceed to checkout, we see 在 the screenshot below that we again have the option of 客户取货
as our shipping 方法.
现在,我们已经完成了自定义运输方法的创建,配置和测试,该方法可使我们的本地客户节省运输成本,并在方便时在我们的仓库取货合格的订单。
本教程可用于定制您的在线商店’的运输逻辑以匹配适合您和您的业务的事物,而不是与可能不适用于每种情况的本机利率模型联系在一起。定制运输方式可能是一个强大的工具;由于创建过程很简单,因此可以在Magento工具带中使用方便的工具!
5 评论
很棒的帖子’s been very 使用ful!
但是,如果客户选择取货,他不应该’结帐时不需要填写他的收货地址。
并且订单确认信息应告知取货地址(可以在Admin上设置)作为交货地址。
有没有办法做到这一点?
吉尔赫姆,
您提出了一个好点。精明的用户实际上可以通过使用非应税区域中的送货地址来规避销售税,但随后选择亲自取货的选项,这意味着应该对订单征税。
我想该解决方案仍然需要客户选择送货地址,因为这是显示送货方式所必需的,但是如果他们选择“Customer 拾起”送货方式,继续进行付款步骤,将其送货地址替换为应纳税地址(例如商家’的地址)。但是,详细解决这个问题超出了此特定博客文章的范围。
我创建一个自定义的运输模块。我的自定义运输模块运行正常。它显示了我在购物车或结帐页面上的管理员中填写的价格。
现在,我需要根据用户填写的个人识别码更改运费或在magento 2中选择可用的运费。
您能帮我怎么做?
内拉杰,
听起来您正在寻找一种定制的表费解决方案,该解决方案将根据客户输入选择运费。这将需要创建一个新的数据库表来保存费率及其标识码/引脚(以及可选的用于填充/更新表的接口),向结帐流程添加新的输入字段,并在运输扩展中添加一些逻辑以进行匹配两个起来。
这是可以实现的,但涵盖这些步骤超出了此特定博客文章的范围。
理查德,尼斯邮报。它’s very 在 formative.
该网站使用Akismet减少垃圾邮件。 了解如何处理您的评论数据.