0

Getting pretty frustrated so figured I'd seek help.

I have tried following multiple tutorials on how to do this but none of them are working.

Running Magento 2.1.5.

I am simply trying to create a customer attribute. my Setup/UpgradeData.php script is as follows:

namespace Wildcard\CustomerMods\Setup;

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

class UpgradeData implements UpgradeDataInterface
{

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     *
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface   $context
     *
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $installer = $setup;
        $installer->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->removeAttribute(Customer::ENTITY, "customer_payment_type");

        $customerSetup->addAttribute(Customer::ENTITY, "customer_payment_type", array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Payment Type",
            "input"    => "select",
            "source"   => 'Wildcard\CustomerMods\Model\Config\Source\Customer\PaymentTypeOptions',
            "visible"  => true,
            "required" => true,
            "default"  => "",
            "frontend" => "",
            "unique"   => false,
            "note"     => ""

        ));

        $my_attribute    = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, ' customer_payment_type');
        $used_in_forms[] = "adminhtml_customer";
        $used_in_forms[] = "customer_account_create";
        $used_in_forms[] = "customer_account_edit";
        $my_attribute->setData("used_in_forms", $used_in_forms)
                     ->setData("is_used_for_customer_segment", true)
                     ->setData("is_system", 0)
                     ->setData("is_user_defined", 0)
                     ->setData("is_visible", 1)
                     ->setData("sort_order", 100);
        $my_attribute->save();
        $installer->endSetup();
    }
}

I have tried increasing the version number in my module.xml. This is picked up in the setup_module table but it just refuses to create the attribute! I am looking in the admin area (new customer and edit customer) and in the eav_attribute table in the database. The attribute is not appearing in either.

Is there something wrong with the code above?

It would be great to see some debug info from Magento about this but there is nothing at all in the logs.

Please can someone help out with this? I'm just about pulling my hair out!

  • Your code is working just perfectly fine, make sure that you are using php bin\magento setup:upgrade to see the changes. – arushi May 2 '17 at 7:51
0

Try this installData.php (Change name and code of attribute as per your need)

   <?php

  namespace <vendor_name>\<module_name>\Setup;

 use Magento\Framework\Module\Setup\Migration;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;

 class InstallData implements InstallDataInterface
 {
   /**
    * Customer setup factory
    *
    * @var \Magento\Customer\Setup\CustomerSetupFactory
    */
    private $customerSetupFactory;
   /**
    * Init
    *
    * @param \Magento\Customer\Setup\CustomerSetupFactory     $customerSetupFactory
    */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
   {
       $this->customerSetupFactory = $customerSetupFactory;
   }
   /**
    * Installs DB schema for a module
    *
    * @param ModuleDataSetupInterface $setup
    * @param ModuleContextInterface $context
    * @return void
    */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
       $installer = $setup;
       $installer->startSetup();
       $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
       $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);

       $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "bank_account_number",  array(
        "type"     => "static",
        "backend"  => "",
        "label"    => "Bank Account Number",
        "input"    => "text",
        "visible"  => true,
        "required" => false,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""

       ));

       $bankaccountnumber   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "bank_account_number");

       $bankaccountnumber = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bank_account_number');
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="adminhtml_checkout";
    $bankaccountnumber->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);
      $bankaccountnumber->save();

      $installer->endSetup();
   }
 }
0

Try adding version_compare statement, for example:

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    $installer = $setup;
    $installer->startSetup();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    if (version_compare($context->getVersion(), '1.0.2') < 0) { //1.0.2 is the new version number
        //your setup code here
        ...
    }

    $installer->endSetup();
}

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.