1

Target:

Using plugin before method, want to changes some behavior of method collectRates(RateRequest $request) of Magento\OfflineShipping\Model\Carrier\Freeshipping

Issue:

Getting 500 Internal Server Error for parameters of beforeCollectRates().

Error message: Warning: array_values() expects parameter 1 to be array, object

{"message":"Warning: array_values() expects parameter 1 to be array, object given in /var/www/html/extension/vendor/magento/framework/Interception/Interceptor.php on line 146","trace":"#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'array_values() ...', '/var/www/html/e...', 146, Array)\n#1 /var/www/html/extension/vendor/magento/framework/Interception/Interceptor.php(146): array_values(Object(Magento\Quote\Model\Quote\Address\RateRequest))\n#2 /var/www/html/extension/var/generation/Magento/OfflineShipping/Model/Carrier/Freeshipping/Interceptor.php(26): Magento\OfflineShipping\Model\Carrier\Freeshipping\Interceptor->___callPlugins('collectRates', Array, Array)\n#3 /var/www/html/extension/vendor/magento/module-shipping/Model/Shipping.php(293): Magento\OfflineShipping\Model\Carrier\Freeshipping\Interceptor->collectRates(Object(Magento\Quote\Model\Quote\Address\RateRequest))\n#4 /var/www/html/extension/vendor/magento/module-shipping/Model/Shipping.php(209): Magento\Shipping\Model\Shipping->collectCarrierRates('freeshipping', Object(Magento\Quote\Model\Quote\Address\RateRequest))\n#5 /var/www/html/extension/vendor/magento/module-quote/Model/Quote/Address.php(995): Magento\Shipping\Model\Shipping->collectRates(Object(Magento\Quote\Model\Quote\Address\RateRequest))\n#6 /var/www/html/extension/vendor/magento/module-quote/Model/Quote/Address.php(936): Magento\Quote\Model\Quote\Address->requestShippingRates()\n#7 /var/www/html/extension/vendor/magento/module-quote/Model/Quote/Address/Total/Shipping.php(161): Magento\Quote\Model\Quote\Address->collectShippingRates()\n#8 /var/www/html/extension/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))\n#9 /var/www/html/extension/vendor/magento/module-quote/Model/ShippingMethodManagement.php(273): Magento\Quote\Model\Quote\TotalsCollector->collectAddressTotals(Object(Magento\Quote\Model\Quote), Object(Magento\Quote\Model\Quote\Address))\n#10 /var/www/html/extension/vendor/magento/module-quote/Model/ShippingMethodManagement.php(213): Magento\Quote\Model\ShippingMethodManagement->getShippingMethods(Object(Magento\Quote\Model\Quote), Array)\n#11 [internal function]: Magento\Quote\Model\ShippingMethodManagement->estimateByExtendedAddress('8', Object(Magento\Quote\Model\Quote\Address))\n#12 /var/www/html/extension/vendor/magento/module-webapi/Controller/Rest.php(307): call_user_func_array(Array, Array)\n#13 /var/www/html/extension/vendor/magento/module-webapi/Controller/Rest.php(216): Magento\Webapi\Controller\Rest->processApiRequest()\n#14 /var/www/html/extension/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))\n#15 /var/www/html/extension/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))\n#16 /var/www/html/extension/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()\n#17 /var/www/html/extension/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))\n#18 {main}"}

Code:

<?php
namespace Devamitbera\CoreRewrite\Plugin;

class FreeShippingPlugin
{
    public function beforeCollectRates(
        \Magento\OfflineShipping\Model\Carrier\Freeshipping $subject ,
            \Magento\Quote\Model\Quote\Address\RateRequest $request)
    {

        return $request;

    }

}

Question:

Why i getting issue at parameters of beforeCollectRates()?

2

try

return [$request];

The Interceptor is looking for an array as return value from the before method:

            $beforeResult = $pluginInstance->$pluginMethod($this, ...array_values($arguments));
            if ($beforeResult) {
                $arguments = $beforeResult;
            }

            [..snip..]

            $result = parent::$method(...array_values($arguments));

see here.

  • Thanks Kristof at Fooman, The error been been vanish.But when i have return return [false]; then shipping method does not disabled – Amit Bera Jun 28 '17 at 10:52
2

Adding to @Kristof at Fooman answer,

When you set to return false to in your before plugin , Magento itself call the original method thats why your return false seems not working. In that case you can use after plugin to change the return value. As per I know before plugin change only the arguments of the method not the return value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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