Get all Root-Category-IDs of all stores

This snippet gives you all entity_id’s of all Root-IDs of the Magento instance: /** * @return array Root-IDs of all stores */ public function getAllRootIds(){# $aRootIds = array(); $aRootIds[Mage_Catalog_Model_Category::TREE_ROOT_ID] = Mage_Catalog_Model_Category::TREE_ROOT_ID; $aAllStores = Mage::app()->getStores(); foreach ($aAllStores as $iID => $store){ $rootID = $store->getRootCategoryId(); $aRootIds[$rootID]=$rootID; } return array_keys($aRootIds); }  

Install attribute in entity main table

Sometimes it is useful to add some fields to the attribute main table (f.e.: IDs from external systems like a middleware or a PIM). In the following example you can see how to add ID-fields to the category entity. Therefore I created a setupscript as an instance of Mage_Catalog_Model_Resource_Setup. Setup-model declaration in config.xml: <?xml version=”1.0″?> […]

Send a file header in controller (file download)

To return a downloadable file in a controller action, you have to send a file header in controller like that: $fileName = ‘mm_actioncodes_export_’.date(“Ymd_His”).’.csv’; $fp = fopen(Mage::getBaseDir(‘var’) . DS .’export’ . DS . $fileName, ‘w’); fputcsv($fp, $codes, ‘;’, ‘”‘); fclose($fp); $this->_prepareDownloadResponse($fileName, array( ‘type’ => ‘filename’, ‘value’ => Mage::getBaseDir(‘var’) . DS .’export’ . DS . $fileName, //’rm’ => true […]

IP Detection

This little snippet tries to get the IP-Address of the user: private function _detectIP(){ //Mage::log(‘_detectIP’, null, __CLASS__.’.log’, true); if (getenv(‘HTTP_CLIENT_IP’)) { $this->_ip = getenv(‘HTTP_CLIENT_IP’); Mage::log(‘Detected IP (getenv(\’HTTP_CLIENT_IP\’)):’, null, __CLASS__.’.log’, true); Mage::log($this->_ip, null, __CLASS__.’.log’, true); } elseif (getenv(‘HTTP_X_FORWARDED_FOR’)) { $ips = explode(‘,’, getenv(‘HTTP_X_FORWARDED_FOR’)); $this->_ip = (string)trim($ips[0]); } elseif (getenv(‘HTTP_X_FORWARDED’)) { $ips = explode(‘,’, getenv(‘HTTP_X_FORWARDED’)); $this->_ip = […]

Directly query database

To directly send queries against the database: //$read = Varien_Db_Adapter_Mysqli extends Zend_Db_Adapter_Mysqli extend Zend_Db_Adapter_Abstract $read = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’); $query = ” SELECT SUM(`grand_total`) AS `grand_total`, SUM(`subtotal`) AS `subtotal`, SUM(`tax_amount`) AS `tax_amount`, SUM(`discount_amount`) AS `discount_amount`, SUM(`shipping_amount`) AS `shipping_amount`, FROM `sns_salesreport_monthstats` WHERE `year` = $year “; $data = $read->fetchRow($query); //fetches only the first row // or multiple rows: $sql […]

Add Content In Magento Controller

If you want to add content to the rendered page inside a controller action, you can do it like that: $this->loadLayout(); $message = $this->getLayout() ->createBlock(‘core/text’, ‘ready-block’) ->setText(‘<h1>Testaction for Controller:</h1>’); $this->getLayout()->getBlock(‘content’)->insert($message); $this->renderLayout();  

Delete users in Magento frontend

If you need the possibility to let the users delete their own accounts, you can use the following snippet in a corresponding controller or observer to delete users in frontend: <?php $oCustomer = $this->_getSession()->getCustomer(); if(!$oCustomer->getId()){ Mage::throwException($this->__(‘Cannot load customer!’)); } //lie to mage 😉 // @todo: Check, if there is a better way Mage::register(‘isSecureArea’, true); $oCustomer->delete(); $this->_getSession()->clear(); […]

Performance optimized attribute saving

Sometimes, when you have to handle many data-sets, it is more efficient to save only those attributes that have changed: // load product $product = Mage::getModel(“catalog/product”)->load(142);   // change attribute $product->setTitle(“Test”);   // tell resource attribute to save only the specified field $product->getResource()->saveAttribute($product, “title”);  

Add image product attribute

This is how to get a new image attribute to a given attribute set inside of a setup script (add image attribute for products): <?php /** * * Date: 5.12.2017 * Time: 0:58:0 * Last modified: 0:57:28 * Class / File: mysql4-upgrade-0.5.2-0.5.3.php * * Installs additional image attributes for customization * preview on product detail […]