0

On localhost it works well, but on online it gives page is not found ex: {domain}/accountstatus/index/suspend/key/.....

vendor/module/Controller/Adminhtml/Index/Active.php vendor/module/Controller/Adminhtml/Index/Suspend.php vendor/module/Controller/Adminhtml/Index/Vacations.php

example for Suspend.php

namespace vendow\module\Controller\Adminhtml\Index;

use Magento\Backend\App\Action\Context;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Eav\Model\Config;

class Suspend extends 
\Magento\Customer\Controller\Adminhtml\Index\AbstractMassAction
{
protected $customerRepository;
protected $eavConfig;

public function __construct(
    Context $context,
    Filter $filter,
    CollectionFactory $collectionFactory,
    CustomerRepositoryInterface $customerRepository,
    Config $eavConfig
) {
    parent::__construct($context, $filter, $collectionFactory);
    $this->customerRepository = $customerRepository;
    $this->eavConfig = $eavConfig;
}

protected function massAction(AbstractCollection $collection)
{
    $customersUpdated = 0;
    $collection = $this->filter->getCollection($this->collectionFactory->create());
    foreach ($collection->getAllIds() as $customerId) {
        // Verify customer exists
        try {
            $customer = $this->customerRepository->getById($customerId);
            $value = $this->UpdateText();
            $customer->setCustomAttribute("account_status_customer", $value);
            $this->saveAttribute($customer);
            $customersUpdated++;
            if ($customersUpdated) {
                $this->messageManager->addSuccess(__('A total of %1 record(s) were updated.', $customersUpdated));
            }
        } catch (\Exception $e) {
            $this->messageManager->addError(__('Something went wrong when save customer'));
        }
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setPath($this->getComponentRefererUrl());

        return $resultRedirect;
    }
}

protected function saveAttribute($customer)
{
    return $this->customerRepository->save($customer);
}

protected function UpdateText()
{
    $attribute = $this->eavConfig->getAttribute('customer', 'account_status_customer');
    $options = $attribute->getSource()->getAllOptions();
    foreach ($options as $option) {
        if($option['label'] == 'Suspend'){
            $value = $option['value'];
        }
    }
    return $value;
}
}

vendor/module/Setup/installData.php

<?php
namespace vendor\module\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
private $customerSetupFactory;

public function __construct(
    CustomerSetupFactory $customerSetupFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerSetup->addAttribute(Customer::ENTITY, 'account_status_customer', [
        'label' => 'Customer Account Status',
        'input' => 'select',
        'required' => false,
        'sort_order' => 100,
        'visible' => true,
        'system' => false,
        'type' => 'int',
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
        'is_used_in_grid' => true,
        'is_visible_in_grid' => true,
        'is_filterable_in_grid' => true,
        'is_searchable_in_grid' => true,
        'option' => ['values' => ['Active', 'Suspend', 'Vacations']],
    ]);

    $setup->endSetup();
}
}

view/adminhtml/ui_component/customer_listing.xml

<listingToolbar name="listing_top">

    <massaction name="listing_massaction">

        <action name="active">

            <argument name="data" xsi:type="array">

                <item name="config" xsi:type="array">

                    <item name="type" xsi:type="string">Active</item>

                    <item name="label" xsi:type="string" translate="true">Active</item>

                    <item name="url" xsi:type="url" path="accountstatus/index/active"/>

                    <item name="confirm" xsi:type="array">

                        <item name="title" xsi:type="string" translate="true">Active</item>

                        <item name="message" xsi:type="string" translate="true">Are you sure to Active selected customers?</item>

                    </item>

                </item>

            </argument>

        </action>

        <action name="suspend">

            <argument name="data" xsi:type="array">

                <item name="config" xsi:type="array">

                    <item name="type" xsi:type="string">Suspend</item>

                    <item name="label" xsi:type="string" translate="true">Suspend</item>

                    <item name="url" xsi:type="url" path="accountstatus/index/suspend"/>

                    <item name="confirm" xsi:type="array">

                        <item name="title" xsi:type="string" translate="true">Suspend</item>

                        <item name="message" xsi:type="string" translate="true">Are you sure to Suspend selected customers?</item>

                    </item>

                </item>

            </argument>

        </action>

        <action name="vacations">

            <argument name="data" xsi:type="array">

                <item name="config" xsi:type="array">

                    <item name="type" xsi:type="string">Vacations</item>

                    <item name="label" xsi:type="string" translate="true">Vacations</item>

                    <item name="url" xsi:type="url" path="accountstatus/index/vacations"/>

                    <item name="confirm" xsi:type="array">

                        <item name="title" xsi:type="string" translate="true">Vacations</item>

                        <item name="message" xsi:type="string" translate="true">Are you sure to Vacations selected customers?</item>

                    </item>

                </item>

            </argument>

        </action>

    </massaction>

</listingToolbar>

enter image description here

  • The problem is that it doesn't add Column Customer Account Status in the customer grid, this means my installData.php doesn't work in online , but work in local – Robinio Tor Feb 12 at 11:44
  • if that's the problem, try to create an updgradeData script or better, remove your module from setup_module table. Then run the upgrade again – magefms Feb 12 at 11:48

Your Answer

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

Browse other questions tagged or ask your own question.