{"id":859,"date":"2014-02-20T04:28:33","date_gmt":"2014-02-20T04:28:33","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=859"},"modified":"2024-07-22T09:42:10","modified_gmt":"2024-07-22T09:42:10","slug":"magento-switch-to-admin-role-programmatically","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/magento-switch-to-admin-role-programmatically\/","title":{"rendered":"Magento \u2013 Switch To Admin Role Programmatically"},"content":{"rendered":"

Many a tasks can be only performed by the admin user only. The need for automated task on backend is obvious, i faced this issue many times and i\u2019m going to share my solution to you soon. This article will show you how to run task in admin role the right way.<\/p>\n


\nThis code will provide admin access. After this code we can perform admin tasks.<\/p>\n

\n&amp;amp;lt;?phpclass Custom_Model_Observer {\n    \/\/ call this function to switch to admin mode\n    private function _initAdmin() {\n        Mage::app()-&amp;amp;gt;setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);\n        Mage::getSingleton(&amp;amp;quot;core\/session&amp;amp;quot;, array(&amp;amp;quot;name&amp;amp;quot;=&amp;amp;gt;&amp;amp;quot;adminhtml&amp;amp;quot;));\n        $userModel = Mage::getModel(&amp;amp;quot;admin\/user&amp;amp;quot;)-&amp;amp;gt;load(1);\n        $session = Mage::getSingleton(&amp;amp;quot;admin\/session&amp;amp;quot;);\n        $session-&amp;amp;gt;setUser($userModel);\n    }\n<\/pre>\n

To switch to admin role before any task. you just need to call this function prior to you code<\/p>\n

Example<\/p>\n

\n&amp;amp;lt;?php\nclass Custom_Model_Observer {\n     \u2026..\n     public function my_custom_function() {\n          $this-&amp;amp;gt;_initAdmin();\n          \/\/ your code go here\n     }\n<\/pre>\n

i hope you will find this snippet useful \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"

Many a tasks can be only performed by the admin user only. The need for automated task on backend is obvious, i faced this issue many times and i\u2019m going to share my solution to you soon. This article will show you how to run task in admin role the right way. This code will…<\/p>\n

Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":1486,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"aioseo_notices":[],"views":10,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/859"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=859"}],"version-history":[{"count":3,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/859\/revisions"}],"predecessor-version":[{"id":1487,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/859\/revisions\/1487"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media\/1486"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=859"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}