Php

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.

Padding a number with zero in PHP

Mar 17, 2011

This really should be simpler than it is. Well, that isn’t entirely true because it really is simple – but it took me at least three google searches to find out how to do what I wanted, so I thought it was worth sharing. I first tried using str_pad and got nowhere but then found an example using sprintf. The following code will take a number ($raw) and prefix it with two zeros.

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”.

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.

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!!

Run a magento module method periodically

Feb 7, 2011

You often want to be able to just write some PHP against your magento system to export, or update bits of data. In the module config file, under the etc folder you need to add a crontab element as a child of the main config element. <crontab> <jobs> <stockupdate_updater> <schedule><cron_expr>*/30 * * * *</cron_expr></schedule> <run><model>stockupdate/updater::import_stock</model></run> </stockupdate_updater> </jobs> </crontab>

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.

Magento 1.4 Checklist/Cheatsheet

Feb 3, 2011

I’m starting to work on more and more Magento websites and there are so many little gotchas and configuration settings I thought I would start compiling a list of things to make sure I check while I do the development. I’ll keep adding to this as I fall victim to the “features” of Magento, if anyone else has any pointers then please comment below! Newsletter Bug Creating your own module

Magento 1.4.2 Newsletter Bug

Feb 3, 2011

Magento newsletters templates don’t work. When you Queue the newsletter the body of the email isn’t brought through from the template. To fix this you need to copy the following your file into your local code and make a small change. Copy… /app/code/local/Mage/Adminhtml/Block/Newsletter/Queue/Edit To… /app/code/local/Mage/Adminhtml/Block/Newsletter/Queue/Edit And then change the code that sets the value of the editor field for the newsletter message to get the template text and not the template text preprocessed.