DownloadPlus for Magento Store Developers

DownloadPlus offers events that allow Store Developers to interact with the Download Processes.

Download Process (before)

This event is called before a download is being processed. Using this event allows to interact before the download is processed by Magento. Or to process the download by your own code, allowing to override the Magento Core Download Processor.

Event

downloadplus_download_process_before

Description

Is triggered before the requested download resource is passed to Mage/Downloadable/controller/DownloadController→_processDownload().

Models passed to Observer

session - Current Customer Session object

resource_path - The resource path to be downloaded, this might be a URL or file with full path as defined for the download

resource_type - The type of the resource (Mage_Downloadable_Helper_Download→LINK_TYPE_URL, Mage_Downloadable_Helper_Download→LINK_TYPE_FILE)

resource_object - The resource model, instance of Mage_Downloadable_Model_Link_Purchased_Item or Mage_Downloadable_Model_Sample

override_core_download - Set this to true in case you want to prevent the Core Downloadable Controller to process the download

redirect_url - Set this to a Shop URL to redirect to if override_core_download is set to true


Code Example

To add your own Observer to this DownloadPlus Event, you may use the following code examples as guideline:

  1. Create a Customer Extension for your store and select a namespace for it: e.g. 'Custom_Storefunctions'
  2. Create a Extension Directory for your custome storefunctions: app/code/local/Custom/Storefunctions
  3. Create the configuration XML files and Observer Model in the locations as shown now:



This configuration XML file tells your Magento Store that there is a Extension to be found:

Your Extensions [app/etc/modules/Custom_Storefunctions.xml]

<config>
    <modules>
        <Custom_Storefunctions>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Pisc_Downloadplus />
            </depends>
            <version>0.1.0</version>
        </Custom_Storefunctions>
    </modules>
</config>


This configuration XML file defines the Observers your Custom_Storefunctions extension is adding:

Your Extensions [app/code/local/Custom/Storefunctions/etc/config.xml]

<global>
   <events>
      <downloadplus_download_process_before>
         <observers>
            <custom_storefunctions_event_download_process_before>
               <type>singleton</type>
               <class>Custom_Storefunctions_Model_Observer</class>
               <method>eventDownloadProcessBefore</method>
            </custom_storefunctions_event_download_process_before>
	 </observers>
      </downloadplus_download_process_before>
   </events>
</global>


This Observer Model is now being called by the above defined Observer and here you may create your functions now. You may add any more Observer for other DownloadPlus Events, Magento Core Events or Events of other Extensions here also:

Your Extensions [app/code/local/Custom/Storefunctions/Model/Observer.php]

class Custom_Storefunctions_Model_Observer
{
 
  /*
   * Event before processing download
   */
  public function eventDownloadProcessBefore($observer)
  {
  	$download = $observer->getEvent()->getDownload();
 
  	// Get the download objects
  	$session = $download->getSession();
  	$resourcePath = $download->getResourcePath();
  	$resourceType = $download->getResourceType();
  	$resourceObject = $download->getResourceObject();
 
  	// Do your own stuff with it here...
 
  	// Set Flag to for overriding the core download processor
  	$download->setData('override_core_download', true);
 
  	// Redirect to shop "home"
  	$download->setData('redirect_url', 'home');
  }
 
}



Order Save (after)

A new Event is introduced by DownloadPlus allowing to interact when a new order is saved. This event may get used to create order specific data, like a serialnumber, by using the DownloadPlus Models or Magento functions.

Event

downloadplus_order_save_after_downloadable_create_serialnumber

Description

Is triggered for all Order Items that are Downloadable Products after the related order has been saved. See also the Example Observer in app/code/local/Pisc/Downloadplus/Model/Observer/Serialnumber.php for a example observer adding a Serial Number to the Downloadable Product order item.

Models passed to Observer

order - Current Order object

order_item - Current Order Item

Code Example

To add your own Observer to this DownloadPlus Event, you may use the following code examples as guideline. See the above code examples for the general approach and the initial preparation of structure.

Your Extensions [config.xml]

<global>
   <events>
      <downloadplus_order_save_after_downloadable_create_serialnumber>
         <observers>
            <yourobserver_event_create_serialnumber>
               <type>singleton</type>
               <class>Your_Extension_Model_Observer</class>
               <method>eventDownloadableCreateSerialnumber</method>
            </yourobserver_event_create_serialnumber>
	 </observers>
      </downloadplus_order_save_after_downloadable_create_serialnumber>
   </events>
</global>


Example Observer class

/*
 * See also 'app/code/local/Pisc/Downloadplus/Observer/Serialnumber.php' for this code example
 */
class Pisc_Downloadplus_Model_Observer_Serialnumber
{
 
	/*
	 * Returns true if in Admin Session
	 */
	public function isAdminSession()
	{
	  $session = Mage::getSingleton('admin/session');
	  if ($session) {
	    return $session->isLoggedIn();
	  }
	  return false;
	}
 
	/*
	 * Returns Session model
	 */
	protected function getSession()
	{
	  return Mage::getSingleton('core/session');
	}
 
	/*
	 * EXAMPLE OBSERVER:
	 * Event when processing orders for creating serial numbers
	 */
	public function eventDownloadableCreateSerialnumber($observer)
	{
	  $order = $observer->getEvent()->getOrder();
	  $orderItem = $observer->getEvent()->getOrderItem();
 
	  // Check if this order number has a serial number already
	  if ($orderItem->getId()) {
	    $serial = Mage::getModel('downloadplus/link_purchased_item_serialnumber');
	    $serial->load($orderItem->getId(), 'order_item_id');
	    if (is_null($serial->getId())) {
	      $serial->setOrderItemId($orderItem->getId());
	      $serial->setSerialTitle(Mage::helper('downloadplus')->__('Serialnumber for ').$orderItem->getName());
 
	      // Set the Serialnumber here
	      $serial->setSerialNumber('');
 
	      // Save the Serialnumber, its Status is being updated with the Order updates
	      $serial->save();
	    }
	  }
	}
 
}