Blog

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.

Barras Car Centre Website

Dec 9, 2010

The Barras Car Centre website required that they could upload details of new and used cars and display them, as well as provide a feed of the information for other third party sites. It seemed a good candidate for a simple Ruby on Rails website, which was up and running really quickly and to everyone’s satisfaction. Take a look at the screenshots below, or take a look at the Barras Car Centre website.

Carey’s Pipe & Tobacco Shop Magento/WordPress eCommerce Website

Oct 31, 2010

For this website the real key was enabling customers to engage via social media and to create a place where pipe smokers could discuss their hobby. Due in part to the way in which smoking in public is becoming less tolerated but also due to the banning of advertising through so many traditional channels the internet and especially social media was an avenue our clients were very keen to utilise. While they had run a success e-commerce website for a number of years they were not happy with their current site, or the technology it was built with and we were able to suggest Magento as a cutting-edge alternative.

Magento Order & Product Properties

Oct 7, 2010

I’m currently working on a module for a magento site that exports orders as they are made on the site. If anyone is trying to do the same then this list of properties may prove useful. I’m simply repeating it here because it took me so long to find and because magento themselves don’t seem to provide a reference and nor is it anywhere in the actual source code! Anyway