Magento

Magento: Limit the address fields to n characters

Mar 21, 2011

There are a number of reasons you may want to do this – but primarily I think it is because your despatch system will likely be printing labels and if the lines are too long it won’t work properly. You could try dealing with the line lengths when you export but that leaves you with the messy situations of trying to work out where to put the bits of the address yo uhave chopped off.

Magento: Retrieve the store name, inside a module

Mar 17, 2011

I thought this would be simple usually that is my first mistake with Magento. As it transpires it really is simple for once but worth mentioning, if only so that when I need it again in six months time I have it written down somewhere. There are plenty of reasons you might want to do this, from turning functionality on or off within your module – or like in my case – using the store code as a prefix on order numbers.

Magento: Display the store telephone number

Mar 13, 2011

Displaying the store telephone number that you entered into the Magento Admin seems like it should be obvious but it took me ages to track down, here is how. In a template file you add the telephone number by calling getStoreConfig like this… <?php echo Mage::getStoreConfig('general/store_information/phone'); ?> But from within the CMS, it is much simpler – there is actually a link to insert it somewhere in the menu that

Magento: fixing a broken pear.ini after migrating server

Mar 10, 2011

When you copy files from one server to another the pear.ini file contains some hardcoded paths that need to be fixed. The file format is some kind of PHP serialisation. The file can be found in downloader/pearlib and you need to change ther paths manually, as well as the prefix numbers that indicate the lengths of the string. e.g. s:9:”some/path” would become s:14:”someother/path”.

Custom translations file in Magento

Feb 25, 2011

It is very helpful to create a single translation file for your magento template to include any words or phrases that you add to the basic templates. Simply create an empty module (create a folder under app/code/local/[yourcompany]/[yourmodule]) and then add a subfolder etc and create a config.xml which should contain the following. <?xml version="1.0" encoding="utf-8"?> <config> <modules> <translations> <version>1.0.0</version> </translations> </modules> <frontend> <translate> <modules> <translations> <files> <default>swn_translations.csv</default> </files> </translations> </modules> </translate> </frontend> <adminhtml> <translate> <modules> <translations> <files> <default>swn_translations.csv</default> </files> </translations> </modules> </translate> </adminhtml> </config> Make sure your module is enabled (see [Creating your own module][1]).

Magento Multi Language, Currency & Region Setup

Feb 22, 2011

Several options here depending on exactly what you need. Currency can be handled at website, Store or Store level and if multiple currencies are set “as allowed” then the currency changer will show up. By default this appears in the left sidebar and is added in using the directory layout file (why directory?). To move this to the header, comment out the directory bit and move it to the page layout file – inside the header block.

Read URL parameters inside Magento (for tracking)

Feb 19, 2011

You likely want to be able to pass some tracking information about the visitors to your Magento site through to your back-end systems. I usually implement tracking using the same method as Google Analytics, just for convenience. Also, there is a ready-made [URL Builder][1] for clients non-google campaigns. This sends tracking data through to the site but you need a custom module to read those parameters and store them in a cookie.

Suppress the Magento newsletter (un)subscribe emails

Feb 15, 2011

This is vital since there is no way to test email templates. To test I recommend unsubscribing all subscribers, testing and then re-subscribing them. Painful, but at least with this fix you can stop everyone getting emails. Edit the file app/code/core/Mage/Newsletter/Model/Subscriber.php and comment out any line with ->send in it.

How to load stock levels into Magento from a CSV file

Feb 9, 2011

Create a module and add a method that you call periodically and throw the following code in. To get started [create your own magento module][1] and then you can use the code below to read a CSV file and set properties on the magento products. // prepare to load products $product = Mage::getModel('catalog/product'); // read CSV file and load each product and update quantity $row = 1; if (($handle = fopen("stockfile.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $sku = $data[0]; $qty = (int)$data[1]; // try and load the SKU $p = $product->loadByAttribute('sku',$sku); if ($p) { // get product's stock data such quantity, in_stock etc $productId = $p->getIdBySku($sku); $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId); $stockItemId = $stockItem->getId(); $stock = array(); // then set product's stock data to update if (!$stockItemId) { $stockItem->setData('product_id', $product->getId()); $stockItem->setData('stock_id', 1); } else { $stock = $stockItem->getData(); } $stockItem->setData('qty', $qty); if ($qty > 0) { $stockItem->setData('is_in_stock', 1); } else { $stockItem->setData('is_in_stock', 0); } $stockItem->setData('manage_stock', 1); $stockItem->setData('use_config_manage_stock', 1); // call save() method to save your product with updated data try{ $stockItem->save(); $product->save($p); } catch (Exception $ex) { // handle the error here!!

Creating your own Magento module

Feb 6, 2011

Create a folder here /app/code/local/[organization_name]/[module_name] You need an etc folder with config.xml in it containing something like the below. In this case it is expecting at least a Model folder with a php file (with any name) that defines a class with a matching name plus the name of the file it is in. <?xml version="1.0"?> <config> <global> <models> <stockupdate> <class>Swn_StockUpdate_Model</class> </stockupdate> </models> </global> </config> This would match a class in a file Model/Updater.php with the following class.