3

I could use a little guidance on writing a plugin for the Magento\Shipping\Model\Shipping->collectRates function. I have properly added the plugin to the di.xml and it's processing the around code. The trouble is, I'm getting an ObjectManager exception when running the proceed callback. At this point in the development of the plugin, I'm just trying to get the proceed to work, then I can add in the other features to my plugin.

Any ideas on what I'm doing wrong? Any help would be appreciated.

Magento Version: 2.1.6

Exception Message Notice: Undefined index: instance in vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 112

Plugin Class

class ShippingPlugin {

  protected $shippingDataBuilder = null;

  protected $overrideFactory = null;

  protected $logger = null;

  public function __construct(
      Builder $shippingDataBuilder,
      Factory $overrideFactory,
      \Psr\Log\LoggerInterface $logger)
  {
    $this->shippingDataBuilder = $shippingDataBuilder;
    $this->overrideFactory = $overrideFactory;
    $this->logger = $logger;
  }



  public function aroundCollectRates(\Magento\Shipping\Model\Shipping $subject, callable $proceed, \Magento\Quote\Model\Quote\Address\RateRequest $rateRequest){


    try {
        $results = $proceed($rateRequest);
    }
    catch (\Exception $e){
        $this->logger->error($e->getMessage());
        $this->logger->error($e->getTraceAsString());
    }

    return $results;
   }
 }

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">
    <type name="Magento\Shipping\Model\Shipping">
        <plugin name="PluginName" type="\Company\Module\Plugin\ShippingPlugin" sortOrder="1" />
    </type>
</config>

Exception Stack Trace My Plugin around function is line #21 in the trace

