Module

Using Layout Update XML in Magento

Jun 19, 2011

The Magento Designer’s Guide has the following short intro about layout XML and I think it explains it quite well, although I will go through some more detail with examples afterwards. Layout is a virtual component of the Magento application. By modifying the components of layout, you can build your store page in an upgrade-compatible way. Layout is comprised of default layout and layout updates that are made up of easy-to-learn XML tags.

Magento: Setting columns and limiting products shown in grid

Apr 15, 2011

I recently started experimenting with creating my own page templates and using the layout update XML to add content into the page. This works well for more complicated page designs and is particularly suitable for the homepage or landing pages. I did hit a minor snag however and that was when I added a product list or grid I couldn’t see how to specify the number of columns the grid should contain.

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.

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.