0

I'm trying to add a few custom customer attributes, I've created a simple module to install the attributes but my InstallData.php file keeps failing.

Here's my code:

<?php
 namespace CustomModules\CustomerNewsletters\Setup;

 use Magento\Eav\Setup\EavSetup;
 use Magento\Eav\Setup\EavSetupFactory;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;

 class InstallData implements InstallDataInterface {
   private $eavSetupFactory;

 public function __construct(EavSetupFactory $eavSetupFactory) {
    $this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'new_products_newsletters',
        [
            'type'         => 'int',
            'label'        => 'New Products Newsletter',
            'input'        => 'boolean',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ],
        'exclusive_offers_newsletters',
        [
            'type'         => 'int',
            'label'        => 'Exclusive Offers Newsletter',
            'input'        => 'boolean',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ],
        'discounts_sales_newsletters',
        [
            'type'         => 'int',
            'label'        => 'Discounts and Sales Newsletter',
            'input'        => 'boolean',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ]
    );
    $newProductsNewsletter = $this->eavConfig->getAttribute(Customer::ENTITY, 'new_products_newsletters');
    $exclusiveOffersNewsletter = $this->eavConfig->getAttribute(Customer::ENTITY, 'exclusive_offers_newsletters');
    $discountSalesNewsletter = $this->eavConfig->getAttribute(Customer::ENTITY, 'discounts_sales_newsletters');

    // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
    $newProductsNewsletter->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $exclusiveOffersNewsletter->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $discountSalesNewsletter->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $newProductsNewsletter->save();
    $exclusiveOffersNewsletter->save();
    $discountSalesNewsletter->save();

    $this->removeAttribute('customer', 'new_products_newsletter');
    $this->removeAttribute('customer', 'exclusive_offers_newsletter');
    $this->removeAttribute('customer', 'discounts_and_sales_newsletter');
}

}

The error I get when running php bin/magento setup:upgrade:

Installing data... Notice: Undefined property: CustomModules\CustomerNewsletters\Setup\InstallData::$eavConfig in C:\xampp\htdocs\hooplaHooray\app\code\CustomModules\CustomerNewsletters\Setup\InstallData.php on line 55

2

On line 55 (and following) you call $this->eavConfig, but that property does not exist in your class.

You need to edit your constructor:

private $eavConfig;

public function __construct(
    EavSetupFactory $eavSetupFactory,
    Config $eavConfig
) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
}

And add the following use-statement:

use Magento\Eav\Model\Config;
1
  • Thank you, that wasn't mentioned in the guide I followed haha
    – Howliee
    Sep 6 '18 at 9:45
1

Please try Below code...

class InstallData implements InstallDataInterface {

private $customerSetupFactory;

/**
 * Constructor
 *
 * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
 */
public function __construct(
    CustomerSetupFactory $customerSetupFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
}

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

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'new_products_newsletters', [
        'type' => 'int',
        'label' => 'New Products Newsletter',
        'input' => 'boolean',
        'source' => '',
        'required' => false,
        'visible' => true,
        'position' => 333,
        'system' => false,
        'backend' => ''
    ]);

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

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'exclusive_offers_newsletters', [
        'type' => 'int',
        'label' => 'Exclusive Offers Newsletter',
        'input' => 'boolean',
        'source' => '',
        'required' => false,
        'visible' => true,
        'position' => 333,
        'system' => false,
        'backend' => ''
    ]);

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

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'discounts_sales_newsletters', [
        'type' => 'varchar',
        'label' => 'Discounts and Sales Newsletter',
        'input' => 'text',
        'source' => '',
        'required' => true,
        'visible' => true,
        'position' => 333,
        'system' => false,
        'backend' => ''
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'discounts_sales_newsletters')
    ->addData(['used_in_forms' => [
            'adminhtml_customer',
            'adminhtml_checkout',
            'customer_account_create',
            'customer_account_edit'
        ]
    ]);
    $attribute->save();
}
}
1
  • please don't downvote this reply, he still new user and I think he's just still need know more and get help from us. appreciating to anyone who want contribute on this forum :)
    – mrfizh
    Sep 6 '18 at 10:52
0

Replace below code there were missing some dependency i have added on it .

like as

use Magento\Eav\Model\Config; use Magento\Customer\Model\Customer;

Also use $eavSetup->removeAttribute in the place of $this->removeAttribute

<?php
 namespace CustomModules\CustomerNewsletters\Setup;
 use Magento\Eav\Setup\EavSetup;
 use Magento\Eav\Setup\EavSetupFactory;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;
 use Magento\Eav\Model\Config;
 use Magento\Customer\Model\Customer;
 class InstallData implements InstallDataInterface {
   private $eavSetupFactory;
   private $eavConfig;
 public function __construct(EavSetupFactory $eavSetupFactory ,  Config  $eavConfig) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'new_products_newsletters',
        [
            'type'         => 'int',
            'label'        => 'New Products Newsletter',
            'input'        => 'boolean',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ],
        'exclusive_offers_newsletters',
        [
            'type'         => 'int',
            'label'        => 'Exclusive Offers Newsletter',
            'input'        => 'boolean',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ],
        'discounts_sales_newsletters',
        [
            'type'         => 'int',
            'label'        => 'Discounts and Sales Newsletter',
            'input'        => 'boolean',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ]
    );
    $newProductsNewsletter = $this->eavConfig->getAttribute(Customer::ENTITY, 'new_products_newsletters');
    $exclusiveOffersNewsletter = $this->eavConfig->getAttribute(Customer::ENTITY, 'exclusive_offers_newsletters');
    $discountSalesNewsletter = $this->eavConfig->getAttribute(Customer::ENTITY, 'discounts_sales_newsletters');

    // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
    $newProductsNewsletter->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $exclusiveOffersNewsletter->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $discountSalesNewsletter->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $newProductsNewsletter->save();
    $exclusiveOffersNewsletter->save();
    $discountSalesNewsletter->save();

    $eavSetup->removeAttribute('customer', 'new_products_newsletter');
    $eavSetup->removeAttribute('customer', 'exclusive_offers_newsletter');
    $eavSetup->removeAttribute('customer', 'discounts_and_sales_newsletter');
}
}

Your Answer

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

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