Tuesday, 24 June 2014

Magento Create Limited & Random Product List.

In my case i have create featured product list with product 10 and it display randomaly.
Please copy past following code in your collection block file.


protected function _getProductCollection()
{
        if (is_null($this->_productCollection)) {
            $collection = Mage::getModel('catalog/product')->getCollection();
            $collection
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
                    ->addAttributeToFilter('featured_product', 1)
            ->addTaxPercents();
            $collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
            $collection->setPage(1, 10)->load();

            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
            $this->_productCollection = $collection;

        }
        return $this->_productCollection;
}

Magento Sort Category(Navigation) By Name or Alphabatically.

To sort Top Menu Categories and its Sub-Categoiries alphabetically,

Copy app/code/core/Mage/Catalog/Block/Navigation.php to app/code/local/Mage/Catalog/Block/Navigation.php OR you can override the same file in your local

public function toLinearArray($collection)
{
        $array = array();
        foreach ($collection as $item) $array[] = $item;
        return $array;
}

protected function _sortCategoriesByName($cat1, $cat2)
{
       return strcasecmp($cat1->getName(), $cat2->getName());
}

In Navigation.php edit the function _renderCategoryMenuItemHtml(), add the following 2 lines before the code $hasChildren = ($children && $childrenCount);

$children = $this->toLinearArray($children);
usort($children, array($this, '_sortCategoriesByName'));

If you need to sort Parent Categories too, use below code before your foreach loop when you display category list

<?php $helper = Mage::helper('catalog/category') ?>
<?php $categories = $helper->getStoreCategories(); ?>
               
<?php $children = $this->toLinearArray($categories);
usort($children, array($this, '_sortCategoriesByName'));?>
   
<?php foreach ($children as $_category): ?>
<?php echo $this->drawItem($_category) ?>
<?php endforeach ?>

Wednesday, 18 June 2014

Magento Product Attribute Show/Hide in Admin Depending on Other Attribute Value

Please past this code in design/adminhtml/catalog/product/edit.phtml

<script type="text/javascript">

    Event.observe(window, 'load', function() {
        var $delDay = $("featured"); // featured is yes/no dropdown id
        if ($delDay.value == 0) {
        $('featured_position').value = ''; // delivery_time is show.hide attribute name
        $('featured_position').disable()
        }
    });

    Event.observe($("featured"), 'change', function() {
        var $delDay = $("featured");
        if ($delDay.value == 1) {
            $('featured_position').enable();
        }
        else {
            $('featured_position').value = '';
            $('featured_position').disable();
        }
    });
</script>

Friday, 13 June 2014

Magento Install GeoIP Extesion

In following link you can get GeoIP Extesion.
This extension allows you to use free GeoIP Lite database by MaxMind with your Magento installation. 

http://www.magentocommerce.com/magento-connect/geoip.html

Magento Create Custom Attribute in Category

1) Yes / No Dropdown

<?php

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$setup->addAttribute('catalog_category', 'is_catalog', array(
   'type' => 'int',
    'group' => 'General',
    'backend' => '',
    'frontend' => '',
    'label' => 'Is Catalog ',
    'input' => 'select',
    'class' => '',
    'source' => 'eav/entity_attribute_source_boolean',            // Leave blank for input type text box
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => false,
    'default' => '0',  //Leave blank for input type text box
    'searchable' => false,
    'filterable' => false,
    'comparable' => false,
    'visible_on_front' => false,
    'unique' => false
));
$installer->endSetup();

2) Textbox

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$setup->addAttribute('catalog_category', 'my_attribute', array(
'group' => 'General',
'input' => 'text',
'type' => 'varchar',
'label' => 'My Attribute',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->endSetup();


Wednesday, 28 May 2014

Magento Reindex Programmatically

for($i=1;$i<=9;$i++){
    Mage::getModel('index/process')->load($i)->reindexEverything();
 }

 // where I is

 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

Magento Clear Cache Programmatically

Mage::app()->cleanCache();