4

I use this code to set the "promotion code " attribute but in some way this attribute is aviable only from admin panel. This is the code :

<?php
namespace Riccardo\Plugin\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->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion_code");

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion_code",  array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Promotion Code",
            "input"    => "text",
            "source"   => "",
            "visible"  => true,
            "required" => false,
            "default" => "",
            "frontend" => "Promotion Code",
            "unique"     => false,
            "note"       => ""

        ));

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

        $promotion_code = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'promotion_code');

        $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";
        $promotion_code->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);

        $promotion_code->save();



        $installer->endSetup();
    }
}

What's option I need to set the attribute for all forms, admin and frontend?

  • Hi this custom attribute has a common value for every customer or want get a value from the customer while register? – Mujahidh Jan 3 '16 at 10:32
  • Thank you for the answer. I want that every time new customer get a registration , he can insert the promotion code or also not. Is it possible? – Riccardo Amadio Jan 3 '16 at 11:54
  • What is promotion code and for what you plan to use it? – Mujahidh Jan 3 '16 at 12:17
  • The Promotion Code is an attribute of customer that I need to use in my module. When new customer wants to creat an account he could insert a promotion code or not instert it. Are you understand ? – Riccardo Amadio Jan 3 '16 at 12:19
  • Yes i understood.For what you are collecting that information(promotion code)? – Mujahidh Jan 3 '16 at 12:21
1

If you use Community Edition you need to add this field in the template manually. In Enterprise Edition field should display on registration form with out template changes.

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.