0

How to disable module programmatically using UpgradeData script?

Edit 2

Code that I'm trying to use to disable modules:

<?php
namespace Config\Bakalland\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Config\Model\ResourceModel\Config;
use Magento\Framework\Module\StatusFactory;

class UpgradeData implements UpgradeDataInterface
{

    /**
     * @var StatusFactory
     */
    protected $moduleStatusFactory;

    /**
     * UpgradeData constructor
     *
     * @param StatusFactory $moduleStatusFactory
     */
    public function __construct(StatusFactory $moduleStatusFactory)
    {
        $this->moduleStatusFactory = $moduleStatusFactory->create();
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '2.1.22', '<')) {
            $this->moduleStatusFactory->setIsEnabled(false, ['Magento_Downloadable', 'Magento_Paypal', 'Magento_Vault']);
        }

        $setup->endSetup();
    }

}
1
3

To disable a module using the UpgradeData setup script, try to create an UpgradeData.php file with the below code

<?php

namespace **<YOUR_VENDOR>**\**<YOUR_MODULENAME>**\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Module\StatusFactory;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var StatusFactory
     */
    protected $moduleStatusFactory;

    /**
     * UpgradeData constructor
     *
     * @param StatusFactory $moduleStatusFactory
     */
    public function __construct(
        StatusFactory $moduleStatusFactory
    ) {
        $this->moduleStatusFactory = $moduleStatusFactory->create();
    }

    /**
     * {@inheritdoc}
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '**<HIGHER_MODULE_VERSION>**', '<')) {
            $this->moduleStatusFactory->setIsEnabled(false, ['Magento_Downloadable','Magento_Paypal','Magento_Vault']);
        }

        $setup->endSetup();
    }   

}

replace the marked bold code with your data and run php bin/magento setup:upgrade from command line.

5
  • Hi, not a thing happens after using your code see my edit. I'm trying to disable: Magento_Downloadable, Magento_Paypal nad Magento_Vault modules
    – Marceli Po
    Aug 1 '17 at 8:21
  • Hey the code is an example to disable the current module not other modules. If you want to disable other modules then remove the function getModuleName() as is not needed and also replace [$this->getModuleName()] with ['Magento_Downloadable','Magento_Paypal','Magento_Vault'] (array of the modules you want to disable)
    – Nikolas
    Aug 1 '17 at 9:18
  • Still nothing. I'm working on 2.1.7 version of Magento. Is there any way to debug this upgrade process to see if it's getting to IF statment? Edit 2 shows code I have right now.
    – Marceli Po
    Aug 1 '17 at 9:37
  • Worked fine for me in Magento 2.1.7 . Just add a die('foo'); after the if statement and run php bin/magento setup:upgrade from command line. I updated my answer.
    – Nikolas
    Aug 1 '17 at 10:13
  • Still nothing even die; is not working :/ I'm investigating it. Thx in advance.
    – Marceli Po
    Aug 1 '17 at 11:39

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.