Programmatically Add Magento Admin User Roles and Resources


Hi all again,

Today i would like to share, how to add magento admin user roles and resources using code.


/* 4 Roles Creation Default*/
$roles = array('User Role 1','User Role 2','User Role 3','User Role 4');
$roleIds = array();
$resources = explode(',', '__root__,admin/sales,admin/sales/order,admin/sales/order/actions,admin/sales/order/actions/hold,admin/sales/order/actions/creditmemo,admin/sales/order/actions/unhold,admin/sales/order/actions/ship,admin/sales/order/actions/comment,admin/sales/order/actions/invoice,admin/sales/order/actions/capture,admin/sales/order/actions/email,admin/sales/order/actions/view,admin/sales/order/actions/reorder,admin/sales/order/actions/edit,admin/sales/order/actions/review_payment,admin/sales/order/actions/cancel,admin/sales/order/actions/create,admin/dashboard,admin/catalog,admin/catalog/products,admin/cms,admin/cms/page,admin/cms/page/save,admin/system,admin/system/acl,admin/system/acl/users');
foreach($roles as $role){
$col = Mage::getModel('admin/role')->setRoleName($role)->setRoleType('G')->setTreeLevel(1)->save();
if($col->getRoleId()){
if( in_array($role, $enabledRoles) )
$roleIds[] = $col->getRoleId();
$rules = Mage::getModel('admin/rules')->setRoleId($col->getRoleId())->setResources($resources);
$rules = Mage::getModel('admin/resource_rules')->saveRel($rules);
}
}

We are created a new user roles(User Role 1, User Role 2, User Role 3 and User Role 4). Then we have to update the Role Resources. I have checked with Magento Version 1.7.0.2.

You can get the Resources list in the backend of the magento roles area.

If any suggestions welcome with comments area below.

Thank you.

Get Reviews for a product in magento


Hi Guys,
Here i have added some Magento Code for Get reviews collection or Reviews Summary collection data of a Product in Magento CMS. I hope it helps lot for beginners.

$_reviews = Mage::getModel('review/review')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addStatusFilter('approved')
->addEntityFilter('product', $_product->getId())
->setDateOrder(); // You can filter the results

foreach($_reviews as $re){
print_r($re->getData()); // You can get the results here
}

Magento – a foreign key constraint fails when reindex


Just execute the query in the database.
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;

get the logged user id in magento


Hi Guys,
Here is the information for magento logged in users data.
Just paste this code to your magento template files or php files too.

echo Mage::helper('customer')->getCustomer()->getId();
print_r(Mage::helper('customer')->getCustomer()->getData());
echo Mage::getSingleton('customer/session')->getCustomer()->getId();