Question

The store I am working on is for downloadable products only. During the checkout if we have a book of a particular attribute set we send a SOAP request to an external company (to get the ebook's downloadable URL from the external company). I have wrote a module which overrides the saveOrderAction of the OnepageController.php

I have created an extra database table which stores the information about the item's downloadable URL which is updated on the saveOrderAction.

As during the checkout the Order ID isn't generated I have created an Observer which checks for the order being saved event, during this it finds the Order ID of the current products which I update the database table I created.

The problem I'm having now is how to update the downloadable URLs when the user clicks download on their downloadable products section of their customer account.

In the DownloadController.php I have found the function linkAction which I believe is called when the download button is clicked for an product. In there I have print_r the $linkPurchasedItem variable which shows an purchased_id and item_id which appear to be unique for the product.

To return the downloadable URL which is saved in the database (on my custom table) and given from the external company I will need to update the database table I have created at some point to insert the 'purchased_id' or 'item_id' so in the DownloadController.php on the linkAction I can run another database call to get the right downloadable URL for the product this function is being called for.

Therefore my questions are:

  • At what point is the purchased_id and/or item_id generated?
  • Is it possible to get them in the OnepageController.php?
  • What is the best way to uniquely identify each product in each order in my custom database table so I can reference it in the DownloadController.php?

Thanks for any help in advance.

Was it helpful?

Solution

Let's take your main questions, in order:

1. At what point is the purchased_id and/or item_id generated?

Both purchased_id and item_id are generated within Mage_Downloadable_Model_Observer from the method saveDownloadableOrderItem. This observer is registered to listen for the event sales_order_item_save_commit_after - which is called every time an order item is saved and the db transaction commit has completed.

The specific line where this is set is ~105*:

$linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')
    ->setPurchasedId($linkPurchased->getId())
    ->setOrderItemId($orderItem->getId());

This happens just before it generates the hash for the download link. You'll see that it does not explicitly set the item_id - indeed, it isn't necessary because it is the autoincrement primary key for the table.

In summary, the ids are set after the order item is successfully saved to the database, meaning the order itself is also saved.

2. Is it possible to get them in the OnepageController.php?

Because the order item has to be saved in order to generate the purchased_id, the answer is no.

3. What is the best way to uniquely identify each product in each order in my custom database table so I can reference it in the DownloadController.php?

In much the same way as shown above in the model setter setOrderItemId, they used the sales order item's id to reference that particular product. You should also use this strategy. In order to inject this into your own table, rewrite Mage_Downloadable_Model_Observer to save to your own table in parallel to the save in the downloadable tables -- otherwise you'll have to copy the pattern set forth in that class, doing many checks to ensure that the product type id is Downloadable, etc.

Summary:

You're not going to be able to generate download links from your SOAP service until after the order is completed. Without a major refactor there is no way to access purchased_id in Onepage.php.


* at least in CE 1.8 Alpha / EE 1.13.0.1

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top