首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Magento 2矩阵速率校验问题(载波采用这种方法而不是found:matrixrate_5334)

Magento 2矩阵速率校验问题(载波采用这种方法而不是found:matrixrate_5334)
EN

Stack Overflow用户
提问于 2020-01-28 13:04:21
回答 1查看 1.6K关注 0票数 1

现在,我到处寻找解决方案,没有任何结果。当客户付款并试图处理订单时,我们面临着运输方式的问题,我们有一个错误,即“承运人用这样的方法而不是found:matrixrate_5334”和运输方法的解体,需要重新删除和创建购物车来查看这些运输方法。

PHP7.0MagentoVersion2.2.1 Webshopp最新版本https://docs.shipperhq.com/troubleshooting-matrixrates/

有人能帮忙吗?

在网站上弹出问题

EN

回答 1

Stack Overflow用户

发布于 2020-11-13 19:23:43

我注意到此错误是由于签出会话报价$this->getShippingMethod()在计算运费时未正确设置/空造成的。

代码语言:javascript
运行
复制
magento/codepool/vendor/magento/module-quote/Model/Quote/Address.php
public function requestShippingRates(AbstractItem $item = null)
Line 1080: if ($this->getShippingMethod() == $rate->getCode()) {

错误:

代码语言:javascript
运行
复制
{"message":"Carrier with such method not found: %1, %2","parameters":["matrixrate","matrixrate_2"]}

解决方案:

我可以通过重写\WebShopApps\MatrixRate\Model\Carrier\Matrixrate collectRates()方法并在结帐报价发送地址中设置送货方法来解决这个问题。

代码语言:javascript
运行
复制
// set quote shipping method
$shippingMethod = $method->getCarrier().'_'.$method->getMethod();
$this->checkoutSession->getQuote()->getShippingAddress()->setShippingMethod($shippingMethod);

============================================

代码语言:javascript
运行
复制
Vendor/MatrixRate/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="WebShopApps\MatrixRate\Model\Carrier\Matrixrate" type="Vendor\MatrixRate\Model\Carrier\Matrixrate"/>
</config>

覆盖Vendor\MatrixRate\Model\Carrier\Matrixrate.php

\Magento\Checkout\Model\Session $checkoutSession注入__construct

代码语言:javascript
运行
复制
public function collectRates(RateRequest $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        // exclude Virtual products price from Package value if pre-configured
        if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) {
            foreach ($request->getAllItems() as $item) {
                if ($item->getParentItem()) {
                    continue;
                }
                if ($item->getHasChildren() && $item->isShipSeparately()) {
                    foreach ($item->getChildren() as $child) {
                        if ($child->getProduct()->isVirtual()) {
                            $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal());
                        }
                    }
                } elseif ($item->getProduct()->isVirtual()) {
                    $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
                }
            }
        }

        // Free shipping by qty
        $freeQty = 0;
        if ($request->getAllItems()) {
            $freePackageValue = 0;
            foreach ($request->getAllItems() as $item) {
                if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                    continue;
                }

                if ($item->getHasChildren() && $item->isShipSeparately()) {
                    foreach ($item->getChildren() as $child) {
                        if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                            $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0;
                            $freeQty += $item->getQty() * ($child->getQty() - $freeShipping);
                        }
                    }
                } elseif ($item->getFreeShipping()) {
                    $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0;
                    $freeQty += $item->getQty() - $freeShipping;
                    $freePackageValue += $item->getBaseRowTotal();
                }
            }
            $oldValue = $request->getPackageValue();
            $request->setPackageValue($oldValue - $freePackageValue);
        }

        if (!$request->getConditionMRName()) {
            $conditionName = $this->getConfigData('condition_name');
            $request->setConditionMRName($conditionName ? $conditionName : $this->defaultConditionName);
        }

        // Package weight and qty free shipping
        $oldWeight = $request->getPackageWeight();
        $oldQty = $request->getPackageQty();

        $request->setPackageWeight($request->getFreeMethodWeight());
        $request->setPackageQty($oldQty - $freeQty);

        /** @var \Magento\Shipping\Model\Rate\Result $result */
        $result = $this->rateResultFactory->create();
        $zipRange = $this->getConfigData('zip_range');
        $rateArray = $this->getRate($request, $zipRange);

        $request->setPackageWeight($oldWeight);
        $request->setPackageQty($oldQty);

        $foundRates = false;

        foreach ($rateArray as $rate) {
            if (!empty($rate) && $rate['price'] >= 0) {
                /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
                $method = $this->resultMethodFactory->create();

                $method->setCarrier('matrixrate');
                $method->setCarrierTitle($this->getConfigData('title'));

                $method->setMethod('matrixrate_' . $rate['pk']);
                $method->setMethodTitle(__($rate['shipping_method']));

                if ($request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) {
                    $shippingPrice = 0;
                } else {
                    $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
                }

                $method->setPrice($shippingPrice);
                $method->setCost($rate['cost']);

                $result->append($method);
                $foundRates = true; // have found some valid rates

                // set quote shipping method
                $shippingMethod = $method->getCarrier().'_'.$method->getMethod();
                $this->checkoutSession->getQuote()->getShippingAddress()->setShippingMethod($shippingMethod);
            }
        }

        if (!$foundRates) {
            /** @var \Magento\Quote\Model\Quote\Address\RateResult\Error $error */
            $error = $this->_rateErrorFactory->create(
                [
                    'data' => [
                        'carrier' => $this->_code,
                        'carrier_title' => $this->getConfigData('title'),
                        'error_message' => $this->getConfigData('specificerrmsg'),
                    ],
                ]
            );
            $result->append($error);
        }

        return $result;
    }

FYI -可能的M2核心问题:

干杯!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59949424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档