Magento, PHP and Linux Tips & Tricks

Magento memorized show all

Turns out Magento has a tendency to remember if you selected Show All on a list page for all subsequently visited list pages. This is not something we wanted. Turns out you can disable this functionality by disabling a single line of code.

You will probably want to override Mage_Catalog_Block_Product_List_Toolbar with your own module. In our case Amasty_Shopby_Block_Catalog_Product_List_Toolbar had already done that so I added it there.

Redis higher than 2.6 in Centos

Turns out Magento requires a Redis version higher than 2.6. The highest version in the CentOS repo or EPEL is 2.4 but the REMI repo has 2.8.

You can learn how to install it here:

To enable it you have to edit /etc/yum.repos.d/remi.repo and enable it:

Then you can install the updated redis version. You can optionally also disable the repo afterwards again just to make sure you don’t end up involuntarily updating many other packages (if you don’t want to..)

Magento + Varnish + Deadlocks

So I just spent the greater part of the evening fixing a problem with a client’s Magento installation which was causing deadlocks during checkout. We tried everything from reimporting a database dump to another mysql version, an upgrade to percona and even a whole other cloud server.

We focused on the database because of the deadlock and also saw a lock on a stock table.

Eventually it turned out to be the PageCache module. Even after disabling it in etc/modules was it still active. Only by disabling it via amin did it really turn off and all the deadlock problems disappeared…

I am seriously starting to dislike varnish,
this is not the first time we have had problems with it in combination with Magento.

Output Line Number PHP

I just happened to wonder if it was possible to output the line number of the current PHP file in debug output. Turns out it is, it’s a magic constant and it’s as easy as using as just doing the following:

You can find more pre-defined magic constants here:

Insert Magento Upsells Programatically

Here are two very handy functions which you can use to create upsells. You call insertUpsells with an array of product_ids and it will make sure all those products are upsells of each other. Just noticed the count could have been done a bit cleaner but it works..

Howto: Zend Framework Cron

Seems there’s still a lot of traffic coming to this old page, so I decided to re-add it. It’s from 2009. I had to fix the URL because I am not using that specific permalink structure anymore. I’m using an Nginx rewrite to forward to this new URL now.

Mind you I wrote this for ZF 1.x, I have no idea if this still works for 2.x but considering the amount of traffic it generates it could still work. If it DOES work, please leave a comment stating so :)

The post:
The trick with cronjobs is that you do not want to load the whole View part of ZF, we don’t need any kind of HTML output! To get this to work, I defined a new constant in the cronjob.php which I will check for in the index.php.

Your cronjob.php could look something like this:

And you’re have to change the index.php:

It’s as easy as that!
You’d be surprised how long it took me to find the answer to this simple problem, like many other opensource projects documentation for ZF has to sometimes be found in the community and a lot of that is outdated. This was just one of a few solutions but it works just fine for me and is pretty simple to implement so I decided to stick with this.

I hope this can help someone out

Delete all Magento products MySQL

You can quickly delete all Magento products using this collection of queries.
UPDATE: Added removal of reviews & ratings!

This works for me under

Added Varnish + Minify + Expire Headers

Just added Varnish to the config of this server and the blog should now be loading within 30ms :)
This is just a test post to see if cache is purged on new posts.

UPDATE: Works just fine! Now to minify css & js and add expire headers to all statics (if they aren’t set yet) in nginx.
UPDATE2: So the blog is fully minified and expire headers are being set. Varnish is running in front of it and if you’re lucky you will see response times of around 12ms :)

Magento admin on seperate server

Since mostly all the tutorials I read about this seemed to be wrong I figured it would be a good idea to write this down for future use.

First thing’s first, disable auto-redirects. Set the following setting to NO:

Now make sure Magento expects you to be hitting Admin on a different URL, otherwise it will STILL redirect you back.
Fill in whatever domain/subdomain you want to use here:

Setting both these things will stop Magento from redirecting you back to the (loadbalanced cluster of) front-end webserver(s).

Moved to DigitalOcean

So I just ditched my previous VPS at TransIP and setup my new one at DigitalOcean since they are cheaper, faster and still have a location in Amsterdam. For the same price as TransIP I also get a backup service and a really, really nice interface to handle my server. The disk bandwidth is insane, all the way up to 340MB/s compared to my old 70MB/s max.

So far I’m very happy! Note to self on setting up nginx though, check the fastcgi_params file to make sure it looks like the block below, otherwise it won’t work:

Specifically the SCRIPT_FILENAME, otherwise you will just get white pages.

1 2 4