2

I create one plugin Vendor\Test\Plugin\Shipping for model Magento\Shipping\Model\Shipping.php using di.xml

app/code/Vendor/Test/etc/di.xml

<type name="Magento\Shipping\Model\Shipping">
    <plugin name="shipping_test" type="Vendor\Test\Plugin\Shipping" sortOrder="1"/>
</type>

app/code/Vendor/Test/Plugin/Shipping.php

<?php

namespace Vendor\Test\Plugin;

use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\Quote\Address\RateRequest;
//use Magento\Shipping\Model\Rate\Result;
use Magento\Checkout\Model\Cart;
use Vendor\Test\Helper\Data;
use Magento\Quote\Model\Quote\Address\RateResult\Error;
use Magento\Shipping\Model\Shipment\Request;
use Magento\Sales\Model\Order\Shipment;
use Magento\Quote\Model\Quote\Address\RateCollectorInterface;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Shipping
{

    protected $_code='Shipping';

    protected $_rateResultFactory;

    protected $_rateMethodFactory;

    protected $_result;

    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,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\App\Filesystem\DirectoryList $directory_list,
        \Vendor\Test\Helper\Data $helper,
        \Magento\Framework\App\ResourceConnection $resourceConnection,
        array $data = []
    ) {
        $this->_rateResultFactory = $rateResultFactory;
        $this->_rateMethodFactory = $rateMethodFactory;
        $this->directory_list = $directory_list;
        $this->cart = $cart;
        $this->helper = $helper;
        $this->resourceConnection = $resourceConnection;
    }

    public function getAllowedMethods()
    {
        $group_info=$this->helper->getAllMethods();
        $carriers=array();
        foreach ($group_info as &$carrier) {
            $carriers[$group_info['label']]=$group_info['label'];
        }
        return $carriers;
    }

    public function afterCollectRates()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');

        if ($customerSession->isLoggedIn()) {
            $gid=$customerSession->getCustomerGroupId();
            $group_info=$this->helper->getGroupData($gid);
        }

        $err = null;
        $this->_result = $this->_rateResultFactory->create();
        $method = $this->_rateMethodFactory->create();
        $method->setCarrier('Shipping');
        $method->setCarrierTitle('sdtgd');
        $method->setMethod('Shipping');
        $method->setMethodTitle('dryfyu');
        $amount=0;

        return $this->getResult();
    }

    public function getResult()
    {
        return $this->_result;
    }

}

?>

But i got the below error. What is issue in this code ?

s:1150:"Fatal Error: 'Uncaught Error: Call to undefined method Magento\Shipping\Model\Rate\Result::getResult() in /var/www/html/magento/m2/mage2/vendor/magento/module-quote/Model/Quote/Address.php:995
Stack trace:
#0 /var/www/html/magento/m2/mage2/vendor/magento/module-quote/Model/Quote/Address.php(936): Magento\Quote\Model\Quote\Address->requestShippingRates()
#1 /var/www/html/magento/m2/mage2/vendor/magento/module-quote/Model/Quote/Address/Total/Shipping.php(161): Magento\Quote\Model\Quote\Address->collectShippingRates()
#2 /var/www/html/magento/m2/mage2/vendor/magento/module-quote/Model/Quote/TotalsCollector.php(265): Magento\Quote\Model\Quote\Address\Total\Shipping->collect(Object(Magento\Quote\Model\Quote), Object(Magento\Quote\Model\ShippingAssignment), Object(Magento\Quote\Model\Quote\Address\Total))
#3 /var/www/html/magento/m2/mage2/vendor/magento/module-quote/Model/ShippingMethodManagement.php(273): Magento\Quote\Model\Quote\TotalsCollector->collectAddressTotals(Objec' in '/var/www/html/magento/m2/mage2/vendor/magento/module-quote/Model/Quote/Address.php' on line 995";

1 Answer 1

1

The collectRates method should return the Magento\Quote\Model\Quote\Address\RateCollectorInterface:

interface RateCollectorInterface
{
    /**
     * @param RateRequest $request
     * @return $this
     */
    public function collectRates(RateRequest $request);
}

Check is your result implements this interface and it is the instance of Magento\Shipping\Model\Rate. I think that your plugin returns result, but it should return the rate.

Update:

I have tried to check your plug-in, and have found that original method result is instance of the Magento\Shipping\Model\Shipping with the result inside:

debug

So your plugin should return the Shipping (I think this is a shipping method instance).

12
  • Means i use in plugin afterCollectRates(RateRequest $request) method right ? @SiarheyUnchukhlebau. Commented Sep 16, 2016 at 6:36
  • @PayalPatel Yes, it should return the Rate, not the Rate/Result Commented Sep 16, 2016 at 6:37
  • 1
    @PayalPatel probably this error comes from the $result = $this->_rateCollector->create()->collectRates($request)->getResult(); line Commented Sep 16, 2016 at 6:40
  • Ok Fine @SiarheyUnchukhlebau, But now one error create for it's. Commented Sep 16, 2016 at 6:54
  • 1
    @PayalPatel Please, check my answer for your second question. I test the code and it's working fine. Commented Sep 16, 2016 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.