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"?>
<config>
 <modules>
  <Satcol_Clickandcollect>
   <version>0.0.1</version>
  </Satcol_Clickandcollect>
 </modules>
 <global>
  <models>
   <satcolclickandcollect>
    <class>Clickandcollect_Model</class>
   </satcolclickandcollect>
  </models>
  <events>
   <checkout_controller_onepage_save_shipping_method>
    <observers>
     <sales_order_place_after>
      <type>singleton</type>
      <class>Satcol_Clickandcollect_Model_Observer</class>
      <method>satcoltest</method>
     </sales_order_place_after>
    </observers>
   </checkout_controller_onepage_save_shipping_method>
  </events>
 </global>
</config>

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

<?php
  class Satcol_Clickandcollect_Model_Observer {
    public function satcoltest($observer) {
      $quote = Mage::getSingleton('checkout/session')->getQuote();
      $shippingAddress = $quote->getShippingAddress();
      $shippingMethod = $shippingAddress->getShippingMethod();
      if($shippingMethod=='freeshipping_freeshipping'){
        $address1 = $shippingAddress->getStreet(1);
        $shippingAddress->setStreet(array('Collect From Your Shop','66-78 Denington Road'))
          ->setCity('Your City')
          ->setRegion('Your County')
          ->setPostcode('Your postcode')
          ->save();
        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"?>
<config>
 <modules>
   <Satcol_Clickandcollect>
     <codePool>local</codePool>
     <active>true</active>
   </Satcol_Clickandcollect>
 </modules>
</config>

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”.

Synology and Drush 8.1.16 – Finally Working

Yep, it’s Tech Monday on a Wednesday again…

Okay, so I’m lumbered with these Fisher Price “My First Server” things.  They run Linux (Yay!) but a strangely butchered and hobbled version (Boo!) and as I need them to run Drupal, I need to get some things working properly.  And, of course, they don’t have APT or RPM on there, that would be too easy.  And by “things”, I mean Drush – essential for command line maintenance of all your Drupal sites.

Apologies for the technical nature of this post, I didn’t have time to paint it or build it to scale.

First thing needed is php7.  Synology has a package for php7, so that’s not a big problem.  It’s currently stuck at 7.0 but I expect we’ll see 7.1 and 2 soon (for a certain definition of “soon”, it took them long enough to get MariaDB 10 on there).  Install that package and you’re good to go forward.  Once the package is installed, nip into the configuration website and enable ALL THE EXTENSIONS EVER.

Drush itself.  First off, I had to create my own home directory as somewhere to put things that stay vaguely safe between DSM updates.  Weirdly, my Synology boxes didn’t create home directories for users (you know, the way normal operating systems do), so there were chunks of things always throwing errors.   Do this for your username…

sudo mkdir /volume1/homes/<username>

Sorted.  Golden.  Etc.  You’ll probably need your root password to do this, then you’ll need to chown it to your user before you can use it properly…

cd into that directory, head over to http://docs.drush.org/en/8.x/install/ and grab the latest 8 release from GitHub.  At time of writing, it’s 8.1.16

Okay.  This is where it gets technical.  More technical, sort of.

Sure, you could just run

php70 ~/drush.phar pm-update

But (a) it’s tedious to type that in all the time and (b) it’ll fail because pm-update calls php pm-updatestatus and that’ll fail because php is not the same as php70 and doesn’t have all the extensions installed.

So we need to perform some surgery in /bin.

Step 1.  Get /bin/php to point to the *right* php.

sudo su
<password>
cd /bin
mv php php56
which php70
(assuming /usr/local/bin/php70, if not then substitute appropriately)
ln -s /usr/local/bin/php70 php
exit
php --version

Now when you run php –version you’ll see a lovely, shiny, 7.0 there instead of the dull, old, tedious 5.6.

Step 2, get Drush in place.

sudo su
cd /bin
cp /volume1/homes/<username>/drush.phar drush
chmod a+x drush
exit

Now you can run drush from anywhere on the server, without having to specify a version of php or a location for the drush.phar file.  Ain’t that a treat!

And if you’re being clever, you’ll see that you could have both drush 8 and 9 installed at the same time – just have different filenames in /bin – you could have “Doctor” running drush 8 and “Master” running 9.  Jeeves and Wooster.  Armstrong and Miller.  Bonnie and Clyde.  Any pairing you like, really.  Or you could be boring and have “drush8” and “drush9”.  Up to you, really.

Word of warning 1 – If you do this to /bin/php it will alter your default command line php for everything.  Shouldn’t be a problem, in fact it will probably make life easier.  But worth bearing in mind if you suddenly start getting weirdness with scripts you’re triggering through cron jobs (and don’t get me started on Synology’s implementation of cron, that makes no sense).

Word of warning 2 – There will come a day when, totally unexpectedly, you’ll type “drush” and your Synology box will reply with “What the f*ck is drush?” Or the Linux equivalent, usually a bit more polite – imagine Jeeves stepping smartly yet unobtrusively forward, coughing politely, whispering “I’m terribly sorry, sir, but I can’t find that command”, then stepping back into the butler’s pantry.  This means your Synology box has updated the Disk Station Manager software and reset ALL of your configuration tweaks.  Go back into php.ini and reset that max_upload_size from 32M again.  Fix those timeout values.  And redo steps 1 and 2, above, assuming you’re lucky enough to still have a home directory with drush.phar in it.

If this has helped you, good.  And I hope you find a way to get to a real server sooner rather than later!

Harira – Moroccan Chicken Soup

There are about as many variations on this recipe as there are families in Morocco, or so I’m told.  This one came to me through a book on world street food and takes a little preparation – but some of that can be short-cutted if you’re in a rush.

It’s rich, hearty, winter-warming, and I can’t make less than a small vat of the stuff.

Serves a family of 7 comfortably.  If you want to make more, use the quantities in brackets to serve a churchful of hungry lent-lunchers and still have enough left over to feed the family that night and over the weekend to come.  Seriously, I can’t seem to make a small quantity of this stuff.

Quick spin round the ingredients, Clive, then back to me.

  • 1 (2) Medium chicken (1.5kg ish)
  • 2 (8) tbsp butter
  • 2 (8) tbsp olive oil
  • 2 (6-7) large onions, sliced
  • 6 (all the garlic in the house) cloves garlic, finely chopped
  • Salt and ground black pepper to taste
  • 1tsp (4) ground turmeric
  • 1tsp (4) ground cinnamon
  • 2 (8) large, ripe tomatoes, diced
  • 1 (4) cup dried chickpeas
  • 1 (2-3) cup short-grained rice
  • 1/2 (2) cup plain flour
  • Parsley
  • 2 (6) eggs
  • Lemons

The night before you want to make the soup…

Pressure cook the chicken.  Put in the pressure cooker with enough water to cover, add a carrot (broken into chunks), a quartered onion (skin still on), 2-3 tsp salt and a dozen or so whole black peppercorns.  I like to sling in a teaspoon or so of ground turmeric at this point.  20 minutes on high pressure, allow to cool.  Strain off the stock and keep it, separate the meat from the bones – shred the meat and save that, all the squidgy bits of carcass (including the soft, pressure-cooked bones) can go to the dog.  He’s now your bestest friend in the whole world EVER.

You can shortcut this by buying cooked chicken and using chicken stock you’ve already got, but if you make your own stock you can control the flavours so much more.

Dissolve the 1/2 cup plain flour in a cup of water and leave it to stand overnight.  Not found a way to shortcut this one.  Answers in the comments below, please!

Put the chickpeas in a bowl and cover with water until they’re about 3-4cm under.  They’re going to absorb the water and expand, so check on them and make sure they’re still covered at some point.  You can bypass this by using 2 tins of chickpeas as they’re pre-soaked for your convenience.

The Main Method

Big, deep, pan.  We’re talking stock pot, jam pan, that sort of thing.  Well, we are if you’ve quadrupled the ingredients.  You can probably get away with something smaller, but not a lot.  Heat the butter and oil, fry the sliced onions and garlic until soft and translucent.  Add the turmeric, cinnamon, salt, pepper, and diced tomatoes.  Simmer this until it reduces to a gorgeous thick sauce.  Smells fantastic at this stage.  Add the drained chickpeas, rice, chicken stock.  Simmer until the chickpeas are cooked through.  This takes about an hour, less if you’re using pre-soaked chickpeas.

If it starts to stick and looks really gloopy, add more stock.  The final consistency is pretty thick and sticky, but you want to serve it by the bowl, not by the slice.  It’s amazing how much liquid the rice soaks up, so just keep adding a cup or 2 of stock as required.  Each time you add stock, check the seasoning of the mix.  Chicken is remarkably bland, it’s amazing what a pinch of salt and a grate of pepper will do.

2 man job, this stage.  One to stir, one to pour.  First, pour in the flour and water mix into the soup in a thin, steady, stream.  The second person at this stage is to keep stirring to ensure it’s thoroughly mixed as it’s added in.  If you can persuade them to keep stirring for the next 15 minutes, go for it.  Otherwise, dismiss them for now, but let them know they’ll be needed again in a quarter of an hour.  Stir frequently throughout the next 15 minutes.

Add the shredded chicken meat back in, mix thoroughly, give it a couple of minutes then take it off the heat.  Chop the parsley, add and mix.  Finally, beat the egg and get your assistant back.  Just as with the flour, pour the beaten eggs into the soup, stirring all the while.

Last, but not least, juice the lemons and stir the juice in.

Serve with chunks of fresh bread.

You can adjust and adapt this recipe with whatever you happen to have on hand.  Got a load a lovely, fresh, chillies?  Add them in!