Magento Free Shipping -> Click and Collect Observer

Recently encountered an issue with Magento and a basic implementation of a “Click and Collect” feature.

We’d set up a system using the “Free Shipping” option within Magento to act as a “Click and Collect” option, modifying the page template for the appropriate part of the one-page checkout to include the information about the click and collect service – where it would be available from, what the opening hours are, that sort of thing.  Now there are plugins that handle store collection but they all tend to focus on retailers with multiple stores, and we’ve just got the one.

When you go to print out the order, postage cost is £0.00 and it clearly says “click and collect” but the shipping address is still the one the customer entered during checkout and that was going to cause problems with the system we’ve got for picking, packing, and shipping orders.

So….  Enter the Observer.  Observers are cool.  They’re Magento’s equivalent of Drupal’s hooks, allowing you to watch what’s going on and dive in when something specific happens.

As things go in Magento, this is a relatively straightforward hack, only took me a couple of days to work out.

Step 1.  Create a module to do the work.  SSH onto your server and…1

cd <magento install dir>/app/code/local
mkdir Satcol
cd Satcol
mkdir Clickandcollect
cd Clickandcollect
mkdir etc
mkdir Model

‘course, you can do this via FTP as well.

You don’t have to call the folders “Satcol” and “Clickandcollect”, it just made sense for me to do that.  Remember what you did call them, though, you’ll need it later.

Inside the “etc” folder, create config.xml

<?xml version="1.0"?>

And if you’ve called your directory anything other than “Satcol” and “Clickandcollect” you need to change the lines where those words are found.

Under “events”, we’re specifying that we’re looking for the exact moment when Magento saves the shipping method – checkout_controller_onepage_save_shipping_method – catchy name, I’m sure you’ll agree, but it does exactly what it says on the tin.  If you’re not using the onepage checkout, then you’ll need to determine what the event you’re observing is called.

The “method” line is important for the next file as it’s the name of the php function you’re going to call in Observer.php

cd back to Model and create Observer.php

  class Satcol_Clickandcollect_Model_Observer {
    public function satcoltest($observer) {
      $quote = Mage::getSingleton('checkout/session')->getQuote();
      $shippingAddress = $quote->getShippingAddress();
      $shippingMethod = $shippingAddress->getShippingMethod();
        $address1 = $shippingAddress->getStreet(1);
        $shippingAddress->setStreet(array('Collect From Your Shop','66-78 Denington Road'))
          ->setCity('Your City')
          ->setRegion('Your County')
          ->setPostcode('Your postcode')
        Mage::log('Click and Collect order placed');

Obviously change the setStreet, setCity, setRegion, and setPostcode values as appropriate.  And the Mage::log is there for debug purposes, it can be removed – or left if you want to check the system log periodically and see how many orders have used this method.

And if you’ve used a different shipping method for your Click and Collect solution,  you’ll need to change the if statement that’s looking at shipping method.

Once these files are in place, you need to turn the module on.  In Drupal, this would be a simple tick box.  In WordPress, you’d enable the plugin in the lovely friendly GUI.  In Magento we’ve got to go to another location on the server and add in another xml file.

SSH to your server, and…

cd <magento install dir>/app/etc/modules
vi Satcol_Clickandcollect.xml

(Or whatever you called your directories back at the start).

<?xml version="1.0"?>

cd back to magento’s /var/cache and clear the cache (or flush it from the admin console) and you’re golden.  Now whenever someone places an order using the Free shipping method you’ve assigned to “Click and Collect” the delivery address will be automagically updated to the address you specify.

And if you know of a simpler, easier, better, way of doing this – please let me know.  Just as long as it isn’t “buy this plugin for only $79”.

Leave a Reply

Your email address will not be published. Required fields are marked *