Debug database queries with parameters

Sometimes you have to query the database directly in your module. In these situations you can use the placeholder feature of the Zend_Db_Statement component. :placeholder will be replaced by the respective value when calling fetch on a Zend_Db_Adapter_Mysqli instance with an array of replacements for example. If you need to debug the query with all parameters in it, […]

Change config during runtime

Sometimes you need to change the Magento configuration during runtime. For example this is the way to temporarily disable the category flat tables <?php Mage::app()->getCacheInstance()->banUse(‘config’); //better switch of config cache Mage::app()->getStore()->setConfig(‘catalog/frontend/flat_catalog_category’, “0”); // … Do wired stuff … Mage::app()->getStore()->setConfig(‘catalog/frontend/flat_catalog_category’, “1”); Attention: You have to reindex categories afterwards!!!

Disable usage of flat tables for categories via code

<?php $oCategoryCollection = Mage::getModel(‘catalog/category’, array(‘disable_flat’ => true))->getCollection(); That’s it! Now the “normal” entity tables are used. That work’s because the field disable_flat decides what kind of collection is loaded in the constructor of Mage_Catalog_Model_Category: <?php class Mage_Catalog_Model_Category extends Mage_Catalog_Model_Abstract { //… /** * Initialize resource mode * * @return void */ protected function _construct() { // […]

Simulate store to set or get store specific values

In earlier Magento versions it was quite difficult to set or read store specific values on entities. In most cases you have to set the store on the whole application instance like that: <?php Mage::app(STOREID)->setStore(STOREID); … $oProduct->setStore(STOREID); … $oCollection->setStore(STOREID); Sometimes it was even necessary to add the store  to entities or collections it-selves too. Now (at least […]

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″?> […]

Make additional product attributes available over REST-API

To do this you only have to add a api2.xml in your modules etc folder that puts the necessary product tags into the node /config/api2/resources/product/attributes (xPath): <config> <api2> <resources> <product> <attributes> <sheldon_ean>EAN</sheldon_ean> <sheldon_articlenbr>Articlenumber (Model)</sheldon_articlenbr> <sheldon_stylenbr>Stylenumber</sheldon_stylenbr> <sheldon_color>Color</sheldon_color> <sheldon_size>Size</sheldon_size> <sheldon_material_description>Material Description</sheldon_material_description> <sheldon_supplement>Supplement</sheldon_supplement> <sheldon_carehints>Carehints</sheldon_carehints> <sheldon_is_aboable>Is aboable product</sheldon_is_aboable> <sheldon_is_sale_product>Is sale product</sheldon_is_sale_product> <sheldon_is_new_product>Is new product</sheldon_is_new_product> <sheldon_is_reddot_product>Is reddot Product</sheldon_is_reddot_product> <sheldon_reddot_years>Product Reddotyears</sheldon_reddot_years> […]

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 = […]