Created a custom attribute via setup, but it only appears to new products
-
16-10-2019 - |
문제
Hello i have the next setup code:
$installer = new Mage_Catalog_Model_Resource_Setup('core_setup');
$installer->addAttribute('catalog_product', 'shipping_city', array(
'group' => 'General',
'type' => Varien_Db_Ddl_Table::TYPE_VARCHAR,
'backend' => '',
'frontend' => '',
'label' => 'Envio p/ Cidade',
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => false,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => true,
'unique' => false,
'apply_to' => 'simple,configurable,virtual',
'is_configurable' => false ));
$installer->endSetup();
Then i debug the products with de " and " event:
$product = $observer->getEvent()->getProduct();
Zend_Debug::dump($product->debug());die;
The problem is that if i try to save an already existing product, it gets the event and my debug brings not my attribute shipping_city. In a new product it appear in my debug but only in de save_after event, in the save_before doesnt appear aswell...
Any thoughts guys?
해결책
If you add a new attribute and it was added to the attribute set you want it in, everything went well with $installer->addAttribute
.
The problem is, because of the EAV there is no value for your new attribute. This means querying and filtering by it, means filtering by NULL and if you call $product->getNewAttribute()
you just get NULL
.
But this behavior is correct and intended.
You can fix this by writing a small script and setting a default value on all products, like this:
$collection = Mage::getResourceModel('catalog/product_collection')->filterByWhateverYouWant();
foreach($collection as $product) {
$product->setNewAttribute('myCoolValue')->save();
}
Be careful with this. This script makes one query for each product in the collection which might be a lot.