4

For 45.000+ products I want to update a specific date attribute.

I use this custom attribute "new_stock", that all needs the value of "01-01-2000".

How can I easily update this attribute for all these products?

I tried this code, inside a custom .php file and run it by browser.
But that did not work

<?php
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $productIds));

foreach($products as $product)
{
$product->setNewStock('2000-01-01');
// below code use for time format 
$product->setNewStockIsFormated(true);
$product->getResource()->saveAttribute($product, 'new_stock');


}?>
7
  • Don't use load() at the start of your foreach loop. You already have the $product that you need, and $id is unset anyway, so just remove that whole line. Also try changing setSpecialFromDateIsFormated to setNewStockIsFormated (though you might not need this line at all, actually). Commented Nov 17, 2015 at 12:22
  • @DougMcLean Thanks! But how can I set the correct product ID's for 45,000 products?
    – JGeer
    Commented Nov 17, 2015 at 12:23
  • Edited my comment to reflect the edits in your question Commented Nov 17, 2015 at 12:25
  • @DougMcLean I edit my code. Should this be correct? And does this run for all products, because I load the code by productIds?
    – JGeer
    Commented Nov 17, 2015 at 12:27
  • Also, if you want to apply this to all products, you don't need a $productIds array or the addAttributeToFilter option. For all products, just use $products = Mage::getModel('catalog/product')->getCollection() Commented Nov 17, 2015 at 12:27

2 Answers 2

4

Credit to @VishwasBhatnagar for the code - I'm just clarifying what I believe the finished code should be, to do all products (the to-and-fro in the comments was getting a bit confusing I think).

<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

require('app/Mage.php'); // this is assuming your script is located in the Magento root dir
Mage::app(); // initiate the Magento engine

$products = Mage::getModel('catalog/product')->getCollection();

foreach($products as $product)
{
    $product->setNewStock('2000-01-01');
    // this line might not be required - compare results with & without it 
    $product->setNewStockIsFormated(true);
    $product->getResource()->saveAttribute($product, 'new_stock');
}
?>
8
  • Thanks!! I tried that, but that does not work. That code does also load within 1 sec, so I guess something is wrong.
    – JGeer
    Commented Nov 17, 2015 at 12:36
  • Ah yes there are 2 lines that are required at the start of scripts like this, where they exist as a standalone script but still need to use Magento functionality... see my edit above Commented Nov 17, 2015 at 12:44
  • Thanks Doug! I also ran exactly that code, but it does the same, complete within 1 sec.
    – JGeer
    Commented Nov 17, 2015 at 12:48
  • I think on that occasion it was a bug in my code - I missed out a semicolon, sorry :). I'll edit it to fix that, and I'll also include a couple of lines to make it tell you when errors occur Commented Nov 17, 2015 at 12:52
  • 1
    @Jelle Share the whole code of your Custom.php in question so that we can point out your error Commented Nov 17, 2015 at 12:58
3

In product Grid Select all the desired Products , in action drop down on the right select update attribute

OR

you can provide an attribute by below code

$product->setAttributeCode($newValue)
$ProductObject->getResource()->saveAttribute($product, 'attribute_Code');

Example:

$product=Mage::getModel('catalog/product')->load($id);
$product->setSpecialFromDate('2010-10-28');
// below code use for time format 
$product->setSpecialFromDateIsFormated(true);
$product->getResource()->saveAttribute($product, 'special_from_date');

Using Product Collection:

$productIds = array(1,3,2);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $productIds));

foreach($products as $product)
{
    $product->setSpecialFromDate('2010-10-28');
    // below code use for time format 
    $product->setSpecialFromDateIsFormated(true);
    $product->getResource()->saveAttribute($product, 'special_from_date');


}
6
  • Thanks! But in that case it is the SpecialFromDate attribute. I created a new custom date attribute for this, named "new_stock". How do I need to use your code for that?
    – JGeer
    Commented Nov 17, 2015 at 11:17
  • @Jelle Go to catalog>attribute>Manage Attribute > look for your custom attribute and there you can find code Commented Nov 17, 2015 at 12:07
  • 1
    @Jelle do you know about Magento's magic getter and setter functions? And attribute can be accessed by these functions: change the code from underscore-separated to TitleCase and put the word "get" or "set" before it. So in your case, $product->setNewStock('2000-01-01'); in place of $product->setSpecialFromDate('2010-10-28');. I think it'll also work with $product->setNewStockIsFormated(true);, try it out and let us know if not :) Oh and where the above code says special_from_date remember to change that to new_stock too. Commented Nov 17, 2015 at 12:11
  • @DougMcLean Thanks! I edit my question what I tried, but that did not work.
    – JGeer
    Commented Nov 17, 2015 at 12:16
  • 1
    @Jelle You should be using product collection code , so that you get all the product date and than update the attribute value using the foreach, and i guess you are running a custom script did you included mage.php in that file Commented Nov 17, 2015 at 12:20

Your Answer

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

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