Skip to content

Instantly share code, notes, and snippets.

@frosit
Created February 25, 2016 23:27
Show Gist options
  • Select an option

  • Save frosit/449115594d9c9683b9f4 to your computer and use it in GitHub Desktop.

Select an option

Save frosit/449115594d9c9683b9f4 to your computer and use it in GitHub Desktop.
Magento CheatSheet

#Magento Cheat Sheet Code snippets for use in Magento.

##Base Methods

	// Get Base URL
    Mage::getBaseUrl();
    // Get Base Directory
    Mage::getBaseDir();
    // Get Skin URL
    Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
    // Unsecure Skin URL
    $this->getSkinUrl('images/imagename.jpg');
    // Secure Skin URL
    $this->getSkinUrl('images/imagename.gif', array('_secure'=>true));
    // Get Media URL
    Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
    // Get JS URL
    Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
    // Get Store URL
    Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    // Get Current URL
    Mage::helper('core/url')->getCurrentUrl();
    // Get Url in cms pages or static blocks
    // Get Base URL
    {=store url=""=}
    // Get Skin URL
    {=skin url='images/imagename.jpg'=}
    // Get Media URL
    {=media url='/imagename.jpg'=}
    // Get Store URL
    {=store url='mypage.html'=}

Cache

// Load cache object
$cache = Mage::app()->getCache();
// Save item to cache
$cache->save($string, $key, array($tag), $lifetime);
// Load item from cache
$cache->load($key);
// Remove item from cache
$cache->remove($key);
// Mas removal from cache
$cache->clean(array($tag));

Session

// Set item to session
Mage::getSingleton('core/session')->setMyValue($myValue);
// Load item from session
$myValue = Mage::getSingleton('core/session')->getMyValue();
// Remove item from session
Mage::getSingleton('core/session')->unsMyValue();

Mail

// Load the email template
$templateId = 'Fav Email';
$emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateId);

// Replace any template variables with a value
$vars = array('user_name' => $userName, 'product_name' => $productName);
// Replace the variables in the template
$emailTemplate->getProcessedTemplate($vars);

// Setup sender name and email address
$emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email', $storeId));
$emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name', $storeId));

// Send the mail
$emailTemplate->send($receiveEmail,$receiveName, $vars);
      

##XML

<!-- Change page template -->
    <reference name="root">
      <action method="setTemplate">
        <template>page/template_file.phtml</template>
      </action>
    </reference>
    
    <!-- Add/remove CSS/JS -->
    <reference name="head">
      <action method="addItem">
      <type>skin_css</type>
        <name>css/file.css</name>
        <params/>
      </action>
      <action method="addItem">
      <type>skin_js</type>
        <name>js/file.js</name>
        <params/>
      </action>
    </reference>
    
    <!-- Add/remove CSS/JS -->
    <reference name="old_parent">
      <action method="unsetChild">
        <name>block_name</name>
      </action>
    </reference>
    <reference name="new_parent">
      <action method="insert">
        <name>block_name</name>
      </action>
    </reference>
    
    <!-- Add/remove CSS/JS -->
    <remove name="block_name">
    
    <!-- Add/remove CSS/JS -->
    <reference name="breadcrumbs">
      <action method="addCrumb">
        <crumbName>Link Text</crumbName>
        <crumbInfo>
          <label>Link Text</label>
          <title>Link Text</title>
          <link>Link-url</link>
        </crumbName>
      </action>
    </reference>
    
    <!-- Insert CMS block -->
    <block type="cms/block" name="relevant_name">
      <action method="setBlockId">
        <block_id>static_block_identifier</block_id>
      </action>
    </block>
    
    <!-- Insert template file -->
    <block type="cms/template" name="relevant_name" template="page/html/template_file.phtml"/>
    
    <!-- Add address fields to customer signup form -->
    <customer_account_create>
      <reference name="customer_form_register">
        <action method="setShowAddressFields">
          <param>true</param>
        </action>
      </reference>
    </customer_account_create>

Snippets

Small snipptes for regular used things.

Clear Cache

// Clear Cache
Mage::app()->removeCache('catalog_rules_dirty');
      

Reindex

/*
1 = Product Attributes
2 = Product Prices
3 = Catalog URL Rewrites
4 = Product Flat Data
5 = Category Flat Data
6 = Category Products
7 = Catalog Search Index
8 = Stock Status
9 = Tag Aggregation Data
*/
// Reindex Prices
Mage::getModel('index/process')->load(2)->reindexEverything();
      

Load products by category ID

$_category = Mage::getModel('catalog/category')->load(47);
$_productCollection = $_category->getProductCollection();

Load category by ID

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

Load product by ID or SKU

$_product1 = Mage::getModel('catalog/product')->load(12);
$_product2 = Mage::getModel('catalog/product')->loadByAttribute('sku','sku123');
      

Get Child Products by Configurable Product

// input is instance of product model
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
      

Log to Custom log file

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

Check if customer is logged in.

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

Get a clean string with no symbols or spaces

$_cleaned = Mage::getModel('catalog/product_url')->formatUrlKey($_dirty);

Get current category

$currentCategory = Mage::registry('current_category');
      

Get current product

$currentProduct = Mage::registry('current_product');

Get current CMS page

$currentCmsPage = Mage::registry('cms_page');

Convert to currency format

$amount = Mage::helper('core')->currency($value, true, false);
      

Run Magento code outside the App (root dir)

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

Get the root Category

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

Get the current URL

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

Is product available for sale

if ($_product->isSaleable()) {
// do something
}

Load config

Mage::getStoreConfig('[MODULE]/[SECTION]/[FIELD]', $storeId)
      

Empty markdown code placeholder

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