Creating a simple magento plugin: Add to cart from link

In this post I’m going to show you how to make a simple magento module that allows you to add products to your cart from a URL link. This feature does not exist by default in magento so we will make it ourselves. Creating a magento plugin can be overwhelming if you’re new to the platform. It’s actually pretty simple once you understand the folder structure, required files, and syntax. Magento has been geniusly designed in a way that you can override or extend any function based on folder structure and by declaring the override or extension with a simple .xml files.

For the first step we are going to create the folder structure. If you’re familiar with installing modules in magento then this should be familiar to you. The folder we’re going to create is:


The app directory is where the core magento files are located. The code directory is where the underlying and functional code resides. The local directory is where you place modules you create. The CM directory is the namespace, you can name this whatever you want. The Addtolink directory is the module’s directory. When creating your namespace and module directories it’s best to start with a capital letter to avoid any issues with magento.

Within that directory we’re going to create 2 directories, controllers and etc:


These are the 2 main module directories, there is one more to be made but we will get to that towards the end. We use the controllers directory because we will be extending a function in the app\code\core\Mage\Core\Controller\Front\ directory which is a core controller function.

Next we’re going to create the following PHP file and name it IndexController.php within the controllers directory:

class shiptronix_addtocart_IndexController extends Mage_Core_Controller_Front_Action{
     public function indexAction(){
          $product = $this->getRequest()->getParam('product');
          $key = Mage::getSingleton('core/session')->getFormKey();;
          if (empty($product)){ //if no product id, redirect to homepage
          else{ //redirect to the add to cart with the form key
               $this->_redirect('checkout/cart/add', array('product'=>$product, 'form_key'=>$key));

This is the main part of this module. This extends Magento’s core function by allowing you to add a string to the end of a URL that adds a certain product to cart. Now that we have created the actual code we’re going to create the config.xml in the app\code\local\CM\Addtolink\etc\ directory. Within that newly created file you’ll place this code:


The config.xml file tells magento how to load our module. You’ll notice that the config file uses simple xml. Pay attention to the way we reference our module’s functionality. What we’re first doing is declaring a module version then telling magento that when routing URLs we want it to use our module. Also note that in magento CM_Addtolink is the same as the directory app\code\local\CM\Addtolink\ and that is how magento knows to check that directory for our code.

We now have the module mostly created. The last step is to create our module’s second .xml file in app\etc\modules. I’m going to call this file CM_Addtolink.xml. In this file we will be declaring the module to magento so that it knows it needs to load this module. Here’s the code:


The CM_Addtolink.xml file declares the module and then the codepool which is app/code/local. Then the file declares that it is active using the word true.

Once the last XML is created you should have the following files in your modules directory:


You are now ready to install the module! If you haven’t installed a module before you can find many guides online. You simply copy your app folder via ftp to your magento’s root folder. Make sure you clear your cache after adding the module.

Once the module is installed you will need to know the product ID of the product you want to add to cart then simply use the following link pattern to add that product to cart from a link:

Hopefully this helped you understand how to make a simple magento module. Credit to Marius from this stackexchange thread for the actual module.


Chris Mallory