1

in shell directory is file myscript.php with code

<?php
require_once 'abstract.php';
class MyModule_Shell_Myscript extends Mage_Shell_Abstract
{
 protected $_argname = array();
 public function __construct()
 {
 parent::__construct();
 set_time_limit(0); 
 if ($this->getArg('argname')) {
 $this->_argname = array_merge(
 $this->_argname,
 array_map(
 'trim',
 explode(',', $this->getArg('argname'))
 )
 );
 }
 }
 public function run()
 { 
 $product = Mage::getModel('catalog/product')->load('2'); 
 var_dump($product->getData());
 }
}
$shell = new MyModule_Shell_Myscript();
$shell->run();

Product with this id is in database When i try to run command php shell/myscript.php I got error enter image description here

diazwatson
2,4782 gold badges28 silver badges39 bronze badges
asked Jul 27, 2017 at 12:14
3
  • 1
    Where does $product_id get set? That error doesn't look related to your shell script but rather another extension. Commented Jul 27, 2017 at 12:34
  • Regadless of not set $product_id the script works for me. Commented Jul 27, 2017 at 12:38
  • @Paul i edited script Commented Jul 27, 2017 at 12:47

1 Answer 1

1

I hope you already solved shell script issue.

Added comma separated value as well in loop along with the usageHelp.

<?php
require_once 'abstract.php';
class MyModule_Shell_Myscript extends Mage_Shell_Abstract
{
 protected $_argname = array();
 public function __construct()
 {
 parent::__construct();
 set_time_limit(0); 
 if ($this->getArg('argname')) {
 $this->_argname = array_merge( $this->_argname, array_map('trim', explode(',', $this->getArg('argname') ) ) );
 }
 }
 public function run()
 { 
 if ($this->getArg('argname')) {
 try {
 foreach( $this->_argname as $row) {
 $product = Mage::getModel('catalog/product')->load( $row );
 var_dump( $product->getData() );
 }
 } catch (Exception $e) {
 echo $e->getMessage();
 }
 } else {
 echo $this->usageHelp();
 }
 }
 /**
 * Get Usage Help Message
 *
 */
 public function usageHelp()
 {
 return <<<USAGE
Usage: php product.php -- [options]
 --argname 12,14,15 Comma separated product ids
USAGE;
 }
}
$shell = new MyModule_Shell_Myscript();
$shell->run();
answered Jul 27, 2017 at 16:14
2
  • thanks, but $product = Mage::getModel('catalog/product') returned null, empty object Commented Jul 28, 2017 at 6:30
  • Forget to mention that what should pass params while executing it should be like as : php product.php --argname 12,34,45 Commented Jul 28, 2017 at 7:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.