0

I have created attribute for products using following code in magento root

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
    Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
    foreach(Mage::getModel('catalog/product')->getCollection() as $product)
    {
        $num= $product->getId();
       $test=generateEAN($num); //this is the values i have to set for the custom attribute of each product
       $installer = new Mage_Sales_Model_Mysql4_Setup;
       $attribute  = array(
        'type' => 'text',
        'label'=> 'test',
        'input' => 'text',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => $test,// i don't know this is the correct way to assign the value to each product
        'group' => "General"
    );

    $installer->addAttribute('catalog_product', 'testing', $attribute);
    $installer->endSetup();

    }
2
  • You just create attribute and set last product id as EAN....
    – Naumov
    Jul 6, 2016 at 11:02
  • First create the attribute alone, and then, iterate over product collection to update that attribute for all products.
    – Prateek
    Jul 6, 2016 at 14:57

1 Answer 1

1

Yes,it works now .Thank you for your comments. I have created attribute first and then edit the code like this .

foreach(Mage::getModel('catalog/product')->getCollection() as $product)
{
      $num= $product->getId();
      $test=generateEAN($num);
      echo $test.'<br>';
      $action = Mage::getModel('catalog/resource_product_action');
      $action->updateAttributes(array($num), array(
    'testing' => $test
));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.