Magento: Loading the product from a template

Wednesday, November 24. 2010
Comments

When I wrangle with Magento Commerce and customize anything, every other bit is of course tied to a product's ID, or sometimes entity ID.

The challenging part is that whenever you're in a template in Magento, the scope is very different from the previous one. This is sometimes frustrating, but when you think of it — it makes sense. That is, in Magento! ;-)

Magento works in blocks and each block is basically a class file, of course $this is never the same. So for example the scope of a block that renders a product page is different from something like Mage_Sales_Block_Order_Item_Renderer_Default (used to display a row of an order).

Code

I recently had to customize the display of an item in an order in the customer's account.

To do so, I had a nifty helper which is tied to a product's ID. Unfortunately, I was only able to retrieve the SKU, quantity ordered and all sorts of other things right away — but the product's ID was not available.

So how do you load a product otherwise? Simple! Using the SKU!

// app/design/frontend/default/my-template/template/sales/order/items/renderer/default.phtml
$_sku     = $this->getItem()->getSku();
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_sku);
$_product->getEntityId(); // here is your ID

So yeah — Mage_Catalog_Model_Product = very helpful. And of course there are a bunch of other attributes to load products by. Well, just about any attribute. Just dig around the docs, or var_dump() a product object on a product page to see what else is available.

Fin

Quick and dirty — I just blogged this because it took me 25 minutes to find that model and the loadByAttribute() method.

And hopefully by blogging this, I'll never ever forget.

Magento: moving a store to another server

Tuesday, June 16. 2009
Comments

Frequently, you do client work and if you are fortunate enough, you can setup a development environment on your own server or your laptop (or whatever), tinker with the files, and templates, and so on — until it's all done.

And whenever you are done, it's time to move files.

Sounds easy? It sort of is!

Checklist

Here's a small check list of things to keep in mind when you move an installation.

  1. You may need to fix up the configuration file in order to adjust all database related settings. It's located in app/etc/local.xml.

  2. Certain directories will need to be made writable. Writable in this case means that the webserver has to be able to write into it. Since most of the standard PHP setups are with mod_php, this will be the user www, apache or www-run (in most cases). If you (or your provider) runs php-cgi, chances are that this is not necessary.

    You may have to ask an administrator to set this up. If the administrator is not available, you can always chmod 777 these, but for obvious reasons this may be risky in a shared environment.

    The following is a list of directories (and contents) which need to be made writable:

    • app/etc/
    • var/
    • media/

    And in case you want to use MagentoConnect, there are additional directories to make writable. Please see this blog post for more info.

  3. URLs, URLs, URLs. You were working on localhost all this time and now you think you can move it over just like that? Wrong!!! ;-) But all you need to do is edit the following keys in the core_config_data table:

    • web/unsecure/base_url
    • web/secure/base_url

That's all folks!

If you have any additions, please leave a comment!

Magento: Error: Please check for sufficient write file permissions

Thursday, April 30. 2009
Comments

Moving a store from one server to another server, you're bound to have small issues. While 99% of the configuration is stored in app/etc/local, there are other bits and pieces that are more spread out. For example, I noticed that there's no central 'Are all file permissions OK?' screen in Magento Commerce and at times the system is pretty secretive when it comes to error messages.

For example, after moving a store from a linux to a BSD (essentially a different WWW user id), Magento Connect just died with "Error: Please check for sufficient write file permissions".

Of course it did not disclose what exactly it failed in the process. Anyhow — debugging in downloader/Maged/Controller.php (Maged_Controller::isWritable()), here's a list of files and directories that need to be made writable so Magento Connect gets to work:

/path/to/your/magento-install

/path/to/your/magento-install/downloader

/path/to/your/magento-install/downloader/config.ini

/path/to/your/magento-install/downloader/pearlib/config.ini

/path/to/your/magento-install/downloader/pearlib/pear.ini

/path/to/your/magento-install/downloader/pearlib/php

Who knows — maybe the secrecy is for security reasons or whatever. In the end the list above is incredibly helpful, when you realized that the linked entry from the knowledge base is missing half of them and/or does not list the relevant parts for Magento Connect there. :D

How to setup multiple stores on different domains with Magento

Wednesday, April 22. 2009
Comments

Multiple stores is probably the killer feature of the Magento Commerce store. It enables the needy to manage multiple stores through a single interface. Your very own mall in a box.

It's also a management/deployment nightmaredream come true. A single piece of software powering multiple websites.

This small HowTo requires a working installation of Magento (tested with Magento Commerce v1.3), multiple stores setup already, an understanding of your (web)server, some shell-fu and probably root access.

I won't go into creating virtual hosts, and if you're stuck with CPanel, Plesk or similar, this may work as well but I can't help you very much.

Making it work

Magento is complex and so on, but let's disregard the caveats for now and figure out how to make it run.

This setup is one of the things that took me a good while to research, so I'm sharing this so it jump starts others. I remember I had a wiki or knowledge base entry over at Magento bookmarked, but that one seemed to have vanished.

My file system layout is as follows:

/web/customer/magento/1.3/

/web/customer/magento/1.0/

/web/customer/example.org/htdocs

/web/customer/example.net/htdocs

Now, let's say you have a store setup for example.org and one setup for example.net, then place an index.php with the following contents in each htdocs directory:

<?php
require_once '/web/customer/magento/1.3/app/Mage.php';
Mage::run('STORE-CODE', 'store');

The STORE-CODE is hidden in your Magento Commerce Admin. Go to System > Configuration and then click on the designated Store View.

Last, but not least — add the following symbolic links inside both htdocs directories:

ln -s ../../magento/1.3/app/ ./app
ln -s ../../magento/1.3/downloader ./downloader
ln -s ../../magento/1.3/js/ ./js
ln -s ../../magento/1.3/media/ ./media
ln -s ../../magento/1.3/skin/ ./skin
ln -s ../../magento/1.3/var/ ./var

Of course you may need to adjust the paths around, but aside from that, it's all straight forward.

And that is all!

Update: Updated instructions for symlinking (added downloader).