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
omelandr
1,2671 gold badge17 silver badges39 bronze badges
1 Answer 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
sandip
4,0643 gold badges29 silver badges57 bronze badges
-
thanks, but $product = Mage::getModel('catalog/product') returned null, empty objectomelandr– omelandr2017年07月28日 06:30:12 +00:00Commented 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,45sandip– sandip2017年07月28日 07:11:21 +00:00Commented Jul 28, 2017 at 7:11
default
$product_idget set? That error doesn't look related to your shell script but rather another extension.$product_idthe script works for me.