3

I want to add a multiselect dropdown attribute to Customer module in Customer Account Tab (Admin Panel) and Want its value from my another module.

Please let me suggest how can I achieve this.

I am using Magento 1.9.2.4

  • Customer module in Customer Account Tab ?? Where exactly front end or admin? – Gopal Patel Dec 2 '16 at 6:18
  • @Gopal, Admin side Customer Module's Customer Account Tab. – Yogita Dec 2 '16 at 6:33
1

etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <YourCompany_YourModule>
      <version>0.1.0</version>
    </YourCompany_YourModule>
  </modules>
  <global>
    <helpers>
      <yourmodule>
        <class>YourCompany_YourModule_Helper</class>
      </yourmodule>
    </helpers>
    <models>
      <yourmodule>
        <class>YourCompany_YourModule_Model</class>
        <resourceModel>yourmodule_mysql4</resourceModel>
      </yourmodule>
    </models>
    <resources>
      <customerattribute1480661105_setup>
        <setup>
          <module>YourCompany_YourModule</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </customerattribute1480661105_setup>
      <customerattribute1480661105_write>
        <connection>
          <use>core_write</use>
        </connection>
      </customerattribute1480661105_write>
      <customerattribute1480661105_read>
        <connection>
          <use>core_read</use>
        </connection>
      </customerattribute1480661105_read>
    </resources>
  </global>
</config> 

Helper/Data.php

<?php
class YourCompany_YourModule_Helper_Data extends Mage_Core_Helper_Abstract
{
}

sql/customerattribute1480661105_setup/mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "customer_attribute",  array(
    "type"     => "text",
    "backend"  => "",
    "label"    => "Customer Attribute",
    "input"    => "multiselect",
    "source"   => "yourmodule/eav_entity_attribute_source_customeroptions14806611050",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "customer_attribute");


$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
        $attribute->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)
        ;
        $attribute->save();



$installer->endSetup();

Model/Eav/Entity/Attribute/Source/Customeroptions14806611050.php

<?php
class YourCompany_YourModule_Model_Eav_Entity_Attribute_Source_Customeroptions14806611050 extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    /**
     * Retrieve all options array
     *
     * @return array
     */
    public function getAllOptions()
    {
    //*******Here I have given sample values instead of this just retrieve your values in this format*******************
        if (is_null($this->_options)) {
            $this->_options = array(

                array(
                    "label" => Mage::helper("eav")->__("1"),
                    "value" =>  1
                ),

                array(
                    "label" => Mage::helper("eav")->__("2"),
                    "value" =>  2
                ),

                array(
                    "label" => Mage::helper("eav")->__("3"),
                    "value" =>  3
                ),

                array(
                    "label" => Mage::helper("eav")->__("5"),
                    "value" =>  4
                ),

            );
        }
        return $this->_options;
    }

    /**
     * Retrieve option array
     *
     * @return array
     */
    public function getOptionArray()
    {
        $_options = array();
        foreach ($this->getAllOptions() as $option) {
            $_options[$option["value"]] = $option["label"];
        }
        return $_options;
    }

    /**
     * Get a text for option value
     *
     * @param string|integer $value
     * @return string
     */
    public function getOptionText($value)
    {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option["value"] == $value) {
                return $option["label"];
            }
        }
        return false;
    }

    /**
     * Retrieve Column(s) for Flat
     *
     * @return array
     */
    public function getFlatColums()
    {
        $columns = array();
        $columns[$this->getAttribute()->getAttributeCode()] = array(
            "type"      => "tinyint(1)",
            "unsigned"  => false,
            "is_null"   => true,
            "default"   => null,
            "extra"     => null
        );

        return $columns;
    }

    /**
     * Retrieve Indexes(s) for Flat
     *
     * @return array
     */
    public function getFlatIndexes()
    {
        $indexes = array();

        $index = "IDX_" . strtoupper($this->getAttribute()->getAttributeCode());
        $indexes[$index] = array(
            "type"      => "index",
            "fields"    => array($this->getAttribute()->getAttributeCode())
        );

        return $indexes;
    }

    /**
     * Retrieve Select For Flat Attribute update
     *
     * @param int $store
     * @return Varien_Db_Select|null
     */
    public function getFlatUpdateSelect($store)
    {
        return Mage::getResourceModel("eav/entity_attribute")
            ->getFlatUpdateSelect($this->getAttribute(), $store);
    }
}

Create function to get all options of your another module and retrieve it in function getAllOptions() of given model.

  • Thanks I will try this and if works for me, I will accept it as a solution. – Yogita Dec 2 '16 at 7:47

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.