0

My UpgradeData.php to add customer attribute which will be reflected under customer account in admin section.

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Namespace\Modulename\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Setup\EavSetup;

/**
 * @codeCoverageIgnore
 */
class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;
    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param IndexerRegistry $indexerRegistry
     * @param \Magento\Eav\Model\Config $eavConfig
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(
                Customer::ENTITY,
                'customer_product_points',
                [
                    'type' => 'int',
                    'label' => 'Customer Product Points',
                    'input' => 'text',
                    'required' => false,
                    'visible' => false,
                    'system' => false,
                ]
            );


        // add attribute to form
        /** @var  $attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customer_product_points');
        $attribute->setData('used_in_forms', ['adminhtml_customer']);
        $attribute->save();
        $setup->endSetup();
    }
}

When I upgrade, it throws below exception

Fatal error: Call to undefined method Magento\Eav\Setup\EavSetup::getEavConfig()

Controller file where I am fetching customer data

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace namespace\Modulename\Controller\Adminhtml\Index;

use \Magento\Customer\Api\Data\CustomerInterface;;

class Points extends \Magento\Customer\Controller\Adminhtml\Index
{
    /**
     * Customer compare grid
     *
     * @return \Magento\Framework\View\Result\Layout
     */
    public function execute()
    {

        $customerId = $this->initCurrentCustomer();



        $customerData = [];
        $customerData['account'] = [];
        $customerData['address'] = [];
        $customer = null;
        $isExistingCustomer = (bool)$customerId;
        if ($isExistingCustomer) {
            try {
                $customer = $this->_customerRepository->getById($customerId);
                $customerData['account'] = $this->customerMapper->toFlatArray($customer);
                $customerData['account'][CustomerInterface::ID] = $customerId;
                try {
                    $addresses = $customer->getAddresses();
                    foreach ($addresses as $address) {
                        $customerData['address'][$address->getId()] = $this->addressMapper->toFlatArray($address);
                        $customerData['address'][$address->getId()]['id'] = $address->getId();
                    }
                } catch (NoSuchEntityException $e) {
                    //do nothing
                }
            } catch (NoSuchEntityException $e) {
                $this->messageManager->addException($e, __('Something went wrong while editing the customer.'));
                $resultRedirect = $this->resultRedirectFactory->create();
                $resultRedirect->setPath('customer/*/index');
                return $resultRedirect;
            }
        }
        $customerData['customer_id'] = $customerId;

        // set entered data if was error when we do save
        $data = $this->_getSession()->getCustomerData(true);

        // restore data from SESSION
        /*if ($data && (!isset(
                $data['customer_id']
                ) || isset(
                $data['customer_id']
                ) && $data['customer_id'] == $customerId)
        ) {
            $request = clone $this->getRequest();
            $request->setParams($data);

            if (isset($data['account']) && is_array($data['account'])) {
                $customerForm = $this->_formFactory->create(
                    'customer',
                    'adminhtml_customer',
                    $customerData['account'],
                    true
                );
                $formData = $customerForm->extractData($request, 'account');
                $customerData['account'] = $customerForm->restoreData($formData);
                $customer = $this->customerDataFactory->create();
                $this->dataObjectHelper->populateWithArray(
                    $customer,
                    $customerData['account'],
                    '\Magento\Customer\Api\Data\CustomerInterface'
                );
            }
        }*/

        $this->_getSession()->setCustomerData($customerData);
        echo '<pre>'; print_r($customerData); exit;
    }
}
  1. How do I fetch my custom attribute customer_product_points ?
  2. Have I missed any step while creating customer attribute programmatically ?
1
  • @DigitalPianism, its about upgrading data and not installing data. class UpgradeData implements UpgradeDataInterface
    – DEEP JOSHI
    Feb 26, 2016 at 10:47

1 Answer 1

0

Not sure about best practices on stackoverflow but i think it is better to keep questions separate in different threads. Anyway, as for question 2:

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->getAttribute();

Just run getAttribute() directly instead. You do not need getEavConfig()

1
  • After removing getEavConfig() getting following error. Fatal error: Call to a member function setData() on a non-object
    – DEEP JOSHI
    Feb 26, 2016 at 11:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.