douzi9211 2016-02-18 11:00
浏览 27

自定义送货方式 - 请指定送货方式

I'm working on a custom shipping method and I've been getting exception each times I try to place an order with my method ("Please specify a shipping method").

I tried with the Magento 2 Flat Rate method and it worked.

I found that in Magento/Quote/Model/QuoteValidator.php on line 52, the getShippingMethod() returned nothing because that function :

public function getShippingMethod()
{
    return $this->getData('shipping_method');
}

in Magento/Quote/Model/Quote/Address.php returned nothing.

Just in case my code was wrong, I also tried with this custom shipping method (I followed this tutorial to create the method) http://www.blog.magepsycho.com/create-custom-shipping-module-in-magento-2/ (just activate the module and tried to place an order with that method) but I'm facing the same issue.

Does someone know how I can resolve this issue ? Thanks.

Edit 1 :

There is my 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')]);
}
}

Edit 2 :

There is my 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>

Edit 3 : Okay, I think that I know what messed up. I suppose there is a size limit for the method name and my first name contained too many chars.

  • 写回答

2条回答 默认 最新

  • doumingchen3628 2016-05-27 20:51
    关注

    Try to change the code to something short like:

    protected $_code = 'couriertransport';
    

    I am not sure if this is the issue but doing so fixed the issue: https://github.com/MagePsycho/magento2-custom-shipping

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题