Magento Short Hand Code

Magento – Run Magento Code Externally

 require_once('app/Mage.php'); //Path to Magento
 umask(0);
 Mage::app();
 // Run you code here

Magento – Get Product Stock Quantity

To get the quantity in stock for a particular product in Magento,

$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

Magento Redirect

 $url = "https://techbandhu.wordpress.com/about/";
 $this->_redirectUrl($url);
 or,
 Mage::app()->getFrontController()->getResponse()->setRedirect($url);

Magento Referer Redirect

 $this->_redirectReferer($refererUrl);

$refererUrl is only used if no referrer found, otherwise it will redirect to original referrer.

Magento – Clear Cache Programmatically

 try {
    $cacheTypes = Mage::app()->useCache();
    foreach ($cacheTypes as $type => $option) {
       Mage::app()->getCacheInstance()->cleanType($type);
    }
 } catch (Exception $e) {
    Mage::log($e->getMessage());
 }

OR

 Mage::app()->cleanCache(); // can also use

Magento – Reindex Programmatically

 for($i=1;$i<=9;$i++){
    Mage::getModel('index/process')->load($i)->reindexEverything();
 }

 // where I is
 1 = Product Attributes
 2 = Product Attributes
 3 = Catalog URL Rewrites
 4 = Product Flat Data
 5 = Category Flat Data
 6 = Category Products
 7 = Catalog Search Index
 8 = Tag Aggregation Data
 9 = Stock Status

Magento – Load category by id

 $_category = Mage::getModel('catalog/category')->load(5);
 $_category_url = $_category->getUrl();

Magento – Load product by id or sku

 $_product = Mage::getModel('catalog/product')->load(108);
 $_product = Mage::getModel('catalog/product')->loadByAttribute('sku','zoom-tees');

Magento – Get Configurable product’s Child products

 $_product = Mage::getModel('catalog/product')->load(108);
 $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $_product);

Magento – Get Configurable product’s Children’s (simple product) custom attributes

 $_product = Mage::getModel('catalog/product')->load(108);
 $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
 $col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
 foreach($col as $simple_product){
     var_dump($simple_product->getId());
 }

Magento – Call Static Block

 echo $this->getLayout()->createBlock('cms/block')->setBlockId('block-name')->toHtml();

Magento – Log to custom file

 Mage::log('Your Log Message', Zend_Log::INFO, 'your_log_file.log');

Magento – Get the current category/product/cms page

 $currentCategory = Mage::registry('current_category');
 $currentProduct = Mage::registry('current_product');
 $currentCmsPage = Mage::registry('cms_page');

Magento – Programmatically change Magento’s core config data

 $my_change_config = new Mage_Core_Model_Config();
 // visible on
 $my_change_config->saveConfig('techbandhu/productqa/visible', "1", 'default', 0);
 // visible off
 $my_change_config->saveConfig('techbandhu/productqa/visible', "0", 'default', 0);

Magento – Get The Root Category

 $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();

Magento – Get all of the top level categories

 $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
 $_category = Mage::getModel('catalog/category')->load($rootCategoryId);
 $_subcategories = $_category->getChildrenCategories();

Magento Get The Current URL

 Mage::helper('core/url')->getCurrentUrl();

Magento Get all Products by Category ID

 $_category = Mage::getModel('catalog/category')->load(47);
 $_productCollection = $_category->getProductCollection();
 if($_productCollection->count()) {
    foreach( $_productCollection as $_product ):
       echo $_product->getName();
       echo $_product->getProductUrl();
    endforeach;
 }

Magento get all orders collection between two dates

/* Format our dates */
$fromDate = date('Y-m-d H:i:s', strtotime($fromDate));
$toDate = date('Y-m-d H:i:s', strtotime($toDate));
 
/* Get the collection */
$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate))
    ->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE));

4 thoughts on “Magento Short Hand Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s