PHP Classes

File: set_attributes.php

Recommend this page to a friend!
  Classes of Victor Andeloci   Useful Magento Scripts   set_attributes.php   Download  
File: set_attributes.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Useful Magento Scripts
Run product operations in Magento e-commerce sites
Author: By
Last change:
Date: 10 days ago
Size: 980 bytes
 

Contents

Class file image Download
<?php

   
include_once './app/Mage.php';
   
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

   
$update_file = "./var/export/set_attributes.csv";

   
$catalog = Mage::getModel('catalog/product');

   
// attribute map
   
$attributes = array();

   
$csv = array_map('str_getcsv', file($update_file));
    foreach (
$csv as $attribute)
     
$attributes[$attribute[0]] = $attribute[1];

    try {
     
$ids = Mage::getModel('catalog/product')->getCollection()->getAllIds();
      foreach (
$ids as $id) {
       
$get_item = $catalog->load($id);
        if(
$get_item) {
          echo
'Setting attributes for : ' . $get_item['sku'] . '<br>';
          foreach (
$attributes as $key => $value)
           
$get_item->setData(trim($key), trim($value));

         
$get_item->save();
        }
      }

      echo
'<h1>Done!</h1>';
    } catch (
Exception $e) {
      echo
"Cannot retrieve products from Magento: ".$e->getMessage()."<br>";
      return;
    }
?>