Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adrmarques/d74dadc22e5681aa7dfc to your computer and use it in GitHub Desktop.
Save adrmarques/d74dadc22e5681aa7dfc to your computer and use it in GitHub Desktop.

Revisions

  1. @arosenhagen arosenhagen revised this gist Jan 23, 2013. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1168,4 +1168,14 @@ $order->load($lastOrderId);
    $_totalData =$order->getData();
    $_grand = $_totalData['grand_total'];
    ?>
    ```
    ```
    ## Set custom order numbers (starting number) ##
    ```mysql
    update eav_entity_store
    inner join eav_entity_type on eav_entity_type.entity_type_id = eav_entity_store.entity_type_id
    set eav_entity_store.increment_prefix='YOURPREFIX'
    set eav_entity_store.increment_last_id='YOURSTARTINGORDERNUMBER'
    where eav_entity_type.entity_type_code='order';
    ```
  2. @arosenhagen arosenhagen revised this gist Oct 2, 2012. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1155,4 +1155,17 @@ where product_tabs_email is the name of the li that you want to open.
    var_dump($_attribute->debug()); // returns the set of values you can use the get magic method on
    }
    ?>
    ```
    ## Get order information on success.phtml ##
    ```php
    <?php
    $_customerId = Mage::getSingleton('customer/session')->getCustomerId();
    $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
    $order = Mage::getSingleton('sales/order');
    $order->load($lastOrderId);
    $_totalData =$order->getData();
    $_grand = $_totalData['grand_total'];
    ?>
    ```
  3. @arosenhagen arosenhagen revised this gist Jun 6, 2012. 1 changed file with 27 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1129,4 +1129,30 @@ to fire the link remotely you can call
    ```js
    csTablist.remoteTabs('product_tabs_email');
    ```
    where product_tabs_email is the name of the li that you want to open.
    where product_tabs_email is the name of the li that you want to open.
    ## Getting Configurable Attributes (Super Attributes) of a Configurable Product ##
    ```php
    <?php
    /**
    * Load Product you want to get Super attributes of
    */
    $product=Mage::getModel("catalog/product")->load(52520);

    /**
    * Get Configurable Type Product Instace and get Configurable attributes collection
    */
    $configurableAttributeCollection=$product->getTypeInstance()->getConfigurableAttributes();

    /**
    * Use the collection to get the desired values of attribute
    */
    foreach($configurableAttributeCollection as $attribute){
    echo "Attr-Code:".$attribute->getProductAttribute()->getAttributeCode()."<br/>";
    echo "Attr-Label:".$attribute->getProductAttribute()->getFrontend()->getLabel()."<br/>";
    echo "Attr-Id:".$attribute->getProductAttribute()->getId()."<br/>";
    var_dump($_attribute->debug()); // returns the set of values you can use the get magic method on
    }
    ?>
    ```
  4. @arosenhagen arosenhagen revised this gist Jun 3, 2012. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1111,4 +1111,22 @@ foreach ($collection as $_product){
    ```php
    <?= $this->getToolbarBlock()->setTemplate('catalog/product/list/toolbar_bottom.phtml')->toHtml(); ?>
    ```
    ```
    ## remotely trigger Varien.Tabs Class or EasyTabs ##
    add this to Varien.Tabs.prototype
    ```js
    Varien.Tabs.prototype = {
    remoteTabs: function(b) {
    var controlledLink = $$("#"+b+" a")[0];
    this.showContent(controlledLink);
    }
    }
    var csTablist = new Varien.Tabs('.tabs');
    ```
    to fire the link remotely you can call
    ```js
    csTablist.remoteTabs('product_tabs_email');
    ```
    where product_tabs_email is the name of the li that you want to open.
  5. @arosenhagen arosenhagen revised this gist May 22, 2012. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1105,4 +1105,10 @@ foreach ($collection as $_product){
    //break;//uncomment this if you want only one category tree to appear.
    };
    ?>
    ```
    ## Different toolbar in product list / grid ##
    ```php
    <?= $this->getToolbarBlock()->setTemplate('catalog/product/list/toolbar_bottom.phtml')->toHtml(); ?>
    ```
  6. @arosenhagen arosenhagen revised this gist Apr 26, 2012. 1 changed file with 32 additions and 8 deletions.
    40 changes: 32 additions & 8 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1070,15 +1070,39 @@ echo $product->getId(); // ID = 462 (aka, Parent of 465)
    ## Get a list of bestsellers ##
    ```php
    <?php
    $collection = Mage::getResourceModel('sales/report_bestsellers_collection')
    ->setModel('catalog/product')
    ->addStoreFilter(Mage::app()->getStore()->getId())//if you want the bestsellers for a specific store view. if you want global values remove this
    ->setPageSize(5)//set the number of products you want
    ->setCurPage(1);
    <?php
    $collection = Mage::getResourceModel('sales/report_bestsellers_collection')
    ->setModel('catalog/product')
    ->addStoreFilter(Mage::app()->getStore()->getId()) //if you want the bestsellers for a specific store view. if you want global values remove this
    ->setPageSize(5)//set the number of products you want
    ->setCurPage(1);
    foreach ($collection as $_product){
    $realProduct = Mage::getModel('catalog/product')->load($_product->getProductId());
    //do something with $realProduct;
    $realProduct = Mage::getModel('catalog/product')->load($_product->getProductId());
    //do something with $realProduct;
    }
    ?>
    ```
    ## Add category names in product view page ##
    ```php
    <?php
    $categoryIds = $_product->getCategoryIds();
    foreach ($categoryIds as $categoryId){
    $tmpId = $categoryId;
    $categories = array();
    while($tmpId != Mage::app()->getStore()->getRootCategoryId()) {
    $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($tmpId);
    $categories[] = $category;
    $tmpId = $category->getParentId();
    };
    for ($i = count($categories) - 1; $i>=0;$i--){
    echo '<a href="'.echo $categories[$i]->getUrl().'">'.echo $categories[$i]->getName().'</a>';
    if ($i>0){
    echo "-&gt;"<!-- this is the tree separator. change to whatever you like-->
    }
    }
    //break;//uncomment this if you want only one category tree to appear.
    };
    ?>
    ```
  7. @arosenhagen arosenhagen revised this gist Apr 26, 2012. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1066,3 +1066,19 @@ echo $product->getId(); // ID = 462 (aka, Parent of 465)
    echo Mage::getModel('sales/order')->formatPricePrecision($_product->getFinalPrice(), 3);
    ?>
    ```
    ## Get a list of bestsellers ##
    ```php
    <?php
    $collection = Mage::getResourceModel('sales/report_bestsellers_collection')
    ->setModel('catalog/product')
    ->addStoreFilter(Mage::app()->getStore()->getId())//if you want the bestsellers for a specific store view. if you want global values remove this
    ->setPageSize(5)//set the number of products you want
    ->setCurPage(1);
    foreach ($collection as $_product){
    $realProduct = Mage::getModel('catalog/product')->load($_product->getProductId());
    //do something with $realProduct;
    }
    ?>
    ```
  8. @arosenhagen arosenhagen revised this gist Apr 23, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -458,7 +458,7 @@ endif;
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $items = $quote->getAllItems();
    foreach ($items as $item) {
    $priceInclVat = $item->getRowTotalInclTax();
    $priceInclVat += $item->getRowTotalInclTax();
    }
    Mage::helper('checkout')->formatPrice($priceInclVat);
    ?>
  9. @arosenhagen arosenhagen revised this gist Apr 22, 2012. 1 changed file with 53 additions and 0 deletions.
    53 changes: 53 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,9 @@ rm -rf app/code/core/Zend/Cache
    rm -rf var/ait_rewrite/*
    rm -rf media/css/*
    rm -rf media/js/*

    // if there are many files which can't get deleted in once
    find /path/to/session/* -mtime +1 -exec rm {} \;
    ```
    ## Load category by id ##
    @@ -1013,3 +1016,53 @@ echo $product->getId(); // ID = 462 (aka, Parent of 465)
    $categoriesArray = $helper->getStoreCategories('name', false, false);
    ?>
    ```
    ## Get product in stock quantity ##
    ```php
    <?php
    $_product = Mage::getModel('catalog/product')->load($product_id);
    $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
    ?>
    ```
    ## Use magento "outside" magento ##
    ```php
    <?php
    require_once 'app/Mage.php';
    Mage::app($yourStoreCode);
    echo Mage::getStoreConfig('general/store_information/name');
    ?>
    ```
    ## Hijack session outside magento ##
    ```php
    <?php
    require_once 'app/Mage.php';
    Mage::app($yourStoreCode);
    Mage::getSingleton('core/session', array('name'=>'frontend'))->setSessionId($_COOKIE['frontend']);
    echo "cart_id=".Mage::helper('checkout/cart')->getCart()->getQuote()->getId();
    ?>
    ```
    ## Filter collection or get configurable products: ##
    ```php
    <?php
    $configurable_products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFiler('type_id',array('eq'=>'configurable'))
    ->load();
    ?>
    ```
    ## Round price ##
    ```php
    <?php
    echo Mage::getModel('sales/order')->formatPricePrecision($_product->getFinalPrice(), 3);
    ?>
    ```
  10. @arosenhagen arosenhagen revised this gist Apr 20, 2012. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    ./mage download community Module_Name
    ```

    ## Clear cache/reindex ##
    ## Reindex ##

    ```bash
    php -f shell/indexer.php reindexall
    @@ -34,14 +34,17 @@ Mage::getModel('index/process')->load(2)->reindexEverything();
    ?>
    ```

    ## Delete cache/sessions/ ##
    ## Delete cache/sessions ##
    ```bash
    rm -rf var/log/*
    rm -rf var/cache/*
    rm -rf var/session/*
    rm -rf var/report/*
    rm -rf var/locks/*
    rm -rf app/code/core/Zend/Cache
    rm -rf var/ait_rewrite/*
    rm -rf media/css/*
    rm -rf media/js/*
    ```

    ## Load category by id ##
  11. @arosenhagen arosenhagen revised this gist Apr 20, 2012. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -708,6 +708,28 @@ echo $product->getId(); // ID = 462 (aka, Parent of 465)
    <?= $this->helper('catalog/image')->init($_product, 'image'); ?>
    ```

    ## Downsize large product images but not enlarge small images ##

    ```php
    <?php
    $this->helper('catalog/image')
    ->init($_product, 'image')
    ->keepFrame(false) // avoids getting the small image in original size on a solid background color presented (can be handy not to break some layouts)
    ->constrainOnly(true) // avoids getting small images bigger
    ->resize(650); // sets the desired width and the height accordingly (proportional by default)
    ?>
    ```

    ## No square (white background) product images ##

    ```html
    <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->backgroundcolor('000', '000', '000')->resize(100); ?>" />
    ```

    ```html
    <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->keepFrame(false)->resize(100); ?>" width="100" ... />
    ```

    ## Show image using current skin path (PHTML) ##

    ```html
  12. @arosenhagen arosenhagen revised this gist Apr 20, 2012. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -662,6 +662,37 @@ echo $product->getId(); // ID = 462 (aka, Parent of 465)
    ?>
    ```

    ## Get all associated children product of a configurable product ##

    ```php
    <?php
    /**
    * Load product by product id
    */
    $product = Mage::getModel('catalog/product')->load(YOUR_PRODUCT_ID);

    /**
    * Get child products id (only ids)
    */
    $childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());

    /**
    * Get children products (all associated children products data)
    */
    $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
    ?>
    ```

    ## Get parent id of simple product associated to configurable product ##

    ```php
    <?php
    $_product = Mage::getModel('catalog/product')->load(YOUR_SIMPLE_PRODUCT_ID);
    $parentIdArray = $_product->loadParentProductIds()->getData('parent_product_ids');
    print_r($parentIdArray);
    ?>
    ```

    ## Check if customer is logged in ##

    ```php
  13. @arosenhagen arosenhagen revised this gist Apr 20, 2012. 1 changed file with 317 additions and 15 deletions.
    332 changes: 317 additions & 15 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -381,27 +381,39 @@ endif;
    ?>
    ```

    ## Cart Data ##
    ## Format Price ##

    ```php
    <?php
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    print_r($cart);
    $cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
    print_r($cart);
    $session = Mage::getSingleton('checkout/session');
    foreach ($session->getQuote()->getAllItems() as $item) {
    echo $item->getName();
    Zend_Debug::dump($item->debug());
    $formattedPrice = Mage::helper('core')->currency($_finalPrice,true,false);
    }
    ?>
    ```


    ## Cart Data ##

    ```php
    <?php
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    print_r($cart);

    $cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
    print_r($cart);

    $session = Mage::getSingleton('checkout/session');
    foreach ($session->getQuote()->getAllItems() as $item) {
    echo $item->getName();
    Zend_Debug::dump($item->debug());
    }
    ?>
    ```

    ## Total items added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsCount();
    ?>
    ```
    @@ -410,7 +422,7 @@ foreach ($session->getQuote()->getAllItems() as $item) {

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();
    ?>
    ```
    @@ -424,12 +436,12 @@ foreach ($session->getQuote()->getAllItems() as $item) {
    ?>
    ```

    ## Grand total for for item added in cart ##
    ## Grand total for item added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();
    Mage::helper('checkout')->formatPrice(Mage::getModel('checkout/cart')->getQuote()->getGrandTotal());
    Mage::helper('checkout')->formatPrice(Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal());
    ?>
    ```

    @@ -446,6 +458,33 @@ foreach ($session->getQuote()->getAllItems() as $item) {
    ?>
    ```

    ## Get products id, name, price, quantity, etc. present in your cart ##

    ```php
    <?php
    // $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
    $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

    foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";
    }
    ?>
    ```

    ## Get number of items in cart and total quantity in cart ##

    ```php
    <?php
    $totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    $totalQuantity = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    ?>
    ```

    ## Get Simple Products of a Configurable Product ##

    ```php
    @@ -654,4 +693,267 @@ echo $product->getId(); // ID = 462 (aka, Parent of 465)

    ```php
    <?= $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_identifier')->toHtml(); ?>
    ```
    ```

    ## Get Customer Shipping/Billing Address ##

    ```php
    <?php
    $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
    if ($customerAddressId){
    $address = Mage::getModel('customer/address')->load($customerAddressId);
    }
    ?>
    ```

    ## Get Product image path ##

    ```php
    <?php
    $productId = 1;
    $product = Mage::getModel('catalog/product')->load($productId);
    $path = Mage::helper('catalog/image')->init($product, 'image')->resize(75, 75);
    ?>
    ```

    ## Get product URL ##

    ```php
    <?php
    $productId = 1;
    $product = Mage::getModel('catalog/product')->load($productId);
    $path = Mage::getUrl().$product->getUrlPath();
    ?>
    ```

    ## Get Category URL ##

    ```php
    <?= Mage::getModel('catalog/category')->load($categoryId)->getUrl(); ?>
    ```

    ## Get product stock quantity ##

    ```php
    <?php
    $id = 52;
    $_product = Mage::getModel('catalog/product')->load($id);

    // or load it by SKU
    // $sku = "microsoftnatural";
    // $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

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

    print_r($stock->getData());

    echo $stock->getQty();
    echo $stock->getMinQty();
    echo $stock->getMinSaleQty();
    ?>
    ```

    ## Get actual price and special price of a product ##

    ```php
    <?php
    $_productId = 52;
    $_product = Mage::getModel('catalog/product')->load($_productId);

    // without currency sign
    $_actualPrice = number_format($_product->getPrice(), 2);
    // with currency sign
    $_formattedActualPrice = Mage::helper('core')->currency(number_format($_product->getPrice(), 2),true,false);

    // without currency sign
    $_specialPrice = $_product->getFinalPrice();
    // with currency sign
    $_formattedSpecialPrice = Mage::helper('core')->currency(number_format($_product->getFinalPrice(), 2),true,false);
    ?>
    ```

    ## Get Currency Symbol ##

    ```php
    <?= Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(); ?>
    ```

    ## Get Currency Code ##

    ```php
    <?= Mage::app()->getStore()->getCurrentCurrencyCode(); ?>
    ```

    ## Track Visitor’s Information ##

    ```php
    <?php
    $visitorData = Mage::getSingleton('core/session')->getVisitorData();
    print_r($visitorData);

    // Array
    // (
    // [] =>
    // [server_addr] => 167772437
    // [remote_addr] => 167772437
    // [http_secure] =>
    // [http_host] => 127.0.0.1
    // [http_user_agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10
    // [http_accept_language] => en-US,en;q=0.8
    // [http_accept_charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
    // [request_uri] => /magento/index.php/catalog/category/view/id/22
    // [session_id] => 13qm5u80238vb15lupqcac97r5
    // [http_referer] => http://127.0.0.1/magento/
    // [first_visit_at] => 2011-01-17 11:42:23
    // [is_new_visitor] =>
    // [last_visit_at] => 2011-01-17 11:58:38
    // [visitor_id] => 41
    // [last_url_id] => 139
    // )

    // user's ip address (visitor's ip address)
    $remoteAddr = Mage::helper('core/http')->getRemoteAddr(true);

    // server's ip address (where the current script is)
    $serverAddr = Mage::helper('core/http')->getServerAddr(true);
    ?>
    ```

    ## Get / filter all products by attribute value ##

    ```php
    <?php
    /**
    * Get all products related to any particular brand
    * Let us suppose that we are fetching the products related to 'Samsung' brand
    * Let us suppose the Manufacturer ID of Samsung = 3
    */

    $manufacturerId = 3;
    $attributeCode = 'manufacturer';

    $products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter($attributeCode, $manufacturerId);

    // print all products
    print_r($products->getItems());
    ?>
    ```

    ## Check if current page is homepage ##

    ```php
    <?php
    if($this->getIsHomePage()) {
    // Homepage
    } else {
    // not on Homepage
    }

    // alternative method
    $routeName = Mage::app()->getRequest()->getRouteName();
    $identifier = Mage::getSingleton('cms/page')->getIdentifier();
    if($routeName == 'cms' && $identifier == 'home') {
    // Homepage
    } else {
    // not on Homepage
    }
    ?>
    ```

    ## Convert Price from Current Currency to Base Currency and vice-versa ##

    ```php
    <?php
    $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
    $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
    $price = 100;

    // convert price from current currency to base currency
    $priceOne = Mage::helper('directory')->currencyConvert($price, $currentCurrencyCode, $baseCurrencyCode);

    // convert price from base currency to current currency
    $priceTwo = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);
    ?>
    ```

    ## Changing price from any one currency to another ##

    ```php
    <?php
    $from = 'USD';
    $to = 'NPR';
    $price = 10;

    $newPrice = Mage::helper('directory')->currencyConvert($price, $from, $to);
    ?>
    ```

    ## Get Currency Rates ##

    ```php
    <?php
    /**
    * Get the base currency
    */
    $baseCurrencyCode = Mage::app()->getBaseCurrencyCode();

    /**
    * Get all allowed currencies
    * returns array of allowed currency codes
    */
    $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();

    /**
    * Get the currency rates
    * returns array with key as currency code and value as currency rate
    */
    $currencyRates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));

    $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();

    /**
    * Get currency rates for Nepalese Currency
    */
    $currencyRates = Mage::getModel('directory/currency')->getCurrencyRates('NPR', array_values($allowedCurrencies));
    ?>
    ```



    ## Get all categories ##

    ```php
    <?php
    $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');
    ?>
    ```

    ## Get all active categories ##

    ```php
    <?php
    $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter();
    ?>
    ```

    ## Get active categories of any particular level ##

    ```php
    <?php
    $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter()->addLevelFilter(1)->addOrderField('name');
    ?>
    ```

    ## Get store specific categories ##

    ```php
    <?php
    $helper = Mage::helper('catalog/category');

    // sorted by name, fetched as collection
    $categoriesCollection = $helper->getStoreCategories('name', true, false);

    // sorted by name, fetched as array
    $categoriesArray = $helper->getStoreCategories('name', false, false);
    ?>
    ```
  14. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -584,6 +584,17 @@ AND
    newsletter_subscriber.`subscriber_status` = 1;
    ```

    ## Set german address format ##

    ```sql
    INSERT INTO `directory_country_format` (`country_format_id`, `country_id`, `type`, `format`) VALUES
    (1, 'DE', 'html', '{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>\r\n{{depend company}}{{var company}}<br />{{/depend}}\r\n{{if street1}}{{var street1}}{{/if}}\r\n{{depend street2}}{{var street2}}<br />{{/depend}}\r\n{{depend street3}}{{var street3}}<br />{{/depend}}\r\n{{depend street4}}{{var street4}}<br />{{/depend}}\r\n{{if postcode}}{{var postcode}}{{/if}} {{if city}}{{var city}}{{/if}}<br/>\r\n{{var country}}<br/>\r\n{{depend telephone}}Tel.: {{var telephone}}{{/depend}}\r\n{{depend fax}}<br/>Fax: {{var fax}}{{/depend}}'),
    (2, 'DE', 'pdf', '{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}|\r\n{{depend company}}{{var company}}|{{/depend}}\r\n{{if street1}}{{var street1}} {{/if}}\r\n{{depend street2}}{{var street2}}|{{/depend}}\r\n{{depend street3}}{{var street3}}|{{/depend}}\r\n{{depend street4}}{{var street4}}|{{/depend}}\r\n{{if postcode}}{{var postcode}} {{/if}}{{if city}}{{var city}}{{/if}}}|\r\n{{var country}}|\r\n{{depend telephone}}Tel.: {{var telephone}}{{/depend}}|\r\n{{depend fax}}<br/>Fax: {{var fax}}{{/depend}}|'),
    (3, 'DE', 'oneline', '{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}, {{var street}}, {{var postcode}} {{var city}}, {{var country}}'),
    (4, 'DE', 'text', '{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}\r\n{{depend company}}{{var company}}{{/depend}}\r\n{{if street1}}{{var street1}} {{/if}}\r\n{{depend street2}}{{var street2}}{{/depend}}\r\n{{depend street3}}{{var street3}}{{/depend}}\r\n{{depend street4}}{{var street4}}{{/depend}}\r\n{{if postcode}}{{var postcode}} {{/if}}{{if city}}{{var city}}{{/if}}\r\n{{var country}}\r\nTel.: {{var telephone}}\r\n{{depend fax}}Fax: {{var fax}}{{/depend}}'),
    (5, 'DE', 'js_template', '#{prefix} #{firstname} #{middlename} #{lastname} #{suffix}<br/>#{company}<br/>#{street0}<br/>#{street1}<br/>#{street2}<br/>#{street3}<br/>#{postcode} #{city}<br/>#{country_id}<br/>Tel.: #{telephone}<br/>Fax: #{fax}');
    ```

    ## Setting file permissions ##

    ```bash
  15. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 1 changed file with 32 additions and 1 deletion.
    33 changes: 32 additions & 1 deletion magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,12 @@
    # Magento Code Snippets #

    ## Download extension manually using mage ##
    ./mage download community Shipping_Agent
    ```bash
    ./mage config-set preferred_state stable
    ./mage clear-cache
    ./mage sync
    ./mage download community Module_Name
    ```

    ## Clear cache/reindex ##

    @@ -29,6 +34,16 @@ Mage::getModel('index/process')->load(2)->reindexEverything();
    ?>
    ```

    ## Delete cache/sessions/ ##
    ```bash
    rm -rf var/log/*
    rm -rf var/cache/*
    rm -rf var/session/*
    rm -rf var/report/*
    rm -rf var/locks/*
    rm -rf app/code/core/Zend/Cache
    ```

    ## Load category by id ##

    ```php
    @@ -569,6 +584,22 @@ AND
    newsletter_subscriber.`subscriber_status` = 1;
    ```

    ## Setting file permissions ##

    ```bash
    find </path/to/magento> -type f \-exec chmod 644 {} \;
    find </path/to/magento> -type d \-exec chmod 755 {} \;
    ```

    Other recommendations in "Securing Magento File & Directory Permissions" (http://blog.nexcess.net/2010/12/06/securing-magento-file-directory-permissions/)
    ```bash
    find </path/to/magento> \-exec chown youruser.youruser {} \;
    find </path/to/magento> -type f \-exec chmod 644 {} \;
    find </path/to/magento> -type d \-exec chmod 711 {} \;
    find </path/to/magento> -type f -name "*.php" \-exec chmod 600 {} \;
    chmod 600 </path/to/magento>/app/etc/*.xml
    ```

    ## Getting Configurable Product from Simple Product ID in Magento 1.5+ ##

    ```php
  16. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 2 changed files with 0 additions and 664 deletions.
    49 changes: 0 additions & 49 deletions 01
    Original file line number Diff line number Diff line change
    @@ -1,49 +0,0 @@
    // Check if customer is logged in
    $_customer = Mage::getSingleton('customer/session')->isLoggedIn();
    if ($_customer) {}

    // Get product image
    echo $this->helper('catalog/image')->init($_product, 'image');

    // Show image using current skin path (PHTML)
    <img src="<?php echo $this->getSkinUrl('images/logo.png');?>" alt="logo" />

    // Show image using current skin path (CMS)
    <img src={{skin url="images/logo.png"}} />

    // Show CMS block (PHTML)
    echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_identifier')->toHtml();

    // Total items added in cart
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsCount();

    // Total Quantity added in cart
    Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();

    // Sub Total for item added in cart
    Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
    Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();

    //grand total for for item added in cart
    Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();

    // sub total of cart inkl tax without shipping
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $items = $quote->getAllItems();
    foreach ($items as $item) {
    $priceInclVat = $item->getRowTotalInclTax();
    }
    Mage::helper('checkout')->formatPrice($priceInclVat);


    // Swap product image on thumbnail click
    // Open app/design/frontend/yourpackage/yourtheme/template/catalog/product/view/media.phtml
    <li>
    <a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" title="<?php echo $_product->getName();?>" onclick="$('image').src = this.href; return false;">
    <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(70, 70); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>
    </a>
    </li>
    Go down to line 71 and change:
    615 changes: 0 additions & 615 deletions 02
    Original file line number Diff line number Diff line change
    @@ -1,615 +0,0 @@
    # Magento Code Snippets #

    ## Download extension manually using pear/mage ##
    ./mage download community Shipping_Agent

    ## Clear cache/reindex ##

    ```php
    <?php
    // clear cache
    Mage::app()->removeCache('catalog_rules_dirty');
    // reindex prices
    Mage::getModel('index/process')->load(2)->reindexEverything();
    /*
    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
    */
    ?>
    ```

    ```bash
    php -f shell/indexer.php reindexall
    ```

    ## Load category by id ##

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

    ## Load product by id or sku ##

    ```php
    <?php
    $_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 ##

    ```php
    <?php
    // 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 ##

    ```php
    <?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());
    }
    ?>
    ```

    ## Log to custom file ##

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

    ## Call Static Block ##

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

    ## Add JavaScript to page ##

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

    ```xml
    <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
    <?= $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

    ```xml
    <!-- 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).

    ## Get the current category/product/cms page ##

    ```php
    <?php
    $currentCategory = Mage::registry('current_category');
    $currentProduct = Mage::registry('current_product');
    $currentCmsPage = Mage::registry('cms_page');
    ?>
    ```

    ## Run Magento Code Externally ##

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

    ## Programmatically change Magento’s core config data ##

    ```php
    <?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);
    ?>
    ```

    ## 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:

    ```xml
    <frontName><![CDATA[supersecret-admin-name]]></frontName>
    ```

    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.

    ```sql
    # 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';
    ```

    ## getBaseUrl – Magento URL Path ##

    ```php
    <?php
    // http://example.com/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    // http://example.com/js/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
    // http://example.com/index.php/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    // http://example.com/media/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
    // http://example.com/skin/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
    ?>
    ```

    ## Get The Root Category In Magento ##

    ```php
    <?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();
    ?>
    ```

    ## Get The Current URL In Magento ##

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

    ## 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.

    ```php
    <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-<?= $category->getId(); ?>" class="level1">
    <?php foreach($subcategories as $subcategory): ?>
    <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
    <a href="<?= $helper->getCategoryUrl($subcategory); ?>"><?= $this->escapeHtml(trim($subcategory->getName(), '- ')); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
    <?php endif; ?>
    <?php endif; ?>
    </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
    <?php endif; ?>
    </div>
    ```

    ## Debug using zend ##

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

    ## $_GET, $_POST & $_REQUEST Variables ##

    ```php
    <?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']);
    ?>
    ```

    ## Get methods of an object ##

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

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

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

    ```php
    <?php
    $class_name = get_class($object);
    $methods = get_class_methods($class_name);
    foreach($methods as $method)
    {
    var_dump($method);
    }
    ?>
    ```

    ## Is product purchasable? ##

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

    ## Load Products by Category ID ##

    ```php
    <?php
    $_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;
    }
    ?>
    ```

    ## Get associated products

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

    ``` php
    <?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="<?= $_item->getProductUrl(); ?>"><?= $_helper->productAttribute($_item, $_item->getName(), 'name'); ?> | <?= $_item->getName(); ?> | <?= $_item->getPrice(); ?></a>
    <br />
    <br />
    <?php endforeach; ?>
    <?php endif; ?>
    ```

    ## Get An Array of Country Names/Codes in Magento ##

    ```php
    <?php
    $countryList = Mage::getResourceModel('directory/country_collection')
    ->loadData()
    ->toOptionArray(false);

    echo '<pre>';
    print_r( $countryList);
    exit('</pre>');
    ?>
    ```

    ## Create a Country Drop Down in the Frontend of Magento ##

    ```php
    <?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="<?= $_country['value']; ?>">
    <?= $_country['label']; ?>
    </option>
    <?php endforeach; ?>
    </select>
    <?php endif; ?>
    ```

    ## Return Product Attributes ##

    ```php
    <?php
    $_product->getThisattribute();
    $_product->getAttributeText('thisattribute');
    $_product->getResource()->getAttribute('thisattribute')->getFrontend()->getValue($_product);
    $_product->getData('thisattribute');
    // 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
    endif;
    ?>
    ```

    ## Cart Data ##

    ```php
    <?php
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    print_r($cart);
    $cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
    print_r($cart);
    $session = Mage::getSingleton('checkout/session');
    foreach ($session->getQuote()->getAllItems() as $item) {
    echo $item->getName();
    Zend_Debug::dump($item->debug());
    }
    ?>
    ```

    ## Total items added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsCount();
    ?>
    ```

    ## Total Quantity added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();
    ?>
    ```

    ## Sub Total for item added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
    Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();
    ?>
    ```

    ## Grand total for for item added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();
    ?>
    ```

    ## Sub total of cart inkl tax without shipping ##

    ```php
    <?php
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $items = $quote->getAllItems();
    foreach ($items as $item) {
    $priceInclVat = $item->getRowTotalInclTax();
    }
    Mage::helper('checkout')->formatPrice($priceInclVat);
    ?>
    ```

    ## Get Simple Products of a Configurable Product ##

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

    ## Reset Development Environment (delete orders, customers, reset ids and counters, truncate statistics) ##

    ```sql
    SET FOREIGN_KEY_CHECKS=0;

    -- Here's where we reset the orders
    TRUNCATE `sales_flat_order`;
    TRUNCATE `sales_flat_order_address`;
    TRUNCATE `sales_flat_order_grid`;
    TRUNCATE `sales_flat_order_item`;
    TRUNCATE `sales_flat_order_status_history`;
    TRUNCATE `sales_flat_quote`;
    TRUNCATE `sales_flat_quote_address`;
    TRUNCATE `sales_flat_quote_address_item`;
    TRUNCATE `sales_flat_quote_item`;
    TRUNCATE `sales_flat_quote_item_option`;
    TRUNCATE `sales_flat_order_payment`;
    TRUNCATE `sales_flat_quote_payment`;
    TRUNCATE `sales_flat_shipment`;
    TRUNCATE `sales_flat_shipment_item`;
    TRUNCATE `sales_flat_shipment_grid`;
    TRUNCATE `sales_flat_invoice`;
    TRUNCATE `sales_flat_invoice_grid`;
    TRUNCATE `sales_flat_invoice_item`;
    TRUNCATE `sendfriend_log`;
    TRUNCATE `tag`;
    TRUNCATE `tag_relation`;
    TRUNCATE `tag_summary`;
    TRUNCATE `wishlist`;
    TRUNCATE `log_quote`;
    TRUNCATE `report_event`;

    ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
    ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
    ALTER TABLE `tag` AUTO_INCREMENT=1;
    ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
    ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
    ALTER TABLE `wishlist` AUTO_INCREMENT=1;
    ALTER TABLE `log_quote` AUTO_INCREMENT=1;
    ALTER TABLE `report_event` AUTO_INCREMENT=1;

    -- Here's where we reset the customers
    TRUNCATE `customer_address_entity`;
    TRUNCATE `customer_address_entity_datetime`;
    TRUNCATE `customer_address_entity_decimal`;
    TRUNCATE `customer_address_entity_int`;
    TRUNCATE `customer_address_entity_text`;
    TRUNCATE `customer_address_entity_varchar`;
    TRUNCATE `customer_entity`;
    TRUNCATE `customer_entity_datetime`;
    TRUNCATE `customer_entity_decimal`;
    TRUNCATE `customer_entity_int`;
    TRUNCATE `customer_entity_text`;
    TRUNCATE `customer_entity_varchar`;
    TRUNCATE `log_customer`;
    TRUNCATE `log_visitor`;
    TRUNCATE `log_visitor_info`;

    ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
    ALTER TABLE `log_customer` AUTO_INCREMENT=1;
    ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
    ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;

    -- This is to Reset all the ID counters
    TRUNCATE `eav_entity_store`;
    ALTER TABLE `eav_entity_store` AUTO_INCREMENT=1;

    SET FOREIGN_KEY_CHECKS=1;

    TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
    TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
    TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;

    DELETE FROM report_event WHERE event_type_id IN (SELECT event_type_id FROM report_event_types WHERE event_name IN ('catalog_product_view'));
    ```

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

    ```sql
    UPDATE
    customer_entity,
    newsletter_subscriber
    SET
    customer_entity.`group_id` = 5
    WHERE
    customer_entity.`entity_id` = newsletter_subscriber.`customer_id`
    AND
    newsletter_subscriber.`subscriber_status` = 1;
    ```

    ## Getting Configurable Product from Simple Product ID in Magento 1.5+ ##

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

    ## Check if customer is logged in ##

    ```php
    <?php
    $_customer = Mage::getSingleton('customer/session')->isLoggedIn();
    if ($_customer) {}
    ?>
    ```

    ## Get product image ##

    ```php
    <?= $this->helper('catalog/image')->init($_product, 'image'); ?>
    ```

    ## Show image using current skin path (PHTML) ##

    ```html
    <img src="<?= $this->getSkinUrl('images/logo.png'); ?>" alt="logo" />
    ```

    ## Show image using current skin path (CMS) ##

    ```html
    <img src={{skin url="images/logo.png"}} />
    ```

    ## Show CMS block (PHTML) ##

    ```php
    <?= $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_identifier')->toHtml(); ?>
    ```
  17. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  18. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,14 @@
    # Magento Code Snippets #

    ## Download extension manually using pear/mage ##
    ## Download extension manually using mage ##
    ./mage download community Shipping_Agent

    ## Clear cache/reindex ##

    ```bash
    php -f shell/indexer.php reindexall
    ```

    ```php
    <?php
    // clear cache
    @@ -25,10 +29,6 @@ Mage::getModel('index/process')->load(2)->reindexEverything();
    ?>
    ```

    ```bash
    php -f shell/indexer.php reindexall
    ```

    ## Load category by id ##

    ```php
  19. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 1 changed file with 615 additions and 0 deletions.
    615 changes: 615 additions & 0 deletions magento-code-snippets.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,615 @@
    # Magento Code Snippets #

    ## Download extension manually using pear/mage ##
    ./mage download community Shipping_Agent

    ## Clear cache/reindex ##

    ```php
    <?php
    // clear cache
    Mage::app()->removeCache('catalog_rules_dirty');
    // reindex prices
    Mage::getModel('index/process')->load(2)->reindexEverything();
    /*
    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
    */
    ?>
    ```

    ```bash
    php -f shell/indexer.php reindexall
    ```

    ## Load category by id ##

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

    ## Load product by id or sku ##

    ```php
    <?php
    $_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 ##

    ```php
    <?php
    // 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 ##

    ```php
    <?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());
    }
    ?>
    ```

    ## Log to custom file ##

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

    ## Call Static Block ##

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

    ## Add JavaScript to page ##

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

    ```xml
    <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
    <?= $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

    ```xml
    <!-- 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).

    ## Get the current category/product/cms page ##

    ```php
    <?php
    $currentCategory = Mage::registry('current_category');
    $currentProduct = Mage::registry('current_product');
    $currentCmsPage = Mage::registry('cms_page');
    ?>
    ```

    ## Run Magento Code Externally ##

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

    ## Programmatically change Magento’s core config data ##

    ```php
    <?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);
    ?>
    ```

    ## 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:

    ```xml
    <frontName><![CDATA[supersecret-admin-name]]></frontName>
    ```

    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.

    ```sql
    # 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';
    ```

    ## getBaseUrl – Magento URL Path ##

    ```php
    <?php
    // http://example.com/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    // http://example.com/js/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
    // http://example.com/index.php/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    // http://example.com/media/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
    // http://example.com/skin/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
    ?>
    ```

    ## Get The Root Category In Magento ##

    ```php
    <?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();
    ?>
    ```

    ## Get The Current URL In Magento ##

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

    ## 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.

    ```php
    <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-<?= $category->getId(); ?>" class="level1">
    <?php foreach($subcategories as $subcategory): ?>
    <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
    <a href="<?= $helper->getCategoryUrl($subcategory); ?>"><?= $this->escapeHtml(trim($subcategory->getName(), '- ')); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
    <?php endif; ?>
    <?php endif; ?>
    </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
    <?php endif; ?>
    </div>
    ```

    ## Debug using zend ##

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

    ## $_GET, $_POST & $_REQUEST Variables ##

    ```php
    <?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']);
    ?>
    ```

    ## Get methods of an object ##

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

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

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

    ```php
    <?php
    $class_name = get_class($object);
    $methods = get_class_methods($class_name);
    foreach($methods as $method)
    {
    var_dump($method);
    }
    ?>
    ```

    ## Is product purchasable? ##

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

    ## Load Products by Category ID ##

    ```php
    <?php
    $_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;
    }
    ?>
    ```

    ## Get associated products

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

    ``` php
    <?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="<?= $_item->getProductUrl(); ?>"><?= $_helper->productAttribute($_item, $_item->getName(), 'name'); ?> | <?= $_item->getName(); ?> | <?= $_item->getPrice(); ?></a>
    <br />
    <br />
    <?php endforeach; ?>
    <?php endif; ?>
    ```

    ## Get An Array of Country Names/Codes in Magento ##

    ```php
    <?php
    $countryList = Mage::getResourceModel('directory/country_collection')
    ->loadData()
    ->toOptionArray(false);

    echo '<pre>';
    print_r( $countryList);
    exit('</pre>');
    ?>
    ```

    ## Create a Country Drop Down in the Frontend of Magento ##

    ```php
    <?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="<?= $_country['value']; ?>">
    <?= $_country['label']; ?>
    </option>
    <?php endforeach; ?>
    </select>
    <?php endif; ?>
    ```

    ## Return Product Attributes ##

    ```php
    <?php
    $_product->getThisattribute();
    $_product->getAttributeText('thisattribute');
    $_product->getResource()->getAttribute('thisattribute')->getFrontend()->getValue($_product);
    $_product->getData('thisattribute');
    // 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
    endif;
    ?>
    ```

    ## Cart Data ##

    ```php
    <?php
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    print_r($cart);
    $cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
    print_r($cart);
    $session = Mage::getSingleton('checkout/session');
    foreach ($session->getQuote()->getAllItems() as $item) {
    echo $item->getName();
    Zend_Debug::dump($item->debug());
    }
    ?>
    ```

    ## Total items added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsCount();
    ?>
    ```

    ## Total Quantity added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();
    ?>
    ```

    ## Sub Total for item added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
    Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();
    ?>
    ```

    ## Grand total for for item added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();
    ?>
    ```

    ## Sub total of cart inkl tax without shipping ##

    ```php
    <?php
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $items = $quote->getAllItems();
    foreach ($items as $item) {
    $priceInclVat = $item->getRowTotalInclTax();
    }
    Mage::helper('checkout')->formatPrice($priceInclVat);
    ?>
    ```

    ## Get Simple Products of a Configurable Product ##

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

    ## Reset Development Environment (delete orders, customers, reset ids and counters, truncate statistics) ##

    ```sql
    SET FOREIGN_KEY_CHECKS=0;

    -- Here's where we reset the orders
    TRUNCATE `sales_flat_order`;
    TRUNCATE `sales_flat_order_address`;
    TRUNCATE `sales_flat_order_grid`;
    TRUNCATE `sales_flat_order_item`;
    TRUNCATE `sales_flat_order_status_history`;
    TRUNCATE `sales_flat_quote`;
    TRUNCATE `sales_flat_quote_address`;
    TRUNCATE `sales_flat_quote_address_item`;
    TRUNCATE `sales_flat_quote_item`;
    TRUNCATE `sales_flat_quote_item_option`;
    TRUNCATE `sales_flat_order_payment`;
    TRUNCATE `sales_flat_quote_payment`;
    TRUNCATE `sales_flat_shipment`;
    TRUNCATE `sales_flat_shipment_item`;
    TRUNCATE `sales_flat_shipment_grid`;
    TRUNCATE `sales_flat_invoice`;
    TRUNCATE `sales_flat_invoice_grid`;
    TRUNCATE `sales_flat_invoice_item`;
    TRUNCATE `sendfriend_log`;
    TRUNCATE `tag`;
    TRUNCATE `tag_relation`;
    TRUNCATE `tag_summary`;
    TRUNCATE `wishlist`;
    TRUNCATE `log_quote`;
    TRUNCATE `report_event`;

    ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
    ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
    ALTER TABLE `tag` AUTO_INCREMENT=1;
    ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
    ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
    ALTER TABLE `wishlist` AUTO_INCREMENT=1;
    ALTER TABLE `log_quote` AUTO_INCREMENT=1;
    ALTER TABLE `report_event` AUTO_INCREMENT=1;

    -- Here's where we reset the customers
    TRUNCATE `customer_address_entity`;
    TRUNCATE `customer_address_entity_datetime`;
    TRUNCATE `customer_address_entity_decimal`;
    TRUNCATE `customer_address_entity_int`;
    TRUNCATE `customer_address_entity_text`;
    TRUNCATE `customer_address_entity_varchar`;
    TRUNCATE `customer_entity`;
    TRUNCATE `customer_entity_datetime`;
    TRUNCATE `customer_entity_decimal`;
    TRUNCATE `customer_entity_int`;
    TRUNCATE `customer_entity_text`;
    TRUNCATE `customer_entity_varchar`;
    TRUNCATE `log_customer`;
    TRUNCATE `log_visitor`;
    TRUNCATE `log_visitor_info`;

    ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
    ALTER TABLE `log_customer` AUTO_INCREMENT=1;
    ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
    ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;

    -- This is to Reset all the ID counters
    TRUNCATE `eav_entity_store`;
    ALTER TABLE `eav_entity_store` AUTO_INCREMENT=1;

    SET FOREIGN_KEY_CHECKS=1;

    TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
    TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
    TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;

    DELETE FROM report_event WHERE event_type_id IN (SELECT event_type_id FROM report_event_types WHERE event_name IN ('catalog_product_view'));
    ```

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

    ```sql
    UPDATE
    customer_entity,
    newsletter_subscriber
    SET
    customer_entity.`group_id` = 5
    WHERE
    customer_entity.`entity_id` = newsletter_subscriber.`customer_id`
    AND
    newsletter_subscriber.`subscriber_status` = 1;
    ```

    ## Getting Configurable Product from Simple Product ID in Magento 1.5+ ##

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

    ## Check if customer is logged in ##

    ```php
    <?php
    $_customer = Mage::getSingleton('customer/session')->isLoggedIn();
    if ($_customer) {}
    ?>
    ```

    ## Get product image ##

    ```php
    <?= $this->helper('catalog/image')->init($_product, 'image'); ?>
    ```

    ## Show image using current skin path (PHTML) ##

    ```html
    <img src="<?= $this->getSkinUrl('images/logo.png'); ?>" alt="logo" />
    ```

    ## Show image using current skin path (CMS) ##

    ```html
    <img src={{skin url="images/logo.png"}} />
    ```

    ## Show CMS block (PHTML) ##

    ```php
    <?= $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_identifier')->toHtml(); ?>
    ```
  20. @arosenhagen arosenhagen revised this gist Apr 18, 2012. No changes.
  21. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 1 changed file with 615 additions and 0 deletions.
    615 changes: 615 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,615 @@
    # Magento Code Snippets #

    ## Download extension manually using pear/mage ##
    ./mage download community Shipping_Agent

    ## Clear cache/reindex ##

    ```php
    <?php
    // clear cache
    Mage::app()->removeCache('catalog_rules_dirty');
    // reindex prices
    Mage::getModel('index/process')->load(2)->reindexEverything();
    /*
    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
    */
    ?>
    ```

    ```bash
    php -f shell/indexer.php reindexall
    ```

    ## Load category by id ##

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

    ## Load product by id or sku ##

    ```php
    <?php
    $_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 ##

    ```php
    <?php
    // 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 ##

    ```php
    <?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());
    }
    ?>
    ```

    ## Log to custom file ##

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

    ## Call Static Block ##

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

    ## Add JavaScript to page ##

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

    ```xml
    <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
    <?= $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

    ```xml
    <!-- 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).

    ## Get the current category/product/cms page ##

    ```php
    <?php
    $currentCategory = Mage::registry('current_category');
    $currentProduct = Mage::registry('current_product');
    $currentCmsPage = Mage::registry('cms_page');
    ?>
    ```

    ## Run Magento Code Externally ##

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

    ## Programmatically change Magento’s core config data ##

    ```php
    <?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);
    ?>
    ```

    ## 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:

    ```xml
    <frontName><![CDATA[supersecret-admin-name]]></frontName>
    ```

    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.

    ```sql
    # 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';
    ```

    ## getBaseUrl – Magento URL Path ##

    ```php
    <?php
    // http://example.com/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    // http://example.com/js/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
    // http://example.com/index.php/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    // http://example.com/media/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
    // http://example.com/skin/
    echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
    ?>
    ```

    ## Get The Root Category In Magento ##

    ```php
    <?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();
    ?>
    ```

    ## Get The Current URL In Magento ##

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

    ## 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.

    ```php
    <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-<?= $category->getId(); ?>" class="level1">
    <?php foreach($subcategories as $subcategory): ?>
    <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
    <a href="<?= $helper->getCategoryUrl($subcategory); ?>"><?= $this->escapeHtml(trim($subcategory->getName(), '- ')); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
    <?php endif; ?>
    <?php endif; ?>
    </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
    <?php endif; ?>
    </div>
    ```

    ## Debug using zend ##

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

    ## $_GET, $_POST & $_REQUEST Variables ##

    ```php
    <?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']);
    ?>
    ```

    ## Get methods of an object ##

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

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

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

    ```php
    <?php
    $class_name = get_class($object);
    $methods = get_class_methods($class_name);
    foreach($methods as $method)
    {
    var_dump($method);
    }
    ?>
    ```

    ## Is product purchasable? ##

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

    ## Load Products by Category ID ##

    ```php
    <?php
    $_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;
    }
    ?>
    ```

    ## Get associated products

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

    ``` php
    <?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="<?= $_item->getProductUrl(); ?>"><?= $_helper->productAttribute($_item, $_item->getName(), 'name'); ?> | <?= $_item->getName(); ?> | <?= $_item->getPrice(); ?></a>
    <br />
    <br />
    <?php endforeach; ?>
    <?php endif; ?>
    ```

    ## Get An Array of Country Names/Codes in Magento ##

    ```php
    <?php
    $countryList = Mage::getResourceModel('directory/country_collection')
    ->loadData()
    ->toOptionArray(false);

    echo '<pre>';
    print_r( $countryList);
    exit('</pre>');
    ?>
    ```

    ## Create a Country Drop Down in the Frontend of Magento ##

    ```php
    <?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="<?= $_country['value']; ?>">
    <?= $_country['label']; ?>
    </option>
    <?php endforeach; ?>
    </select>
    <?php endif; ?>
    ```

    ## Return Product Attributes ##

    ```php
    <?php
    $_product->getThisattribute();
    $_product->getAttributeText('thisattribute');
    $_product->getResource()->getAttribute('thisattribute')->getFrontend()->getValue($_product);
    $_product->getData('thisattribute');
    // 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
    endif;
    ?>
    ```

    ## Cart Data ##

    ```php
    <?php
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    print_r($cart);
    $cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
    print_r($cart);
    $session = Mage::getSingleton('checkout/session');
    foreach ($session->getQuote()->getAllItems() as $item) {
    echo $item->getName();
    Zend_Debug::dump($item->debug());
    }
    ?>
    ```

    ## Total items added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsCount();
    ?>
    ```

    ## Total Quantity added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();
    ?>
    ```

    ## Sub Total for item added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
    Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();
    ?>
    ```

    ## Grand total for for item added in cart ##

    ```php
    <?php
    Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();
    ?>
    ```

    ## Sub total of cart inkl tax without shipping ##

    ```php
    <?php
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $items = $quote->getAllItems();
    foreach ($items as $item) {
    $priceInclVat = $item->getRowTotalInclTax();
    }
    Mage::helper('checkout')->formatPrice($priceInclVat);
    ?>
    ```

    ## Get Simple Products of a Configurable Product ##

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

    ## Reset Development Environment (delete orders, customers, reset ids and counters, truncate statistics) ##

    ```sql
    SET FOREIGN_KEY_CHECKS=0;

    -- Here's where we reset the orders
    TRUNCATE `sales_flat_order`;
    TRUNCATE `sales_flat_order_address`;
    TRUNCATE `sales_flat_order_grid`;
    TRUNCATE `sales_flat_order_item`;
    TRUNCATE `sales_flat_order_status_history`;
    TRUNCATE `sales_flat_quote`;
    TRUNCATE `sales_flat_quote_address`;
    TRUNCATE `sales_flat_quote_address_item`;
    TRUNCATE `sales_flat_quote_item`;
    TRUNCATE `sales_flat_quote_item_option`;
    TRUNCATE `sales_flat_order_payment`;
    TRUNCATE `sales_flat_quote_payment`;
    TRUNCATE `sales_flat_shipment`;
    TRUNCATE `sales_flat_shipment_item`;
    TRUNCATE `sales_flat_shipment_grid`;
    TRUNCATE `sales_flat_invoice`;
    TRUNCATE `sales_flat_invoice_grid`;
    TRUNCATE `sales_flat_invoice_item`;
    TRUNCATE `sendfriend_log`;
    TRUNCATE `tag`;
    TRUNCATE `tag_relation`;
    TRUNCATE `tag_summary`;
    TRUNCATE `wishlist`;
    TRUNCATE `log_quote`;
    TRUNCATE `report_event`;

    ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
    ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
    ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
    ALTER TABLE `tag` AUTO_INCREMENT=1;
    ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
    ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
    ALTER TABLE `wishlist` AUTO_INCREMENT=1;
    ALTER TABLE `log_quote` AUTO_INCREMENT=1;
    ALTER TABLE `report_event` AUTO_INCREMENT=1;

    -- Here's where we reset the customers
    TRUNCATE `customer_address_entity`;
    TRUNCATE `customer_address_entity_datetime`;
    TRUNCATE `customer_address_entity_decimal`;
    TRUNCATE `customer_address_entity_int`;
    TRUNCATE `customer_address_entity_text`;
    TRUNCATE `customer_address_entity_varchar`;
    TRUNCATE `customer_entity`;
    TRUNCATE `customer_entity_datetime`;
    TRUNCATE `customer_entity_decimal`;
    TRUNCATE `customer_entity_int`;
    TRUNCATE `customer_entity_text`;
    TRUNCATE `customer_entity_varchar`;
    TRUNCATE `log_customer`;
    TRUNCATE `log_visitor`;
    TRUNCATE `log_visitor_info`;

    ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
    ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
    ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
    ALTER TABLE `log_customer` AUTO_INCREMENT=1;
    ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
    ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;

    -- This is to Reset all the ID counters
    TRUNCATE `eav_entity_store`;
    ALTER TABLE `eav_entity_store` AUTO_INCREMENT=1;

    SET FOREIGN_KEY_CHECKS=1;

    TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
    TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
    TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;

    DELETE FROM report_event WHERE event_type_id IN (SELECT event_type_id FROM report_event_types WHERE event_name IN ('catalog_product_view'));
    ```

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

    ```sql
    UPDATE
    customer_entity,
    newsletter_subscriber
    SET
    customer_entity.`group_id` = 5
    WHERE
    customer_entity.`entity_id` = newsletter_subscriber.`customer_id`
    AND
    newsletter_subscriber.`subscriber_status` = 1;
    ```

    ## Getting Configurable Product from Simple Product ID in Magento 1.5+ ##

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

    ## Check if customer is logged in ##

    ```php
    <?php
    $_customer = Mage::getSingleton('customer/session')->isLoggedIn();
    if ($_customer) {}
    ?>
    ```

    ## Get product image ##

    ```php
    <?= $this->helper('catalog/image')->init($_product, 'image'); ?>
    ```

    ## Show image using current skin path (PHTML) ##

    ```html
    <img src="<?= $this->getSkinUrl('images/logo.png'); ?>" alt="logo" />
    ```

    ## Show image using current skin path (CMS) ##

    ```html
    <img src={{skin url="images/logo.png"}} />
    ```

    ## Show CMS block (PHTML) ##

    ```php
    <?= $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_identifier')->toHtml(); ?>
    ```
  22. @arosenhagen arosenhagen revised this gist Apr 18, 2012. 1 changed file with 26 additions and 3 deletions.
    29 changes: 26 additions & 3 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,18 @@
    // Loggedin User
    Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn();
    // Check if customer is logged in
    $_customer = Mage::getSingleton('customer/session')->isLoggedIn();
    if ($_customer) {}

    // Get product image
    echo $this->helper('catalog/image')->init($_product, 'image');

    // Show image using current skin path (PHTML)
    <img src="<?php echo $this->getSkinUrl('images/logo.png');?>" alt="logo" />

    // Show image using current skin path (CMS)
    <img src={{skin url="images/logo.png"}} />

    // Show CMS block (PHTML)
    echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_identifier')->toHtml();

    // Total items added in cart
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    @@ -23,4 +36,14 @@ $items = $quote->getAllItems();
    foreach ($items as $item) {
    $priceInclVat = $item->getRowTotalInclTax();
    }
    Mage::helper('checkout')->formatPrice($priceInclVat);
    Mage::helper('checkout')->formatPrice($priceInclVat);


    // Swap product image on thumbnail click
    // Open app/design/frontend/yourpackage/yourtheme/template/catalog/product/view/media.phtml
    <li>
    <a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" title="<?php echo $_product->getName();?>" onclick="$('image').src = this.href; return false;">
    <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(70, 70); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>
    </a>
    </li>
    Go down to line 71 and change:
  23. @arosenhagen arosenhagen created this gist Apr 16, 2012.
    26 changes: 26 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    // Loggedin User
    Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn();

    // Total items added in cart
    Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsCount();

    // Total Quantity added in cart
    Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
    Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();

    // Sub Total for item added in cart
    Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
    Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();

    //grand total for for item added in cart
    Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();

    // sub total of cart inkl tax without shipping
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $items = $quote->getAllItems();
    foreach ($items as $item) {
    $priceInclVat = $item->getRowTotalInclTax();
    }
    Mage::helper('checkout')->formatPrice($priceInclVat);