1

I added a new customer attribute n2gEmails:

app/code/Fekete/Newsletter2Go/etc/extension_attributes.xml:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="n2gEmails" type="string"/>
    </extension_attributes>
</config>

app/code/Fekete/Newsletter2Go/Setup/Patch/Data/AddN2gEmailsCustomerAttribute.php:

<?php

namespace Fekete\Newsletter2Go\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Setup\CustomerSetup;

class AddN2gEmailsCustomerAttribute implements DataPatchInterface, PatchRevertableInterface
{

    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * @var CustomerSetup
     */
    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'n2gEmails',
            [
                'type' => 'varchar',
                'label' => 'Newsletter2Go Emails',
                'input' => 'text',
                'source' => '',
                'required' => false,
                'visible' => true,
                'position' => 333,
                'system' => false,
                'backend' => ''
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'n2gEmails')->addData([
            'used_in_forms' => [
                'adminhtml_customer',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'n2gEmails');

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [

        ];
    }



}

Then I executed php bin/magento setup:upgrade.

How can I access this attribute now?

2 Answers 2

1

This example shows how you can access the attribute from a helper.

app\code\Fekete\Newsletter2Go\Helper\Data.php

namespace Fekete\Newsletter2Go\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{

    private $session;
    private $customer;
    private $n2g_emails;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context
       ,\Magento\Customer\Model\Session $session
    ) {

        $this->session = $session;
        $this->customer = $session->getCustomer();
        $this->n2g_emails = $this->customer->getN2gEmails();
    }

...
0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerSession = $objectManager->create('Magento\Customer\Model\Session');  

if ($customerSession->isLoggedIn()) {

$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')>create();

$customer = $customerFactory->load($customerSession->getCustomerId());

$newattr=$customer->getN2gEmails();

//echo $newattr;

}

" Hope This Will Help You !!!"

1
  • Nah, this is not the supposed way how to do it, it is really dirty code.
    – Black
    Commented Sep 17, 2019 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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