Skip to content

Instantly share code, notes, and snippets.

@kevnk
Last active December 22, 2015 09:49
Show Gist options
  • Save kevnk/6454864 to your computer and use it in GitHub Desktop.
Save kevnk/6454864 to your computer and use it in GitHub Desktop.
Snippets for a Magento front-end developer doing some repeated customizations not native to default theme.

LAYOUTS

Add breadcrumbs

<reference name="breadcrumbs">
    <action method="addCrumb">
        <crumbName>Home</crumbName>
        <crumbInfo>
            <label>Home</label>
            <title>Home</title>
            <link>/</link>
            <first>true</first>
            <last/>
            <readonly/>
        </crumbInfo>
    </action>
</reference>    

Add, remove, and reorder Magento's top.links

http://www.classyllama.com/development/magento-development/editing-magentos-top-links-the-better-way

Adding common javascript assets to page

<default>
    <reference name="head">
        <!-- files in root /js directory -->
        <action method="addJs"><script>jquery/jquery-1.7.2.min-noConflict.js</script></action>
        <action method="addJs"><script>jquery/fancybox/jquery.fancybox-1.3.4.pack.js</script></action>
        <action method="addItem"><type>js_css</type><stylesheet>jquery/fancybox/jquery.fancybox-1.3.4.css</stylesheet><params>media="all"</params></action>
        <!-- Add google webfont -->
        <action method="addLinkRel"><rel>stylesheet</rel><href>http://fonts.googleapis.com/css?family=Gudea:400,700,400italic</href></action>
        <!-- files from skin directory -->
        <action method="addItem"><type>skin_js</type><name>js/main.js</name></action>
    </reference>
</default>

TEMPLATES

Social Link Params

<?php
    $_request = Mage::app()->getRequest();
    $_protocol = ($_request->isSecure()) ? 'https': 'http';
    if($_product = Mage::registry('current_product')) {
        $_pinUrl = '?url=' . $_product->getProductUrl();
        $_fbUrl = '?u=' . $_product->getProductUrl();
        $_pinImg = '&media=' . $this->helper('catalog/image')->init($_product, 'image');
        $_fbImg = '&p[images][0]=' . $this->helper('catalog/image')->init($_product, 'image');
        $_fbName = $_product->getName() ? '&p[title]=' . $_product->getName() : '';
        $_pinName = $_product->getName() ? '&description=' . $_product->getName() : '';
        $_desc = $_product->getShortDescription() ? $_product->getShortDescription() : $_product->getDescription();
        $_fbDesc = $_desc ? '&p[summary]=' . $_desc : '';
    } else {
        $_pinUrl = '?url=' . $this->helper('core/url')->getCurrentUrl();
        $_fbUrl = '?u=' . $this->helper('core/url')->getCurrentUrl();
        $_pinImg = '';
        $_pinName = '';
        $_fbImg = '';
        $_fbName = '';
        $_fbDesc = '';
    }
    $_pinParams = urlencode($_pinUrl . $_pinImg . $_pinName);
    $_fbParams = urlencode($_fbUrl . $_fbImg . $_fbName . $_fbDesc);
?>
<a class="social-btn fblike" href="//www.facebook.com/sharer.php/<?php echo $_fbParams  ?>" rel="external" title="<?php echo $this->__('Like us on facebook'); ?>"><?php echo $this->__('Like us on facebook'); ?></a>
<a class="social-btn pinit" href="//pinterest.com/pin/create/button/<?php echo $_pinParams  ?>" data-pin-do="buttonPin" data-pin-config="none" rel="external" title="<?php echo $this->__('Pin this page on Pinterest'); ?>"><?php echo $this->__('Pin this page on Pinterest'); ?></a>

Get Request / Layout Handle

<?php
    $_request = Mage::app()->getRequest();
    $_module = $_request->getModuleName();
    $_controller = $_request->getControllerName();
    $_action = $_request->getActionName();
    $_handle = $_module . '_' . $_controller . '_' . $_action;
?>

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