1

I am working on a project in which I have created a module to create custom customer in manufacturer customer group now I want to add this manufacturer customer group as a product attribute to product so that when I create a product from admin there should be an drop down list from the manufacturer customer group to add the manufacturer of the product.

  • So what should be the value of the product attribute? A customer id? – Fabian Schmengler Sep 7 '16 at 12:24
  • @fschmengler : It should be the name of the customer in drop down. – mudit mehrotra Sep 7 '16 at 12:25
  • @fschmengler : I mean in drop down it should list the names but in foreign key prospects there should be the customer id in the database to associate the product with that customer. Am I right? – mudit mehrotra Sep 7 '16 at 12:30
2

Write your own source model for the product attribute.

Write the source model

This is a child class of Mage_Eav_Model_Entity_Attribute_Source_Abstract. The most important method to implement is getAllOptions() which returns the possible values and labels for the dropdown:

public function getAllOptions()
{
    if (is_null($this->_options)) {
        $customers = $this->_getManufacturerCustomers();
        $this->_options = [];
        foreach ($customers as $customer) {
            $this->_options[] = [
                'label' => $customer->getName(),
                'value' =>  $customer->getId(),
            ];
        }
    }
    return $this->_options;
}
protected function _getManufacturerCustomers()
{
    // take this value from configuration!
    $manufacturerGroup = 123;

    $customers = Mage::getResourceModel('customer/customer_collection');
    $customers->addAttributeToFilter('group_id', $manufacturerGroup);

    // add attributes you need to display the customer name
    $customers->addAttributeToSelect(['firstname', 'middlename' 'lastname']);

    return $customers;
}

I wrote _getManufactureCustomers() as a draft, adjust it as needed but the principle should be clear.

Use the source model

Let's say, the product attribute is manufacturer_customer and the source model alias is stack_manufacturer/attribute_source_manufacturer. Then the install script that adds the attribute looks like this:

$installer->addAttribute('catalog_product', 'manufacturer_customer', array(
    'label'             => 'Manufacturer Customer',
    'type'              => 'int',
    'input'             => 'select',
    'source'            => 'stack_manufacturer/attribute_source_manufacturer',

    // add more parameters as required

));

Flat Index

If you want the attribute to be included in the flat index (because you need it in collections in the frontend), add these methods to the source model:

public function getFlatColums()
{
    $columns = array(
        $this->getAttribute()->getAttributeCode() => array(
            'type'      => 'int',
            'unsigned'  => false,
            'is_null'   => true,
            'default'   => null,
            'extra'     => null
        )
    );
    return $columns;
}


public function getFlatUpdateSelect($store)
{
    return Mage::getResourceModel('eav/entity_attribute')
        ->getFlatUpdateSelect($this->getAttribute(), $store);
}
  • Thanks for the answer I will try it. I am new to magento so if there is some problem in implementation will comment here :) – mudit mehrotra Sep 7 '16 at 12:59
  • can you give the file paths where I can add these codes. I am new to magento so I am not getting where from where I should start. It would be a great help. – mudit mehrotra Sep 8 '16 at 6:02
  • I can't give you a complete functioning module and you would not learn anything from it. So here are the things you need to know to get started: how to create a module; how to create models; how to create install scripts to add product attributes. There are plenty of good tutorials out there for these basics – Fabian Schmengler Sep 8 '16 at 6:41
  • 1
    change 'group' to 'group_id', this is the actual field name. – Fabian Schmengler Sep 14 '16 at 10:11
  • 1
    This is a different question. But I can tell you that much, you can configure the attribute in Catalog > Manage Attributes to make it "available in product listing" and "filterable". Also you have to add the getFlatColumns() and getFlatUpdateSelect() methods as in my answer above. – Fabian Schmengler Sep 14 '16 at 10:22

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.