Skip to content

Instantly share code, notes, and snippets.

Forked from davidalexander/gist:1086455
Last active July 23, 2021 15:20
  • Star 20 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Magento Tips, Tricks and Snippets

Magento Tips, Tricks and Snippets


Helpful Urls

Download extension manually using pear/mage

Pear for 1.4, mage for 1.5. File downloaded into /downloader/.cache/community/

./pear download magento-community/Shipping_Agent
./mage download community Shipping_Agent


Clear cache/reindex

// clear cache

// reindex prices
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

// Recreate a single index programmatically
Mage::getModel('index/process')->load(Mage_Catalog_Helper_Product_Flat::CATALOG_FLAT_PROCESS_CODE, 'indexer_code')->reindexEverything();

Categories and Products

Load category by id

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

Load product by id or sku

$_product_1 = Mage::getModel('catalog/product')->load(12);
$_product_2 = Mage::getModel('catalog/product')->loadByAttribute('sku','cordoba-classic-6-String-guitar');

Get Configurable product's Child products

// input is $_product and result is iterating child products
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);

Get Configurable product's Children's (simple product) custom attributes

// 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){

Is product purchasable?

<?php if($_product->isSaleable()) { // do stuff } ?>

Load Products by Category ID

$_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());

Get The Root Category

$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();

Return Product Attributes

// The following returns the option IDs for an attribute that is a multiple-select field: 
$_product->getData('color'); // i.e. 456,499
// The following returns the attribute object, and instance of Mage_Catalog_Model_Resource_Eav_Attribute: 
$_product->getResource()->getAttribute('color'); // instance of Mage_Catalog_Model_Resource_Eav_Attribute
// The following returns an array of the text values for the attribute: 
$_product->getAttributeText('color') // Array([0]=>'red', [1]=>'green')
// The following returns the text for the attribute
if ($attr = $_product->getResource()->getAttribute('color')):
    echo $attr->getFrontend()->getValue($_product); // will display: red, green

Get Simple Products of a Configurable Product

