0

I would like to make a mass modification to attribute sets. I would like to add a series of attributes among those not yet assigned to the attributes set (200) in the "General" section.

I would like to make this change from the database. Can anyone help me on how to proceed? What tables contain this data?

I must repeat the operation in the photo for 200 set of attributes, assigning each part a portion of unassigned attributes enter image description here

0

I wouldn't do this immediately on the database, I'd write a script for this, which changes this "the Magento way". If you run it on the database, additional scripts (including the flat catalog) will not run, so it might still not be visible on the frontend.

I would get a collection with all products that do not have a value for this specific attribute and have the attribute set you mentioned. From there, you could set the default value for that product. You could either do a $product->save(), if you have additional scripts for the product, or you could do a $product->getResource()->saveAttribute();. The last option will be faster and if you only have to run the reindex for the products after all products are saved, you can just run them manually.

Here is an example of a script to set the default value. It hasn't been tested and of course the name and value for the attribute and the attribute set aren't set properly yet.

<?php

require 'abstract.php';

class Namespace_Shell_UpdateAttributes 
    extends Mage_Shell_Abstract
{
    public function run()
    {
        /** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('attribute_set_id', 200)
            ->addAttributeToFilter('attribute_name', array('null' => true));

        /** @var Mage_Catalog_Model_Product $product */
        foreach ($collection as $product) {
            $product->setAttributeName('Default Value');

            $product->getResource()->saveAttribute($product, 'attribute_name');
        }
    }
}

$script = new Namespace_Shell_UpdateAttributes();
$script->run();
1
  • I have not tried yet, but how do I specify which attributes I must add to the "General" section? I do not have to add all attributes that are not assigned, but only one part
    – Riccardo
    May 1 '17 at 8:02
0

I would do it from .php file.

Create a file my_file_name.php in your root folder:

<?php
require_once 'app/Mage.php';
Mage::app();
$core_setup_model = Mage::getModel('eav/entity_setup','core_setup');

$setOfAttributes = array('first_attribute', 'second_attribute');

foreach($setOfAttributes as $attrib){

    $attributeId = $core_setup_model->getAttributeId('catalog_product',$attrib); //change attribute_name to your own attribute name;

    $allAttributeSetIds = $core_setup_model->getAllAttributeSetIds('catalog_product');
    foreach ($allAttributeSetIds as $attributeSetId) {
        try{
            $attributeGroupId = $core_setup_model->getAttributeGroupId('catalog/product',$attributeSetId,'General');
        }
        catch(Exception $e){
            $attributeGroupId = $core_setup_model->getDefaultAttributeGroupId('catalog/product',$attributeSetId);
        }

        $core_setup_model->addAttributeToSet('catalog_product', $attributeSetId, $attributeGroupId, $attributeId);

   }
}

Now, in your browser just call yourdomain.com/my_file_name.php.

I haven't tested but this should work. Good luck.

3
  • Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (offquatt_mage231.mgss_eav_entity_attribute, CONSTRAINT FK_MGSS_EAV_ENTT_ATTR_ATTR_ID_MGSS_EAV_ATTR_ATTR_ID FOREIGN KEY (attribute_id) REFERENCES mgss_eav_attribute (attribute_id) O)'.....................................
    – Riccardo
    May 1 '17 at 8:18
  • Hi Adarsh Khatri I tried your code but it goes wrong. Is it possible to solve it? Thank you
    – Riccardo
    May 2 '17 at 11:28
  • That code should work, I had used it for similar purpose. It might be that some of the attribute you trying to add are already added. You may want to look in to Mage_Eav_Model_Entity_Setup.php's addAttributeToSet() function and find out what's wrong. Happy debugging. May 3 '17 at 23:12

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.