我正在制定自定义送货方式,每次尝试使用我的方法下订单时,我都会遇到异常(&#34;请指定送货方式&#34;)。< / p>
我尝试使用Magento 2 Flat Rate方法,但它确实有效。
我发现在第52行的Magento / Quote / Model / QuoteValidator.php中,getShippingMethod()
没有返回任何内容,因为该函数:
public function getShippingMethod()
{
return $this->getData('shipping_method');
}
在Magento / Quote / Model / Quote / Address.php中没有返回任何内容。
为了防止我的代码出错,我也尝试使用这种自定义送货方式(我按照本教程创建方法)http://www.blog.magepsycho.com/create-custom-shipping-module-in-magento-2/(只需激活模块并尝试使用该方法下订单)但是我面临同样的问题。
有人知道如何解决此问题吗? 感谢。
编辑1:
有我的Model / Carrier / method.php:
class Method extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements \Magento\Shipping\Model\Carrier\CarrierInterface
{
protected $_logger;
/**
* @var string
*/
protected $_code = 'coursierprive_transport';
/**
* @var bool
*/
protected $_isFixed = true;
/**
* @var \Magento\Shipping\Model\Rate\ResultFactory
*/
protected $_rateResultFactory;
/**
* @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
*/
protected $_rateMethodFactory;
/**
* @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\ResultFactory $rateResultFactory
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
array $data = []
)
{
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
$this->_logger = $logger;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
/**
* @param RateRequest $request
* @return \Magento\Shipping\Model\Rate\Result
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active'))
return (false);
if ($request->getAllItems())
{
foreach ($request->getAllItems() as $item)
{
// Some stuff used to check dimensions, weight, post code... etc
}
}
if (//some tests)
return (false);
$result = $this->_rateResultFactory->create();
$shippingPrice = 5.5;
$method = $this->_rateMethodFactory->create();
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes())
$shippingPrice = 0;
$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);
$result->append($method);
return ($result);
}
/**
* Get allowed shipping methods
*
* @return array
*/
public function getAllowedMethods()
{
return (['coursierprive_transport' => $this->getConfigData('name')]);
}
}
编辑2:
有我的Config.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Magento/Store/etc/config.xsd">
<default>
<carriers>
<coursierprive_transport>
<active>1</active>
<sallowspecific>0</sallowspecific>
<price>5.5</price>
<model>CoursierPrive\Transport\Model\Carrier\Method</model>
<name>Express</name>
<title>Coursier Privé</title>
<specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
</coursierprive_transport>
</carriers>
</default>
编辑3:好的,我想我知道搞砸了什么。我想方法名称有一个大小限制,我的名字包含太多的字符。
答案 0 :(得分:1)
尝试将代码更改为以下内容:
protected $_code = 'couriertransport';
我不确定这是否是问题,但这样做解决了这个问题: https://github.com/MagePsycho/magento2-custom-shipping
答案 1 :(得分:0)
我的情况:protected $ _code ='effectconnect_shipment'; =&GT;所以完整代码必须是'effectconnect_shipment_effectconnect_shipment' 我检查了这段代码的长度只有40,所以重命名为$ _code ='effectconnect';它工作!!!