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";