main.ERROR: Notice: Undefined index: instance in vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 112 [] []
main.ERROR: #0 vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(112): Magento\Framework\App\ErrorHandler->handler(8, 'Undefined index...', '/var/httpd/company...', 112, Array)
#1 vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(53): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\\Framewo...', NULL, 'context', 'Magento\\Directo...')
#2 vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(82): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Magento\\Directo...', Array, Array)
#3 vendor/magento/framework/ObjectManager/ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Directo...')
#4 vendor/magento/framework/Model/AbstractModel.php(475): Magento\Framework\ObjectManager\ObjectManager->get('Magento\\Directo...')
#5 vendor/magento/framework/Model/AbstractModel.php(214): Magento\Framework\Model\AbstractModel->_getResource()
#6 vendor/magento/module-directory/Model/Country.php(72): Magento\Framework\Model\AbstractModel->_init('Magento\\Directo...')
#7 vendor/magento/framework/Model/AbstractModel.php(193): Magento\Directory\Model\Country->_construct()
#8 vendor/magento/module-directory/Model/Country.php(61): Magento\Framework\Model\AbstractModel->__construct(Object(Magento\Framework\Model\Context), Object(Magento\Framework\Registry), NULL, NULL, Array)
#9 vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Magento\Directory\Model\Country->__construct(Object(Magento\Framework\Model\Context), Object(Magento\Framework\Registry), Object(Magento\Framework\Locale\TranslatedLists), Object(Magento\Directory\Model\Country\FormatFactory), Object(Magento\Directory\Model\ResourceModel\Region\CollectionFactory), NULL, NULL, Array)
#10 vendor/magento/framework/ObjectManager/Factory/Compiled.php(88): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Directo...', Array)
#11 vendor/magento/framework/ObjectManager/ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Compiled->create('Magento\\Directo...', Array)
#12 vendor/magento/module-directory/Model/CountryFactory.php(37): Magento\Framework\ObjectManager\ObjectManager->create('Magento\\Directo...', Array, false)
#13 vendor/magento/module-ups/Model/Carrier.php(270): Magento\Directory\Model\CountryFactory->create()
#14 vendor/magento/module-ups/Model/Carrier.php(204): Magento\Ups\Model\Carrier->setRequest(Object(Magento\Quote\Model\Quote\Address\RateRequest))
#15 vendor/magento/module-shipping/Model/Shipping.php(293): Magento\Ups\Model\Carrier->collectRates(Object(Magento\Quote\Model\Quote\Address\RateRequest))
#16 vendor/magento/module-shipping/Model/Shipping.php(209): Magento\Shipping\Model\Shipping->collectCarrierRates('ups', Object(Magento\Quote\Model\Quote\Address\RateRequest))
#17 vendor/magento/framework/Interception/Interceptor.php(74): Magento\Shipping\Model\Shipping->collectRates(Object(Magento\Quote\Model\Quote\Address\RateRequest))
#18 vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Shipping\Model\Shipping\Interceptor->___callParent('collectRates', Array)
#19 vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Shippin...', 'collectRates', Object(Magento\Shipping\Model\Shipping\Interceptor), Array, 'ProgrammaticShi...')
#20 app/code/Company/Module/Plugin/ProgrammaticShipping.php(67): Magento\Shipping\Model\Shipping\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Quote\Model\Quote\Address\RateRequest))
#21 vendor/magento/framework/Interception/Interceptor.php(142): Company\Module\Plugin\ProgrammaticShipping->aroundCollectRates(Object(Magento\Shipping\Model\Shipping\Interceptor), Object(Closure), Object(Magento\Quote\Model\Quote\Address\RateRequest))
#22 var/generation/Magento/Shipping/Model/Shipping/Interceptor.php(26): Magento\Shipping\Model\Shipping\Interceptor->___callPlugins('collectRates', Array, Array)
#23 vendor/magento/module-quote/Model/Quote/Address.php(995): Magento\Shipping\Model\Shipping\Interceptor->collectRates(Object(Magento\Quote\Model\Quote\Address\RateRequest))
#24 vendor/magento/module-quote/Model/Quote/Address.php(936): Magento\Quote\Model\Quote\Address->requestShippingRates()
#25 vendor/magento/module-quote/Model/Quote/Address/Total/Shipping.php(161): Magento\Quote\Model\Quote\Address->collectShippingRates()
#26 vendor/magento/module-quote/Model/Quote/TotalsCollector.php(265): Magento\Quote\Model\Quote\Address\Total\Shipping->collect(Object(Magento\Quote\Model\Quote\Interceptor), Object(Magento\Quote\Model\ShippingAssignment), Object(Magento\Quote\Model\Quote\Address\Total))
#27 vendor/magento/module-quote/Model/ShippingMethodManagement.php(273): Magento\Quote\Model\Quote\TotalsCollector->collectAddressTotals(Object(Magento\Quote\Model\Quote\Interceptor), Object(Magento\Quote\Model\Quote\Address))
#28 vendor/magento/module-quote/Model/ShippingMethodManagement.php(213): Magento\Quote\Model\ShippingMethodManagement->getShippingMethods(Object(Magento\Quote\Model\Quote\Interceptor), Array)
#29 vendor/magento/module-quote/Model/GuestCart/GuestShippingMethodManagement.php(103): Magento\Quote\Model\ShippingMethodManagement->estimateByExtendedAddress(173156, Object(Magento\Quote\Model\Quote\Address))
#30 [internal function]: Magento\Quote\Model\GuestCart\GuestShippingMethodManagement->estimateByExtendedAddress('9e74658791eb1f4...', Object(Magento\Quote\Model\Quote\Address))
#31 vendor/magento/module-webapi/Controller/Rest.php(307): call_user_func_array(Array, Array)
#32 vendor/magento/module-webapi/Controller/Rest.php(216): Magento\Webapi\Controller\Rest->processApiRequest()
#33 var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#34 vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#35 vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#36 index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))

2 Answers 2

2

You done a silly mistake

Magento\Shipping\Model\Shipping $subject

Should be

\Magento\Shipping\Model\Shipping $subject

As First slash is missing that $subjectdoes not get object of anby class.

Change in that class then

  • Delete var/generation
  • FLush Cache
1
  • Amit, thanks for the response. Since the code was throwing the exception in the function I added a logging line to get the class name for confirmation. The get_class($subject); code is returning "Magento\Shipping\Model\Shipping\Interceptor". I wish it was a silly mistake and I could move on but unfortunately, I'm getting the same exception.
    – Gary
    Sep 4, 2017 at 15:58
1

Turns out it was indeed a silly mistake but the mistake was in another class. The issue came from another class where I had used the constructor dependency injection to inject the ObjectManager. Once I fixed that, the collectRates around method worked like a champ.

So, for anybody else getting an exception in AbstractFactory.php, make sure you have not injected the ObjectManager.

Your Answer

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

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