3
$installer = $this;

$installer->startSetup();

$installer->addAttribute('customer','badge', array( 
    'label'             => 'Badge',
    'type'              => 'text',    //backend_type
    'input'             => 'multiselect', //frontend_input
    'backend'           => 'eav/entity_attribute_backend_array',    
    'global'            =>  Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source'            => 'marketplace/eav_entity_attribute_source_badge', // Goes to Step 2
    'visible'           => true,
    'required'          => false,
    'default'           => '',
    'frontend'          => '',
    'unique'            => false,
    'note'              => ''
));

Mage::getSingleton('eav/config')
    ->getAttribute('customer', 'badge')
    ->setData('used_in_forms', array(
        'customer_account_create', 'customer_account_edit', 'customer_address_edit',
        'checkout_onepage_register', 'checkout_onepage_register_guest', 'checkout_onepage_billing_address',
        'adminhtml_customer','checkout_onepage_shipping_address','checkout_multishipping_register'
    ))
    ->save();
$installer->endSetup();

My Badge,php

class Company_Marketplace_Model_System_Config_Source_Badge
    extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{

    protected $_options;

    public function toOptionArray()
    {
        $collection = Mage::getModel('marketplace/badge')->getCollection();
        $allattributes = array();
        foreach ($collection as $data) {
            $allattributes[] = array(
                'value' => $data->getBadgeName(),
                'label' => $data->getBadgeName()
            );
        }

        return $allattributes;
    }
}

Get error:

Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute()
in app\code\local\Company\Marketplace\sql\marketplace_setup\mysql4-upgrade-1.0.0-1.0.1.php on line 6

1 Answer 1

7

I assume you have this piece of xml in the config.xml file inside the <global> tag

    <resources>
        <[module]_setup>
            <setup>
                <module>Company_Marketplace</module>
            </setup>
        </[module]_setup>
    </resources> 

You need to tell magento to use the customer setup model for your setup. You can do that by transforming the markup above into:

    <resources>
        <[module]_setup>
            <setup>
                <module>Company_Marketplace</module>
                <class>Mage_Customer_Model_Resource_Setup</class> <!-- this needs to be added -->
            </setup>
        </[module]_setup>
    </resources> 

[EDIT]
add this method to your source model also:

public function getOptionText($value)
{
    $isMultiple = false;
    if (strpos($value, ',')) {
        $isMultiple = true;
        $value = explode(',', $value);
    }

    $options = $this->getAllOptions(false);

    if ($isMultiple) {
        $values = array();
        foreach ($options as $item) {
            if (in_array($item['value'], $value)) {
                $values[] = $item['label'];
            }
        }
        return $values;
    }

    foreach ($options as $item) {
        if ($item['value'] == $value) {
            return $item['label'];
        }
    }
    return false;
}
4
  • Error solved when i m click on Manage customer it will return this error Fatal error: Class Company_Marketplace_Model_System_Config_Source_Badge contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Mage_Eav_Model_Entity_Attribute_Source_Interface::getAllOptions) in F:\wamp\www\magento1\app\code\local\Company\Marketplace\Model\Eav\Entity\Attribute\Source\Badge.php on line 3
    – ND17
    Commented Mar 2, 2015 at 7:29
  • You need to implement the method getOptionText as well in your custom source model.
    – Marius
    Commented Mar 2, 2015 at 7:32
  • Please can you show that how to implement that method
    – ND17
    Commented Mar 2, 2015 at 7:34
  • @ND17 I edited my answer.
    – Marius
    Commented Mar 2, 2015 at 7:38

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.