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 SchmenglerCommented Sep 7, 2016 at 12:24
-
@fschmengler : It should be the name of the customer in drop down.– mudit mehrotraCommented Sep 7, 2016 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 mehrotraCommented Sep 7, 2016 at 12:30
1 Answer
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 :) Commented Sep 7, 2016 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. Commented Sep 8, 2016 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 Commented Sep 8, 2016 at 6:41
-
1change 'group' to 'group_id', this is the actual field name. Commented Sep 14, 2016 at 10:11
-
1This 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()
andgetFlatUpdateSelect()
methods as in my answer above. Commented Sep 14, 2016 at 10:22