Skip to content

Instantly share code, notes, and snippets.

@OdinsHat
Forked from grafikchaos/gist:9938618
Last active April 22, 2018 07:12
Show Gist options
  • Select an option

  • Save OdinsHat/8e896aafcd8093b13b23 to your computer and use it in GitHub Desktop.

Select an option

Save OdinsHat/8e896aafcd8093b13b23 to your computer and use it in GitHub Desktop.

Revisions

  1. OdinsHat renamed this gist Jul 18, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. OdinsHat revised this gist May 22, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -662,10 +662,10 @@ Imagine you have a controller: ```controllers/IndexController.php```
    With action:

    ```php
    iframeAction()
    public iframeAction()
    {
    $this->loadLayout();
    $this->renderLayout();
    $this->renderLayout();
    }
    ```
    Add to the layout.xml or module layout xml file:
  3. OdinsHat revised this gist May 22, 2015. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -655,6 +655,36 @@ OR
    `path` = "dev/debug/template_hints_blocks";
    ```

    ## Create New Route ##

    Imagine you have a controller: ```controllers/IndexController.php```

    With action:

    ```php
    iframeAction()
    {
    $this->loadLayout();
    $this->renderLayout();
    }
    ```
    Add to the layout.xml or module layout xml file:
    ```xml
    <productdesignr_index_iframe>
    <reference name="root">
    <action method="setTemplate"><template>develo_productdesignr/tool/iframe.phtml</template></action>
    </reference>
    <reference name="head">
    <action method="addCss">
    <name>productdesignr/styles/app.css</name>
    </action>
    </reference>
    <reference name="content">
    <block type="productdesignr/tool_view" name="design_tool" template="develo_productdesignr/tool/view.phtml" />
    </reference>
    </productdesignr_index_iframe>
    ```

    ## Get a single product attribute's value ##

    Source: http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/
  4. OdinsHat revised this gist Dec 11, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -248,6 +248,15 @@ $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedPro
    ?>
    ```

    ## Get Configurable products with certain attribute ##

    ```php
    $collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect(array('material','shape'))
    ->addAttributeToFilter('type_id', array('eq' => 'configurable'))
    ->addAttributeToFilter('shape', array('eq' => '155'));
    ```

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

    ```php
  5. OdinsHat revised this gist Dec 11, 2014. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,15 @@
    # Magento Snippets #

    ## Changing Order Status ##

    ```php
    // shipping_cdl setup in the admin area under shipping methods
    $orderModel = Mage::getModel('sales/order');
    $orderModel->reset()->load($orderId);
    $orderModel->addStatusHistoryComment('Sent to CDL', 'shipping_cdl');
    $orderModel->setStatus('shipping_cdl')->save();
    ```

    ## Add Attribute Group and Attributes to Attribute Sets ##

    In an install script, you can follow the below example:
  6. Josh J. revised this gist Sep 4, 2014. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -150,6 +150,38 @@ $installer->addAttribute(
    $installer->endSetup();
    ```

    ## Add attributes to product collection (and flat table) via config.xml ##

    Thanks to this [StackOverflow answer](http://stackoverflow.com/a/11798502/633432): http://stackoverflow.com/a/11798502/633432


    In your module's `etc/config.xml` file

    ```xml
    <config>
    <!-- ... some xml nodes ... -->

    <frontened>
    <!--
    Add our custom attributes to the product collection (and the product flat table)
    w/o having to specify the attribute being 'Used in Product Listing'
    -->
    <product>
    <collection>
    <attributes>
    <custom_attributecode1/>
    <custom_attributecode2/>
    <custom_attributecode3/>
    </attributes>
    </collection>
    </product>
    </frontend>

    <!-- ... more xml nodes ... -->
    </config>
    ```


    ## Download extension manually using pear/mage ##
    Pear for 1.4, mage for 1.5. File downloaded into /downloader/.cache/community/

  7. Josh J. revised this gist Sep 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -141,7 +141,7 @@ $installer->addAttribute(
    'visible_on_front' => false,
    'user_defined' => true,
    'default_value' => false,
    'used_in_product_listing' => true,
    'used_in_product_listing' => true,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    )
    );
  8. Josh J. revised this gist Sep 4, 2014. 1 changed file with 150 additions and 0 deletions.
    150 changes: 150 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,155 @@
    # Magento Snippets #

    ## Add Attribute Group and Attributes to Attribute Sets ##

    In an install script, you can follow the below example:

    ```php
    <?php
    /**
    * Pan_JewelryDesigner Extension
    *
    * @category Pan
    * @package Pan_JewelryDesigner
    * @copyright Copyright (c) 2014 August Ash, Inc. (http://www.augustash.com)
    * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
    * @author August Ash Team <core@augustash.com>
    */

    // need to use the Mage_Eav_Model_Entity_Setup class b/c it has the attribute-related methods
    $installer = new Mage_Eav_Model_Entity_Setup();

    // start transaction
    $installer->startSetup();

    // get all attribute sets (filter only sets for products)
    $sets = Mage::getModel('eav/entity_attribute_set')
    ->getResourceCollection()
    ->addFilter('entity_type_id', Mage_Catalog_Model_Product::ENTITY);

    //loop through all the sets
    foreach ($sets as $set) {
    // create an attribute group instance
    $modelGroup = Mage::getModel('eav/entity_attribute_group');

    // set the group name
    $modelGroup->setAttributeGroupName('Jewelry Designer')
    ->setAttributeSetId($set->getId())
    ->setSortOrder(25);

    // save the new group
    $modelGroup->save();
    }

    /**
    * Create Jewelry Designer specific attributes and
    * assign to our 'Jewelry Designer' attribute group
    */

    // Media Image attributes
    $imgAttrs = array('designer_canvas', 'designer_grid_thumb');

    foreach ($imgAttrs as $attrCode) {
    $data = array(
    'group' => 'Images',
    'input' => 'media_image',
    'label' => ucwords(str_replace('_', ' ', $attrCode)),
    'visible' => true,
    'required' => false,
    'visible_on_front' => false,
    'user_defined' => true,
    'used_in_product_listing' => true,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    );

    $installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, $data);
    }


    /**
    * Product Type (i.e., 'bracelet', 'charm', 'clip', 'spacer', etc.)
    */
    $installer->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'item_type',
    array(
    'group' => 'Jewelry Designer',
    'type' => 'varchar',
    'input' => 'select',
    'label' => 'Item Type',
    'visible' => true,
    'required' => false,
    'visible_on_front' => false,
    'user_defined' => true,
    'used_in_product_listing' => true,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source' => 'eav/entity_attribute_source_table',
    'option' => array(
    'values' => array(
    'bracelet' => 'Bracelet',
    'charm' => 'Charm',
    'clip' => 'Clip',
    'spacer' => 'Spacer'
    )
    ),

    )
    );


    /**
    * Bead Width (bead_width)
    */
    $installer->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'bead_width',
    array(
    'group' => 'Jewelry Designer',
    'type' => 'varchar',
    'input' => 'select',
    'label' => 'Bead/Charm Size (Width)',
    'visible' => true,
    'required' => false,
    'visible_on_front' => false,
    'user_defined' => true,
    'used_in_product_listing' => true,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source' => 'eav/entity_attribute_source_table',
    'option' => array(
    'values' => array(
    'tiny' => 'Tiny',
    'small' => 'Small',
    'medium' => 'Medium',
    'large' => 'Large'
    )
    ),
    )
    );

    /**
    * Exclude From Designer (exclude_from_designer)
    */
    $installer->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'exclude_from_designer',
    array(
    'group' => 'Jewelry Designer',
    'input' => 'boolean',
    'label' => 'Exclude from Designer App',
    'visible' => true,
    'required' => false,
    'visible_on_front' => false,
    'user_defined' => true,
    'default_value' => false,
    'used_in_product_listing' => true,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    )
    );

    // end transaction
    $installer->endSetup();
    ```

    ## Download extension manually using pear/mage ##
    Pear for 1.4, mage for 1.5. File downloaded into /downloader/.cache/community/

  9. Josh J. revised this gist Apr 9, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -498,6 +498,7 @@ TRUNCATE TABLE `catalog_product_super_attribute`;
    TRUNCATE TABLE `catalog_product_super_link`;
    TRUNCATE TABLE `catalog_product_enabled_index`;
    TRUNCATE TABLE `catalog_product_website`;
    TRUNCATE TABLE `catalog_product_relation`;
    TRUNCATE TABLE `catalog_category_product_index`;
    TRUNCATE TABLE `catalog_category_product`;
    TRUNCATE TABLE `cataloginventory_stock_item`;
  10. Josh J. revised this gist Apr 2, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -503,6 +503,7 @@ TRUNCATE TABLE `catalog_category_product`;
    TRUNCATE TABLE `cataloginventory_stock_item`;
    TRUNCATE TABLE `cataloginventory_stock_status`;
    TRUNCATE TABLE `cataloginventory_stock`;
    TRUNCATE TABLE `catalog_product_flat_1`;
    INSERT INTO `catalog_product_link_type`(`link_type_id`,`code`) VALUES (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
    INSERT INTO `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) VALUES (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
    INSERT INTO `cataloginventory_stock`(`stock_id`,`stock_name`) VALUES (1,'Default');
  11. Josh J. revised this gist Apr 2, 2014. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -508,4 +508,27 @@ INSERT INTO `catalog_product_link_attribute`(`product_link_attribute_id`,`link_
    INSERT INTO `cataloginventory_stock`(`stock_id`,`stock_name`) VALUES (1,'Default');
    TRUNCATE TABLE `catalog_product_entity`;
    SET FOREIGN_KEY_CHECKS = 1;
    ```

    ## Delete All Categories ##

    Source: http://www.codelogbook.com/remove-all-categories-from-a-magento-store/

    ```sql
    SET FOREIGN_KEY_CHECKS = 0;
    TRUNCATE TABLE `catalog_category_entity`;
    TRUNCATE TABLE `catalog_category_entity_datetime`;
    TRUNCATE TABLE `catalog_category_entity_decimal`;
    TRUNCATE TABLE `catalog_category_entity_int`;
    TRUNCATE TABLE `catalog_category_entity_text`;
    TRUNCATE TABLE `catalog_category_entity_varchar`;
    TRUNCATE TABLE `catalog_category_product`;
    TRUNCATE TABLE `catalog_category_product_index`;

    INSERT INTO `catalog_category_entity`(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`POSITION`,`level`,`children_count`) VALUES (1,3,0,0,'0000-00-00 00:00:00','2009-02-20 00:25:34','1',1,0,1),(2,3,3,0,'2009-02-20 00:25:34','2009-02-20 00:25:34','1/2',1,1,0);

    INSERT INTO `catalog_category_entity_int`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) VALUES (1,3,32,0,2,1),(2,3,32,1,2,1);

    INSERT INTO `catalog_category_entity_varchar`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) VALUES (1,3,31,0,1,'Root Catalog'),(2,3,33,0,1,'root-catalog'),(3,3,31,0,2,'Default Category'),(4,3,39,0,2,'PRODUCTS'),(5,3,33,0,2,'default-category');
    SET FOREIGN_KEY_CHECKS = 1;
    ```
  12. Josh J. revised this gist Apr 2, 2014. No changes.
  13. Josh J. revised this gist Apr 2, 2014. 1 changed file with 48 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -460,4 +460,52 @@ Source: http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attr

    ```php
    <?php echo $_product->getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product) ?>
    ```

    ## Delete All Products ##

    Source: [http://www.propellermind.com/magento-tips/delete-all-products-from-magento-using-phpmyadmin/](http://www.propellermind.com/magento-tips/delete-all-products-from-magento-using-phpmyadmin/)

    ```sql
    SET FOREIGN_KEY_CHECKS = 0;
    TRUNCATE TABLE `catalog_product_bundle_option`;
    TRUNCATE TABLE `catalog_product_bundle_option_value`;
    TRUNCATE TABLE `catalog_product_bundle_selection`;
    TRUNCATE TABLE `catalog_product_entity_datetime`;
    TRUNCATE TABLE `catalog_product_entity_decimal`;
    TRUNCATE TABLE `catalog_product_entity_gallery`;
    TRUNCATE TABLE `catalog_product_entity_int`;
    TRUNCATE TABLE `catalog_product_entity_media_gallery`;
    TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
    TRUNCATE TABLE `catalog_product_entity_text`;
    TRUNCATE TABLE `catalog_product_entity_tier_price`;
    TRUNCATE TABLE `catalog_product_entity_varchar`;
    TRUNCATE TABLE `catalog_product_link`;
    TRUNCATE TABLE `catalog_product_link_attribute`;
    TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
    TRUNCATE TABLE `catalog_product_link_attribute_int`;
    TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
    TRUNCATE TABLE `catalog_product_link_type`;
    TRUNCATE TABLE `catalog_product_option`;
    TRUNCATE TABLE `catalog_product_option_price`;
    TRUNCATE TABLE `catalog_product_option_title`;
    TRUNCATE TABLE `catalog_product_option_type_price`;
    TRUNCATE TABLE `catalog_product_option_type_title`;
    TRUNCATE TABLE `catalog_product_option_type_value`;
    TRUNCATE TABLE `catalog_product_super_attribute_label`;
    TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
    TRUNCATE TABLE `catalog_product_super_attribute`;
    TRUNCATE TABLE `catalog_product_super_link`;
    TRUNCATE TABLE `catalog_product_enabled_index`;
    TRUNCATE TABLE `catalog_product_website`;
    TRUNCATE TABLE `catalog_category_product_index`;
    TRUNCATE TABLE `catalog_category_product`;
    TRUNCATE TABLE `cataloginventory_stock_item`;
    TRUNCATE TABLE `cataloginventory_stock_status`;
    TRUNCATE TABLE `cataloginventory_stock`;
    INSERT INTO `catalog_product_link_type`(`link_type_id`,`code`) VALUES (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
    INSERT INTO `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) VALUES (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
    INSERT INTO `cataloginventory_stock`(`stock_id`,`stock_name`) VALUES (1,'Default');
    TRUNCATE TABLE `catalog_product_entity`;
    SET FOREIGN_KEY_CHECKS = 1;
    ```
  14. @petemcw petemcw revised this gist Aug 24, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -456,6 +456,8 @@ OR

    ## Get a single product attribute's value ##

    Source: http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/

    ```php
    <?php echo $_product->getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product) ?>
    ```
  15. @petemcw petemcw revised this gist Aug 24, 2011. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -452,4 +452,10 @@ WHERE
    `path` = "dev/debug/template_hints"
    OR
    `path` = "dev/debug/template_hints_blocks";
    ```

    ## Get a single product attribute's value ##

    ```php
    <?php echo $_product->getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product) ?>
    ```
  16. @davidalexander davidalexander revised this gist Aug 18, 2011. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -438,4 +438,18 @@ if($_product->getTypeId() == "configurable") {
    <?php
    }
    ?>
    ```


    ## Turn template hints on/off via database ##

    ```sql
    UPDATE
    `core_config_data`
    SET
    `value` = 0
    WHERE
    `path` = "dev/debug/template_hints"
    OR
    `path` = "dev/debug/template_hints_blocks";
    ```
  17. @davidalexander davidalexander revised this gist Jul 31, 2011. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -412,4 +412,30 @@ foreach ($session->getQuote()->getAllItems() as $item) {
    Zend_Debug::dump($item->debug());
    }
    ?>
    ```

    ## 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>
    <?php
    echo $simpleproduct->getName() . " - " . (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty();
    ?>
    </li>
    <?php
    }
    ?>
    </ul>
    <?php
    }
    ?>
    ```
  18. @davidalexander davidalexander revised this gist Jul 19, 2011. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -396,4 +396,20 @@ 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());
    }
    ?>
    ```
  19. @davidalexander davidalexander revised this gist Jul 19, 2011. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -385,5 +385,15 @@ $_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;
    ?>
    ```
  20. @davidalexander davidalexander revised this gist Jul 17, 2011. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -113,10 +113,14 @@ to `page.xml`, and then add the `mytemplate.phtml` file. Any block added to the
    <?php $logged_in = Mage::getSingleton('customer/session')->isLoggedIn(); // (boolean) ?>
    ```

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

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

    ## Run Magento Code Externally ##
  21. @davidalexander davidalexander revised this gist Jul 17, 2011. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Magento Snippets #

    ## Download extension manually using pear/mage ##
    Pear for 1.4, mage for 1.5. Goes into /downloader/.cache/community/
    Pear for 1.4, mage for 1.5. File downloaded into /downloader/.cache/community/

    ./pear download magento-community/Shipping_Agent
    ./mage download community Shipping_Agent
    @@ -371,4 +371,15 @@ $_countries = Mage::getResourceModel('directory/country_collection')
    'values' => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),
    ));
    ?>
    ```

    ## Return Product Attributes ##

    ```php
    <?php
    $_product->getThisattribute();
    $_product->getAttributeText('thisattribute');
    $_product->getResource()->getAttribute('thisattribute')->getFrontend()->getValue($_product);
    $_product->getData('thisattribute');
    ?>
    ```
  22. @davidalexander davidalexander revised this gist Jul 17, 2011. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,17 @@ $_product_2 = Mage::getModel('catalog/product')->loadByAttribute('sku','cordoba-
    ?>
    ```

    ## Get Configurable product’s Children’s (simple product) custom attributes ##

    ## 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
    @@ -282,6 +292,7 @@ if($_productCollection->count()) {
    echo $this->htmlEscape($_product->getName());
    endforeach;
    }
    ?>
    ```

    ## Update all subscribers into a customer group (e.g. 5) ##
    @@ -328,6 +339,7 @@ $countryList = Mage::getResourceModel('directory/country_collection')
    echo '<pre>';
    print_r( $countryList);
    exit('</pre>');
    ?>
    ```

    ## Create a Country Drop Down in the Frontend of Magento ##
  23. @davidalexander davidalexander revised this gist Jul 17, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    ## Download extension manually using pear/mage ##
    Pear for 1.4, mage for 1.5. Goes into /downloader/.cache/community/

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

  24. @davidalexander davidalexander revised this gist Jul 17, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Magento Snippets #

    ## Download extension manually using pear/mage ##

    Pear for 1.4, mage for 1.5. Goes into /downloader/.cache/community/
    ./pear download magento-community/Shipping_Agent
    ./mage download community Shipping_Agent

  25. @davidalexander davidalexander revised this gist Jul 17, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@

    ## Download extension manually using pear/mage ##

    ./pear download magento-community/Shipping_Agent # 1.4.*
    ./mage download community Shipping_Agent # 1.5.*
    ./pear download magento-community/Shipping_Agent
    ./mage download community Shipping_Agent

    ## Clear cache/reindex ##

  26. @davidalexander davidalexander revised this gist Jul 17, 2011. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    # Magento Snippets #

    ## Download module files manually using Magento Connect 2 ##
    ## Download extension manually using pear/mage ##

    ./mage download community Shipping_Agent
    ./pear download magento-community/Shipping_Agent # 1.4.*
    ./mage download community Shipping_Agent # 1.5.*

    ## Clear cache/reindex ##

  27. @davidalexander davidalexander revised this gist Jul 16, 2011. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -335,8 +335,7 @@ $countryList = Mage::getResourceModel('directory/country_collection')
    $_countries = Mage::getResourceModel('directory/country_collection')
    ->loadData()
    ->toOptionArray(false) ?>
    if (count($_countries) > 0) {
    ?>
    <?php if (count($_countries) > 0): ?>
    <select name="country" id="country">
    <option value="">-- Please Select --</option>
    <?php foreach($_countries as $_country): ?>
    @@ -345,9 +344,7 @@ if (count($_countries) > 0) {
    </option>
    <?php endforeach; ?>
    </select>
    <?php
    }
    ?>
    <?php endif; ?>
    ```

    ## Create a Country Drop Down in the Magento Admin ##
  28. @davidalexander davidalexander revised this gist Jul 16, 2011. 1 changed file with 47 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -313,4 +313,51 @@ In /app/design/frontend/default/site/template/catalog/product/view/type/
    <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) ?>
    if (count($_countries) > 0) {
    ?>
    <select name="country" id="country">
    <option value="">-- Please Select --</option>
    <?php foreach($_countries as $_country): ?>
    <option value="<?php echo $_country['value'] ?>">
    <?php echo $_country['label'] ?>
    </option>
    <?php endforeach; ?>
    </select>
    <?php
    }
    ?>
    ```

    ## Create a Country Drop Down in the Magento Admin ##

    ```php
    <?php
    $fieldset->addField('country', 'select', array(
    'name' => 'country',
    'label' => 'Country',
    'values' => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),
    ));
    ?>
    ```
  29. @davidalexander davidalexander created this gist Jul 16, 2011.
    316 changes: 316 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,316 @@
    # Magento Snippets #

    ## Download module files manually using Magento Connect 2 ##

    ./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
    */
    ?>
    ```

    ## 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 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
    <?php echo $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
    <?php echo $this->getChildHtml() ?>
    ```

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

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

    ## Check if customer is logged in ##

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

    ## Get the current category ##

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

    ## 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
    <?php echo 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-<?php echo $category->getId() ?>" class="level1">
    <?php foreach($subcategories as $subcategory): ?>
    <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
    <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
    </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
    <?php echo 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;
    }
    ```

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

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