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

3
  • 1
    Where does $product_id get set? That error doesn't look related to your shell script but rather another extension.
    – Paul
    Jul 27, 2017 at 12:34
  • Regadless of not set $product_id the script works for me.
    – sv3n
    Jul 27, 2017 at 12:38
  • @Paul i edited script
    – omelandr
    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();
2
  • thanks, but $product = Mage::getModel('catalog/product') returned null, empty object
    – omelandr
    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
    – sandip
    Jul 28, 2017 at 7:11

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.