7

I have created the custom attribute (test) for products as a text field with default value('test') from admin panel

And assigned that attribute to default attribute set.

Now I can able to see the new custom attribute in product edit page.

When I try to filter with the product collection

Mage::getModel('catalog/product')->getCollection()
       ->addAttributeToSelect('*')
       ->addAttributeToFilter('test', array('like' => 'test'))->getData();

It return the empty array.

My Question is :

Is there any way to set the default value to attributes through installer script?

Why the default value is not set to that attribute?

Is there is any way to add the default values for all products?. I have more than 10,000 products in my store.

Note :

I don't want load the whole product collection and set value for each product.

2 Answers 2

11

You could try something like this. I believe the below method is quite efficient.

$attrData = array(
    'attribute_code_here'=> 'Default Value Here',
);
$storeId = 0;
$productIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
Mage::getModel("catalog/product_action")->updateAttributes(
    $productIds, 
    $attrData, 
    $storeId
);
5
  • @marius Im a huge Fan. Thanks for the upvote :)
    – NathanielR
    Jun 18, 2015 at 13:14
  • Is there is a way set this value in installer script or config.xml? Jun 18, 2015 at 13:18
  • @Marius I am building a new extension. So I will create this attribute through installer script. Is this possible to set this default value in installer scritp? Jun 18, 2015 at 13:35
  • @saravanavelu You could look at data scripts or create your attribute through the installer and then run the snippet as a standalone script
    – NathanielR
    Jun 18, 2015 at 13:42
  • While installing the module, need to update the attributes is this possible without standalone script? Jun 18, 2015 at 13:45
0

In case somebody's wondering how to achieve this in Magento 2, here is the code (based on @Marius answer above)

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\Action;

class InstallData implements InstallDataInterface
{
    /** @var EavSetupFactory  */
    private $eavSetupFactory;

    /** @var CollectionFactory  */
    private $collectionFactory;

    /** @var Action  */
    private $action;

    public function __construct(
        EavSetupFactory $eavSetupFactory,
        CollectionFactory $collectionFactory,
        Action $action
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->collectionFactory = $collectionFactory;
        $this->action = $action;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'my_custom_attribute',
            [
                'type' => 'int',
                'frontend' => '',
                'frontend_class' => '',
                'label' => 'My Custom Attribute',
                'input' => 'boolean',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'source' => 'Vendor\Module\Model\Config\Options',
                'visible' => true,
                'user_defined' => false,
                'required' => false,
                'default' => '0',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'used_in_product_listing' => false,
                'unique' => false,
            ]
        );

        //set default value for the new custom attribute
        $productIds = $this->collectionFactory->create()->getAllIds();
        $this->action->updateAttributes($productIds, ['my_custom_attribute' => 0], 0);
    }
}

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.