Wednesday, May 29, 2013

Magento : Adding a Yes/No type custom category attribute

Hello, Here is the another magento code snippet that i have got to share with you.

[php]
<?php
require_once("app/Mage.php");
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('catalog_category', 'left_nav', array(
'group' => 'General Information',
'type' => 'int',
'label' => 'Show in left navigation',
'input' => 'select',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'source' => 'eav/entity_attribute_source_boolean'
));
?>
[/php]

After running this script and refreshing the admin end you will find the added attribute at end of General Information tab.

Hope this helps some one
Thanks.

Magento : How to remove any attribute from category programatically

Here is the simple code for deleting any category attribute using the simple lines of code.
[php]

require_once("app/Mage.php");
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('catalog_category');

$setup->removeAttribute($entityTypeId,'display_mode');

[/php]

This could be usefull if you added some custom attribute and want to remove them back.

Happy coding :-)

Monday, May 13, 2013

Magento : Get products from category order by product ID

Here is the very simple code for getting products from a particular category and order them using product ID in decending order.

[php]
error_reporting(1);
ini_set('display_errors','1');
require_once('app/Mage.php');
Mage::app('default');


$products = Mage::getModel('catalog/category')->load(3345) //3345 : Category ID
->getProductCollection()
->addAttributeToSelect('*')->setOrder('entity_id','DESC'); //Order the collection in decending order.

foreach ($products as $p) {
echo $p->getSku().'<br/>';

}

[/php]

Hope it helps some one.

Happy coding.