Browsing articles in "Magento"
Oct 16, 2012

Magento: Get details of all Admin users

As we can get details of all the customers of the Magento store, we can also get details of all the Admin users. It may be necessary sometimes when you want to display all the admin uses either in dropdown for filtering something or just as a text or something based on requirement.

The following code will get you all the Admin users with their details:

$adminUserModel = Mage::getModel('admin/user');
$userCollection = $adminUserModel->getCollection()->load(); 
Sep 13, 2012

Magento: Get category object from category name

Although this is very weird, to get category object (id, entity_type_id, attribute_set_id, children, etc.) from category name, sometimes it may be useful. Below lines of code will help you in getting it.

$cat = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', 'Category_Name_Here');

To get the category id from $cat object, simply use:


Beware, the name should only return one category else the above query will return first category id no matter how many categories are retrieved.

Sep 13, 2012

Magento: Get product attribute’s select option id/label/value

If you have a select dropdown for any product attribute, to get the value from label or vice versa is always needed in order to display or get value for further processing, etc. Every now and then you will require this values while working on product attributes. There aer many ways you can achieve it but the best, in terms of performance and simplicity is what I will tell you here. Get product attribute’s value from label, label from value easily in Magento.

Suppose, you have an product attribute called “color” in Magento. You have the label (e.g. Red), and you want to find it’s value. The below code will help you get the value for it.

$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
    echo $color_id = $attr->getSource()->getOptionId("Red");

Continue reading »

Sep 13, 2012

Magento: Submitting form in admin redirects to dashboard

When trying to submit a form which is created in admin, it redirects to dashboard no matter how much you try. I faced this problem and tried many things but without any luck. Everything was running, for file upload I had enctype=”multipart/form-data” in form, checked the form action through firebug and run it directly in browser URL which was also going good. After so much frustration, googled the problem and found that I was not the only one to face this problem.

Finally, got one forum where I found the solution to this problem. In Magento, when submitting the form, you need to have one extra hidden field, with name “form_key” which should have form’s key value from session. Here is the line of code, which helped me in posting form successfully.

<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />

Hope this helps some frustrated mind 🙂

Aug 22, 2012

Magento: Get route name, module name, controller and action name from URL

Getting route name, module name, controller name and action name is very easy in Magento. You can get these values anywhere, in controller itself and also in template files.





Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors