0

Here is my code. I have a simple batch script I'm testing. I can't get save() to work at all. I can get saveAttribute... to work.

All this simple test would be update status. We will use it for many other things, but I'm just getting the construct to work. It works off IDs --from 1 --to 500 for example. I've tried other scripts and same issue. Can't seem to isolate the issue.

<?php

$options = getopt('',array("from:","to:"));
if( !empty($options) &&
    isset($options['from']) &&
    isset($options['to']) )
{
    if(is_numeric($options['from']) && is_numeric($options['to'])){
        echo runScript($options['from'],$options['to']);
    }else {
        echo "Make sure parameters are numeric";
    }

}else{
    echo "Missing Arguments";
}

function runScript($from,$to){
    $_mage_file = 'app/Mage.php';
    if(file_exists($_mage_file)){
        require_once($_mage_file);
        umask(0);
        Mage::app();
        echo "Processing...";

        $_collection = Mage::getModel('catalog/product')->getCollection()
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('entity_id', array(
                            'from' => $from,
                            'to' => $to
                        ))
                        ->load();


        foreach($_collection as $_product){


                try{

                    // BOF AC Script




//                    $product = Mage::getModel('catalog/product')->load($product_id);

            $_product->setData('status','2');
//                    $_product->getResource()->saveAttribute($_product, 'status'); //Works

                    // Save the values 
                  $_product->save();  //Doesn't work

                    // EOF AC Script


                }catch (Exception $e){
                    echo "Exception :". $e->getMessage();
                    exit;
                }


        }



    }   else{
        return "Please make sure script is at Magento root. app/Mage.php file not found";
    }


}

This script work just fine, but we have other observer that fire when a product is saved on the admin that only fire for certain roles.

Here is that script. What I need is a way to work with both.

$admin_user_session = Mage::getSingleton('admin/session');

$adminuserId = $admin_user_session->getUser()->getUserId();

$role_data = Mage::getModel('admin/user')->load($adminuserId)->getRole()->getData();

$role_name = $role_data['role_name'];
5
  • try to init Magento app : Mage::app('admin'); (after require_once)
    – bchatard
    Commented Sep 4, 2016 at 20:12
  • I've already tried that... Commented Sep 4, 2016 at 20:14
  • Do you get any error messages? Are log files enabled in the Magento backend?
    – CCK1979
    Commented Sep 5, 2016 at 6:10
  • Thanks for that. I've updated the answer to what was the underlying cause, however the issue is still there. Commented Sep 6, 2016 at 2:05
  • I just have to unregister session. And it will ignore any observer that is getting fired for that session before save();!!! Commented Sep 18, 2016 at 4:42

1 Answer 1

0

plaease change following in your foreach loop and in collection

        $_collection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('entity_id', array(
                        'from' => $from,
                        'to' => $to
                    ));

 foreach($_collection as $_product){


            try{

                $product = Mage::getModel('catalog/product')->load($product->getId());

                $product->setData('status','2');
                $product->save();  
            }catch (Exception $e){
                echo "Exception :". $e->getMessage();
                exit;
            }
} 

you are loading diff object and you are saving differnt object that is the issue I have updated foreach loop please check and let me know

5
  • Just fails, doesn't loop. get to the save();. I had to change $product_id to $_product->getId as it's in a colleciton. Commented Sep 5, 2016 at 19:57
  • please check my updated answer Commented Sep 6, 2016 at 1:38
  • I appreciate all the help, but it wasn't firing due to an observer checking for $admin_user_session = Mage::getSingleton('admin/session'); $adminuserId = $admin_user_session->getUser()->getUserId(); $role_data = Mage::getModel('admin/user')->load($adminuserId)->getRole()->getData(); $role_name = $role_data['role_name']; So of course one the save() fires it can't check for roles as it's an CLI Commented Sep 6, 2016 at 1:52
  • can you update your question so that I can understand now where is the mistake Commented Sep 6, 2016 at 1:59
  • Updated the question to what I found was the cause, but still have the issue. Commented Sep 6, 2016 at 2:04

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