0

I'm trying to create a plugin for Magento 2.1.3 that disables the flat rate shipping method in the cart, but keeps it enabled in the admin panel.

I've followed the instructions to the best of my knowledge, and Magento doesn't seem to be picking up my plugin.

Here's what I've got:

app/code/MaadDecor/DisableFlatRate/etc/di.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\OfflineShipping\Model\Carrier\Flatrate">
    <plugin name="hidePlug" type="MaadDecor\DisableFlatRate\Plugin\Hide" sortOrder="1" disabled="false"/>
</type>

app/code/MaadDecor/DisableFlatRate/Plugin/Hide.php

<?php

namespace MaadDecor\DisableFlatRate\Plugin;

use Magento\Backend\Model\Auth\Session;
use Psr\Log\LoggerInterface;

class Hide
{
    protected $backendSession;

    /**
    * @var LoggerInterface
    */

    private $logger;

    public function __construct(Session $session, LoggerInterface $logger)
    {
        $this->backendSession = $session;
        $this->logger = $logger;
    }

    public function aroundCollectRates($subject, callable $proceed)
    {

        $this->logger->info('hit before the auth check');
        if ($this->backendSession->isLoggedIn()) {
            return $proceed();
        }
        $this->logger->info('hit after the auth check');
        //        return $proceed();
        return false;
    }    
}

For some reason none of the logs are being added, and the shipping rate still displays on the front end.

Any ideas?

2 Answers 2

0

you need to move your di.xml from

app/code/MaadDecor/DisableFlatRate/etc/di.xml

to

app/code/MaadDecor/DisableFlatRate/di.xml 

Now clean cache and then try.

2
  • That didn't do anything, and I see in all the other packages that their di.xml is in the etc subfolder.
    – Laravelian
    Commented Feb 20, 2017 at 18:27
  • have you try caching and then check. Commented Feb 21, 2017 at 10:24
0

Replace the aroundCollectRates Function with this :

public function aroundCollectRates(
    Magento\OfflineShipping\Model\Carrier\Flatrate $subject,
    \Closure $proceed
) {
    $this->logger->info('hit before the auth check');
    if ($this->backendSession->isLoggedIn()) {
        return $proceed();
    }
    $this->logger->info('hit after the auth check');
    //        return $proceed();
    return false;
}    

Please reply if this solves your problem.

2
  • This doesn't make a difference. I just dont think this method is being called somehow.
    – Laravelian
    Commented Feb 20, 2017 at 18:28
  • Well this is one of the obsolete_classes in Magento 2, That might be the reason Or It the flatrate method is disabled from admin. Commented Feb 21, 2017 at 6:40

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.