0

I am trying to add a mass action function to the orders grid in the admin panel, using the following code placed in:

app/code/vendor/module/Controller/Order

MassDelete.php

<?php

namespace Module\Core\Controller\Adminhtml\Order;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Sales\Api\OrderManagementInterface;
use Module\Core\Model\Config;

class MassDelete extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
{

    protected $orderManagement;
//    private $config;

    // Constructor
    public function __construct(
        Context $context, 
        Filter $filter, 
        CollectionFactory $collectionFactory, 
        OrderManagementInterface $orderManagement
//        Config $config
        ) {
        parent::__construct($context, $filter);
        $this->collectionFactory = $collectionFactory;
        $this->orderManagement = $orderManagement;
//        $this->config = $config;
    }


    protected function massAction(AbstractCollection $collection)
    {
//        if ($this->config->isEnabled()) {
            $countDeleteOrder = 0;
            $model = $this->_objectManager->create('Magento\Sales\Model\Order');
            foreach ($collection->getItems() as $order) {
                if (!$order->getEntityId()) {
                    continue;
                }
                $loadedOrder = $model->load($order->getEntityId());
                $loadedOrder->delete();
                $countDeleteOrder++;
            }
            $countNonDeleteOrder = $collection->count() - $countDeleteOrder;

            if ($countNonDeleteOrder && $countDeleteOrder) {
                $this->messageManager->addError(__('%1 order(s) have not been deleted', $countNonDeleteOrder));
            } elseif ($countNonDeleteOrder) {
                $this->messageManager->addError(__('Something went wrong. No orders have been deleted'));
            }

            if ($countDeleteOrder) {
                $this->messageManager->addSuccess(__('You successfully deleted %1 order(s)', $countDeleteOrder));
            }

            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath($this->getComponentRefererUrl());
            return $resultRedirect;
        }
//    }


}

Above code works and deletes the selected orders. But as you can see I commented out some code which prevents it from working properly, because I want it to be possible to enable or disable the MassDelete function in the admin panel. For that, I have created the following files:

I have a Config.php in

app/code/vendor/module/Model

with following code:

namespace Module\Core\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Config
{
    const XML_PATH_ENABLED = 'module_core_massdelete/delete_orders/enabled';

    private $config;

    public function __construct(ScopeConfigInterface $config)
    {
        $this->config = $config;
    }

    public function isEnabled()
    {
        return $this->config->getValue(self::XML_PATH_ENABLED);
    }
}

a config.xml in

app/code/vendor/module/etc

with code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <module_core_massdelete>
            <delete_orders>
                <enabled>0</enabled>
            </delete_orders>
        </module_core_massdelete>
    </default>
</config>

and lastly a system.xml in

app/code/vendor/module/etc/adminhtml

with code

<?xml version="1.0" ?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="custom-tab" translate="label" sortOrder="100000" class="custom-tab">
            <label>module</label>
        </tab>
        <section id="module_core_massdelete" translate="label" type="text" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="-200">
            <label>Delete Orders</label>
            <resource>Module_Core::config</resource>
            <tab>custom-tab</tab>
            <group id="delete_orders" translate="label" type="text" showInDefault="1" showInStore="1" showInWebsite="0" sortOrder="5">

                <label>Delete orders</label>
                <field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Enable function</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

But somehow, when making a reference to Module\Core\Model\Config in MassDelete.php, the functions won't work and throws a HTTP request 500. Is there someone who knows a solution to this?

1 Answer 1

1

First of all to define a setting you will need a system.xml . the config.xml is to fill in values when the user does not define it but it doesnt create them

So in etc/adminhtml you create a system.xml

There you define a new tab , section , group and field for your setting like this :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
    <system>
        <tab id="your_tab" translate="label" sortOrder="100">
            <label>Tab Label</label>
        </tab>
        <section id="your_section" translate="label" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Section Label</label>
            <tab>your_tab</tab>
            <resource>Your_Module::config</resource>
            <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Settings</label>
                <field id="enable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Next you use a Helper and not a model to get the settings Class Settings.php in Vendor/Module/Helper

<?php


namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;

class Settings extends AbstractHelper {

    protected $_scopeConfig;

    const SECTION = 'your_section';
    const GROUP = 'general';
    const SCOPE = ScopeInterface::SCOPE_STORE;


    public function __construct(
        Context $context
    )
    {
        parent::__construct($context);
    }

    protected function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
            $config_path, self::SCOPE
        );
    }

    protected function formConfigPath($field){
        $group = self::GROUP;
        $section = self::SECTION;

        $path = $section . '/' . $group . '/' . $field;

        return $path;
    }

    public function getEnabled(){
        $field = 'enable';
        $path = $this->formConfigPath($field);
        return $this->getConfig($path);
    }
}

Last thing to do it to include the Helper in the file where you want to do something with it.

So use it in that class

use Vendor\Module\Helper\Settings

protected $settings;
public function __construct(
.....
Settings $settings
....
){
$this->settings = $settings;
}

public function YourFunction (){
$enabled = $this->settings->getEnabled();

if ($enabled == true){
//code if enabled
}
}
3
  • Okay thanks! It works now! Only one thing: if an order gets deleted succesfully, it throws a succes message on the page. I've included an 'else' in my if statement in MassDelete.php which throws an error message when the function has been disabled, but this keeps won't work an throws a white screen. Have to figure that out now.
    – Condor
    Mar 19, 2018 at 15:55
  • If it works please accept my answer and upvote it... its the drive of this community. If you do not have a return function after your if statement it has no link to go back to so keep the $resultredirect thing outside of your if statement Mar 19, 2018 at 23:01
  • Yep, I was gonna accept your answer, no worries! :) I've managed to get it all working! Thank you very much.
    – Condor
    Mar 20, 2018 at 7:25

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.