if($_product->getTypeId() == "configurable") {
    $ids = $_product->getTypeInstance()->getUsedProductIds();
    foreach ($ids as $id) {
        $simpleproduct = Mage::getModel('catalog/product')->load($id);
        	echo $simpleproduct->getName() . " - " . (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty();

Getting Configurable Product from Simple Product ID in Magento 1.5+

$simpleProductId = 465;
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
$product = Mage::getModel('catalog/product')->load($parentIds[0]);
echo $product->getId(); // ID = 462 (aka, Parent of 465)

# Generate URL to variant of configurable by $sku
$simple = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$productId = array_shift(Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($simple->getId()));
$url = Mage::getUrl('catalog/product/view', array( 'id' => $productId, 'option_id' => $simple->getVariant() ));


Get the current category/product/cms page

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

Get associated products

In /app/design/frontend/default/site/template/catalog/product/view/type/

<?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; ?>


Call Static Block

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

Add JavaScript to page

First approach: page.xml - you can add something like

<action method="addJs"><script>path/to/my/file.js</script></action>

Second approach: Find page/html/head.phtml in your theme and add the code directly to page.html.

Third approach: If you look at the stock page.html mentioned above, you'll see this line

<?php echo $this->getChildHtml() ?>

Normally, the getChildHtml method is used to render a specific child block. However, if called with no paramater, getChildHtml will automatically render all the child blocks. That means you can add something like

<!-- existing line --> <block type="page/html_head" name="head" as="head">
<!-- new sub-block you're adding --> <block type="core/template" name="mytemplate" as="mytemplate" template="page/mytemplate.phtml"/>

to page.xml, and then add the mytemplate.phtml file. Any block added to the head block will be automatically rendered. (this automatic rendering doesn't apply for all layout blocks, only for blocks where getChildHtml is called without paramaters).

Customer handling

Check if customer is logged in

<?php $logged_in = Mage::getSingleton('customer/session')->isLoggedIn(); // (boolean) ?>

Logging and Exception handling

Log to custom file

<?php Mage::log('Your Log Message', Zend_Log::INFO, 'your_log_file.log'); ?>

Send success / errors to session

<?php Mage::getSingleton('core/session')->addSuccess($this->__('Hello world.')); ?>
<?php Mage::getSingleton('core/session')->addError($this->__('Bad world!!')); ?>


Programmatically change or read Magento’s core config data

// 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);


Changing the Admin URL

Open up the /app/etc/local.xml file, locate the <frontName> tag, and change the ‘admin’ part it to something a lot more random, eg:


Clear your cache and sessions.

Magento: Mass Exclude/Unexclude Images

By default, Magento will check the 'Exclude' box for you on all imported images, making them not show up as a thumbnail under the main product image on the product view.

# Mass Unexclude
UPDATE`catalog_product_entity_media_gallery_value` SET `disabled` = '0' WHERE `disabled` = '1';
# Mass Exclude
UPDATE`catalog_product_entity_media_gallery_value` SET `disabled` = '1' WHERE `disabled` = '0';

Category Navigation Listings in Magento

Make sure the block that you’re working is of the type catalog/navigation. If you’re editing catalog/navigation/left.phtml then you should be okay.

<div id="leftnav">
	<?php $helper = $this->helper('catalog/category') ?>
	<?php $categories = $this->getStoreCategories() ?>
	<?php if (count($categories) > 0): ?>
		<ul id="leftnav-tree" class="level0">
			<?php foreach($categories as $category): ?>
				<li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
					<a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
					<?php if ($this->isCategoryActive($category)): ?>
						<?php $subcategories = $category->getChildren() ?>
						<?php if (count($subcategories) > 0): ?>
							<ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
								<?php foreach($subcategories as $subcategory): ?>
									<li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
										<a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
								<?php endforeach; ?>
							<script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
						<?php endif; ?>
					<?php endif; ?>
			<?php endforeach; ?>
		<script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
	<?php endif; ?>

Environment and Debugging

Helpful Debug watches

file_put_contents(sprintf('/tmp/layout_%s.xml', $this->getAction()->getFullActionName()), $this->getLayout()->getXmlString())

Debug using zend

<?php echo Zend_Debug::dump($thing_to_debug, 'debug'); ?>

Run Magento Code Externally

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

Turn template hints on/off via database

`value` = 0
`path` = "dev/debug/template_hints"
`path` = "dev/debug/template_hints_blocks";

Delete all products

TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_product_entity`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;

Get methods of an object

First, use get_class to get the name of an object's class.

<?php $class_name = get_class($object); ?>

Then, pass that get_class_methods to get a list of all the callable methods on an object

$class_name = get_class($object);
$methods = get_class_methods($class_name);
foreach($methods as $method)

Check if module is installed/active

Mage::getConfig()->getModuleConfig('Sitewards_B2BProfessional')->is('active', 'true');

Read and write configuration values

// Read data
$mailTemplateId = Mage::getStoreConfig('YOUR/PATH/HERE', $storeId);

// Write data
Mage::getModel('core/config')->saveConfig('YOUR/PATH/HERE', $value );

Update all subscribers into a customer group (e.g. 5)

	customer_entity.`group_id` = 5
	customer_entity.`entity_id` = newsletter_subscriber.`customer_id`
	newsletter_subscriber.`subscriber_status` = 1;

Get An Array of Country Names/Codes in Magento

$countryList = Mage::getResourceModel('directory/country_collection')
    echo '<pre>';
    print_r( $countryList);

Create a Country Drop Down in the Frontend of Magento

$_countries = Mage::getResourceModel('directory/country_collection')
                                    ->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'] ?>
        <?php endforeach; ?>
<?php endif; ?>

Create a Country Drop Down in the Magento Admin

    $fieldset->addField('country', 'select', array(
        'name'  => 'country',
        'label'     => 'Country',
        'values'    => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),

Cart Data

$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item) {
    echo $item->getName();

URLs, Requests and Responses

$_GET, $_POST & $_REQUEST Variables

// $_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']);

Redirecting to other location

$target = '';


$request = Mage::app()->getRequest();
$response = Mage::app()->getResponse();

# All pameters by controller
$controller = Mage::registry('controller');
$params = $controller->getRequest()->getParams();

# Get a specific post parameter
$param = $controller->getRequest()->getPost('myPostParameterName');

# Check Ajax-Requests
if ($request->isXmlHttpRequest()) { echo 'yes'; }


Get The Current URL In Magento

<?php echo Mage::helper('core/url')->getCurrentUrl(); ?>

getBaseUrl – Magento URL Path

echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

Customer login url with redirect to current page

Mage::getUrl('customer/account/login', array ( Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME => Mage::helper('core')->urlEncode(Mage::getUrl('*/*/*', array('_current' => true))) )); ?>');


File names

  • mysql4-install-0.1.0.php
  • mysql4-upgrade-0.1.0-0.1.1.php

Config XML Layout


Attribute creation

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('customer', 'attribute_name', array(
    'label'           => 'Händlersuche erlaubt',
    'type'            => 'int',
    'input'           => 'select',
    'global'          => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source'          => 'eav/entity_attribute_source_boolean',
    'default'         => 0,
    'visible'         => true,
    'required'        => false,
    'user_defined'    => true,
    'is_user_defined' => true,
    'position'        => 100,

    ->getAttribute('customer', 'attribute_name')
    ->setData('used_in_forms', array('adminhtml_customer'))

Other Helpful stuff

$templateName = Mage::getDesign()->getTemplateFilename('path/to/template', $params);


{{block type="core/template" template="cms/startseite.phtml"}}
require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'CartController.php';

Mage::helper('core')->currency($field, true, false);

$localeCode = Mage::getStoreConfig('general/locale/code', $storeId);

Copy link

tobi-pb commented Nov 2, 2013


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment