Prem Tiwari - - Magento Tutorial
Hello Magento folks, Today I am going to share the 15 extremely helpful Magento scripts which will help more in your Magento web application development.
I have collected these all scripts during my Magento website development and some from the other websites. I have put them in one post so that many Magento developers can use it.
Want to debug your Magento code using Zend framework, then use the below code snippet.
1 2 3 | < ?php echo Zend_Debug::dump($thing_to_debug, 'debug'); ?> |
By using the below code get all products by category ID.
1 2 3 4 5 6 7 8 9 10 11 12 | $_category = Mage::getModel('catalog/category')->load(47); $_productCollection = $_category->getProductCollection(); if($_productCollection->count()) { foreach( $_productCollection as $_product ): echo $_product->getProductUrl(); echo $this->getPriceHtml($_product, true); echo $this->htmlEscape($_product->getName()); endforeach; } |
1 2 3 4 5 6 7 8 9 | < ?php $countryList = Mage::getResourceModel('directory/country_collection') ->loadData() ->toOptionArray(false); print_r( $countryList); ?> |
Get all associated product lists in magento. Use below code in /app/design/frontend/default/site/template/catalog/product/view/type/
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < ?php $_helper = $this->helper('catalog/output'); ?> < ?php $_associatedProducts = $this->getAllowProducts() ?> < ?php //var_dump($_associatedProducts); ?> <br /> <br /> < ?php if (count($_associatedProducts)): ?> < ?php foreach ($_associatedProducts as $_item): ?> <a href="<?php echo $_item->getProductUrl() ?>">< ?php echo $_helper->productAttribute($_item, $_item->getName(), 'name') ?> | < ?php echo $_item->getName() ?> | < ?php echo $_item->getPrice() ?></a> <br /> <br /> < ?php endforeach; ?> < ?php endif; ?> |
Get the parent category of a product in Magento.
1 2 3 4 5 6 7 8 | < ?php $rootCategoryId = Mage::app()->getStore()->getRootCategoryId(); $_category = Mage::getModel('catalog/category')->load($rootCategoryId); // You can then get all of the top level categories using: $_subcategories = $_category->getChildrenCategories(); ?> |
1 2 3 4 5 6 7 | < ?php $currentCategory = Mage::registry('current_category'); $currentProduct = Mage::registry('current_product'); $currentCmsPage = Mage::registry('cms_page'); ?> |
1 2 3 4 5 6 7 8 9 10 | < ?php // find 'path' in table 'core_config_data' e.g. 'design/head/demonotice' $my_change_config = new Mage_Core_Model_Config(); // turns notice on $my_change_config->saveConfig('design/head/demonotice', "1", 'default', 0); // turns notice off $my_change_config->saveConfig('design/head/demonotice', "0", 'default', 0); ?> |
1 2 3 4 5 6 | < ?php $logged_in = Mage::getSingleton('customer/session')->isLoggedIn(); // (boolean) ?> |
1 2 3 4 5 6 | < ?php $product1 = Mage::getModel('catalog/product')->load(12); $product2 = Mage::getModel('catalog/product')->loadByAttribute('sku','cordoba-classic-6-String-guitar'); ?> |
1 2 3 4 5 6 7 8 9 10 | < ?php // input is $_product and result is iterating child products $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); $col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); foreach($col as $simple_product){ var_dump($simple_product->getId()); } ?> |
1 2 3 4 5 | < ?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block-name')->toHtml(); ?> |
1 2 3 4 5 6 7 8 | < ?php require_once('app/Mage.php'); //Path to Magento umask(0); Mage::app(); // Run you code here ?> |
1 2 3 4 5 6 7 8 9 10 | < ?php // find 'path' in table 'core_config_data' e.g. 'design/head/demonotice' $my_change_config = new Mage_Core_Model_Config(); // turns notice on $my_change_config->saveConfig('design/head/demonotice', "1", 'default', 0); // turns notice off $my_change_config->saveConfig('design/head/demonotice', "0", 'default', 0); ?> |
1 2 3 4 5 6 7 8 9 10 11 | < ?php // $_GET $productId = Mage::app()->getRequest()->getParam('product_id'); // The second parameter to getParam allows you to set a default value which is returned if the GET value isn't set $productId = Mage::app()->getRequest()->getParam('product_id', 44); $postData = Mage::app()->getRequest()->getPost(); // You can access individual variables like... $productId = $postData['product_id']); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < ?php $_countries = Mage::getResourceModel('directory/country_collection') ->loadData() ->toOptionArray(false) ?> < ?php if (count($_countries) > 0): ?> <select name="country" id="country"> <option value="">-- Please Select --</option> < ?php foreach($_countries as $_country): ?> <option value="<?php echo $_country['value'] ?>"> < ?php echo $_country['label'] ?> </option> < ?php endforeach; ?> </select> < ?php endif; ?> |
I hope this tutorial help you, If you have any question related to Magento CMS, then feel free to put your comments in below comment section. I will be happy to help you as much as I can. Do you like & share this tutorial with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool Magento tutorials. Subscribe this blog to get my best tutorial directly in your email inbox.
Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies. View all posts by Prem Tiwari
Tags: magento 2 tutorial, magento advanced tutorials, magento backend tutorial, magento code snippets, magento custom module development tutorial, magento module development tutorial, magento tutorial, magento tutorial for developer, magento tutorials