如何在PrestaShop中通过编程向Carrier添加配送方式?
如何通过自定义模块编程向Carrier表添加配送方式
步骤1:创建自定义模块基础结构
先搭建模块的核心目录和配置文件:
app/code/Vendor/CustomShipping/registration.php
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_CustomShipping', __DIR__);
app/code/Vendor/CustomShipping/etc/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="Vendor_CustomShipping" setup_version="1.0.0"> <sequence> <module name="Magento_Shipping"/> </sequence> </module> </config>
步骤2:编写数据安装脚本
通过InstallData.php向carrier表插入配送方式数据:
app/code/Vendor/CustomShipping/Setup/InstallData.php
<?php namespace Vendor\CustomShipping\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Shipping\Model\CarrierFactory; class InstallData implements InstallDataInterface { private $carrierFactory; public function __construct(CarrierFactory $carrierFactory) { $this->carrierFactory = $carrierFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); // 实例化Carrier模型并设置字段值 $carrier = $this->carrierFactory->create(); $carrier->setData([ 'code' => 'custom_shipping', // 唯一标识,不可重复 'title' => '自定义配送方式', // 前台显示标题 'active' => 1, // 1=启用,0=禁用 'sallowspecific' => 0, // 0=支持所有国家,1=仅指定国家 'sort_order' => 10, // 排序优先级,值越小越靠前 'model' => 'Vendor\CustomShipping\Model\Carrier\Custom', // 配送逻辑模型路径 'price_type' => 'O', // O=固定价格,P=按百分比 'price' => 5.00, // 基础配送价格 'specificerrmsg' => '抱歉,该配送方式暂不支持您的地址', // 不支持时的提示语 'is_trackable' => 0, // 是否支持物流追踪 'shipping_type' => 'flatrate' // 配送类型标识 ]); $carrier->save(); $setup->endSetup(); } }
步骤3:实现配送逻辑模型
创建自定义配送方式的核心逻辑类,处理费率计算:
app/code/Vendor/CustomShipping/Model/Carrier/Custom.php
<?php namespace Vendor\CustomShipping\Model\Carrier; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Shipping\Model\Carrier\AbstractCarrier; use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Rate\ResultFactory; use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory; use Psr\Log\LoggerInterface; class Custom extends AbstractCarrier implements CarrierInterface { protected $_code = 'custom_shipping'; protected $_isFixed = true; private $_rateResultFactory; private $_rateMethodFactory; public function __construct( ScopeConfigInterface $scopeConfig, LoggerInterface $logger, ResultFactory $rateResultFactory, MethodFactory $rateMethodFactory, array $data = [] ) { $this->_rateResultFactory = $rateResultFactory; $this->_rateMethodFactory = $rateMethodFactory; parent::__construct($scopeConfig, $logger, $data); } // 计算配送费率 public function collectRates(RateRequest $request) { // 如果配送方式未启用,直接返回 if (!$this->getConfigFlag('active')) { return false; } $result = $this->_rateResultFactory->create(); $method = $this->_rateMethodFactory->create(); // 设置配送方式基本信息 $method->setCarrier($this->_code); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod($this->_code); $method->setMethodTitle($this->getConfigData('title')); // 设置价格和成本 $shippingPrice = $this->getConfigData('price'); $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); return $result; } // 返回允许的配送方法 public function getAllowedMethods() { return [$this->_code => $this->getConfigData('title')]; } }
步骤4:添加后台配置项(可选)
让管理员可在后台修改配送方式的参数:
app/code/Vendor/CustomShipping/etc/adminhtml/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_shipping" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>自定义配送方式</label> <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0"> <label>启用</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1"> <label>标题</label> </field> <field id="price" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0"> <label>价格</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>排序</label> </field> <field id="sallowspecific" translate="label" type="select" sortOrder="101" showInDefault="1" showInWebsite="1" showInStore="0"> <label>允许指定国家</label> <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model> </field> <field id="specificerrmsg" translate="label" type="text" sortOrder="103" showInDefault="1" showInWebsite="1" showInStore="1"> <label>错误提示信息</label> </field> </group> </section> </system> </config>
app/code/Vendor/CustomShipping/etc/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_shipping> <active>1</active> <title>自定义配送方式</title> <price>5.00</price> <sort_order>10</sort_order> <sallowspecific>0</sallowspecific> <specificerrmsg>抱歉,该配送方式暂不支持您的地址</specificerrmsg> </custom_shipping> </carriers> </default> </config>
最后:安装模块并生效
执行以下命令完成模块安装:
bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:clean
注意事项
code字段必须唯一,避免与现有配送方式冲突- 若需要复杂的价格计算逻辑,可修改
collectRates方法(比如根据重量、金额计算) - 设置
sallowspecific=1时,需在后台指定允许配送的国家
内容的提问来源于stack exchange,提问作者Swati Mavani
相关产品推荐
相关产品推荐

