0

I added a custom attribute value that is displayed on the customer grid and on the customer view page (adminhtml), but when I want to change and save an attribute from customer grid that works in the grid , in the customer view it appears correctly, but when I change it from the customer view page it doesn't work.

<?php
namespace CustomersState\AccountState\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 Active 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("customer_states", $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', 'customer_states');
    $options = $attribute->getSource()->getAllOptions();
    foreach ($options as $option) {
        if ($option['label'] == 'Active') {
            $value = $option['value'];
        }elseif($option['label'] == 'Suspend'){
            $value = $option['value'];
        }
    }
    return $value;
    }
}

ui_component/customer_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <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="accountstate/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="accountstate/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="accountstate/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>
</listing>

Setup/InstallData.php

<?php
namespace CustomersState\AccountState\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, 'customer_states', [
        'label' => 'Customer Account Status',
        'input' => 'select',
        'required' => false,
        'sort_order' => 100,
        'visible' => true,
        'system' => false,
        'type' => 'varchar',
        '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();
}

}

How can i save attribute of customer view page ?

Customer grid

Customer grid

Customer view page

Custoemr view page

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.