Adding attributes to custom product only?

2019-08-10 11:46发布

问题:

noob here. I have tried to add a few attributes to my custom product (customproduct) but they end up on all products as well.

I would like to get a Group/Tab (Custom Settings) of attributes to show just under the General Tab on my Custom Product (customproduct) only. Someone who can guide me the right way?

My code looks like this: (mysql4-install-0.1.0.php)

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$setup->addAttribute('catalog_product', 'PartnerID', array(
        'group'         => 'Custom Settings',
        'input'         => 'text',
        'type'          => 'text',
        'label'         => 'Partner ID',
        'backend'       => '',
        'visible'       => 1,
        'required'      => 0,
        'user_defined' => 1,
        'searchable' => 0,
        'filterable' => 0,
        'comparable'    => 0,
        'visible_on_front' => 0,
        'visible_in_advanced_search'  => 0,
        'is_html_allowed_on_front' => 0,
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'apply_to'      => 'customproduct',
    ));

回答1:

To answer my own question, here is the answer: I needed to add the attributes to the following array and re-run the installscript. (Also, I had an error in my configuration of my new product type customproduct)

$fieldList = array(
            'PartnerId'
                        ...
    );

    // make these attributes applicable to customproduct
    foreach ($fieldList as $field) {
        $applyTo = split(',', $installer->getAttribute('catalog_product', $field, 'apply_to'));
        if (!in_array('customproduct', $applyTo)) {
            $applyTo[] = 'customproduct';
            $installer->updateAttribute('catalog_product', $field, 'apply_to', join(',', $applyTo));
        }
    }

Now the new attribute(s) are only available for customproduct.