Background
The site I'm working on sells webcasts that have downloadable products (books) associated with them. Users can buy the book, or they can buy the webcast and get the book for free.
The books themselves are manuals, and whenever an update is made to the book users that have made a recent purchase (1 year +1 month) need to be notified of the update so they can re-download the book. This is working.
The Issue
The way the downloadable products are updated is that a new link is added. The base file itself never changes, instead new files are added that include instructions for people collating the book on how to assemble it (this is because they are a green company and they don't want people printing off these 1000 page manuals completely every time they add a single page). If a new revision is made and users are notified, they do not have a link to download the changes because it did not exist when they initially placed the order. All they see are the links to the files that existed when they initially placed the order because that's what exists in the downloadable_link_purchased_item
table.
What I need is a way to allow the customer to download -all- revisions (please read: all available links) of a downloadable product given that they have purchased the product.
I have tried $productModel->getLinks()
and $productModel->getDownloadableLinks()
, but both return null:
$purchased = Mage::getResourceModel('downloadable/link_purchased_collection')
->addFieldToFilter('customer_id', $session->getCustomerId())
->addOrder('created_at', 'desc');
if(count($purchased)) {
$this->setPurchased($purchased);
$purchasedIds = array();
foreach ($purchased as $_item) {
$purchasedIds[] = $_item->getId();
$order = Mage::getModel('sales/order')->load($_item->getOrderId());
$items = $order->getAllItems();
foreach($items as $item) {
if('downloadable' == $item->getProductType()) {
$product = Mage::getModel('catalog/product')->load($item->getProductId());
echo('Links: <pre>');
var_dump($product->getLinks());
echo('</pre>');
die;
}
}
}
}
I also thought that maybe I could add a new entry into downloadable_link_purchased_item
table for each customer who has purchased the updated book whenever an update is made, and generate a new link hash for each entry. However, I don't think this would really be a good idea (they didn't actually buy it at that time, I would have to generate my own link_hash which I don't really feel comfortable in doing, and really I'd just like to give them access without messing with the data).
I've been stuck for half a day trying to figure out how to tackle this, and would greatly appreciate any help or pointing/prodding in the correct/not-so-wrong direction. I don't have much experience with downloadable products or how their URLs work. Thanks in advance.
Update
I tried going the route of inserting links into the downloadable_link_purchased_item
table with the following code in an observer that fires whenever a product is updated through the admin:
$product = $observer->getProduct();
echo('<pre>');
if('downloadable' == $product->getTypeId()) {
foreach($_POST['downloadable']['link'] as $link) {
if('0' == $link['link_id']) {
// This is a new file being added. Insert a record into
// downloadable_link_purchased_item for each customer
// that has purchased this item.
$file = json_decode($link['file']);
$orderItems = Mage::getModel('sales/order_item')->getCollection()
->addFieldToFilter('product_id', $product->getId());
foreach($orderItems as $orderItem) {
$order = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('entity_id', $orderItem->getOrderId())
->getFirstItem();
$downloadPurchase = Mage::getModel('downloadable/link_purchased')
->setOrderId($orderItem->getOrderId())
->setOrderIncrementId($order->getIncrementId())
->setOrderItemId($orderItem->getId())
->setCreatedAt(date('Y-m-d H:i:s'))
->setUpdatedAt(date('Y-m-d H:i:s'))
->setCustomerId($order->getCustomerId())
->setProductName($product->getName())
->setProductSku($product->getSku())
->setLinkSectionTitle('Click here to download');
var_dump($downloadPurchase->save());
// I got this line from /app/code/core/Mage/Downloadable/sql/downloadable_setup/mysql4-upgrade-0.1.12-0.1.13.php
$linkHash = strtr(base64_encode(microtime() . $downloadPurchase->getId() . $orderItem->getId() . $product->getId()), '+/=', '-_,');
$downloadItem = Mage::getModel('downloadable/link_purchased_item')
->setProductId($product->getId())
->setNumberOfDownloadsBought(0)
->setNumberOfDownloadsUsed(0)
->setLinkTitle($link['title'])
->setIsShareable($link['is_shareable'])
->setLinkFile($file[0]->file)
->setLinkType('file')
->setStatus('available')
->setCreatedAt(date('Y-m-d H:i:s'))
->setUpdatedAt(date('Y-m-d H:i:s'))
->setLinkHash($linkHash)
->setOrderItemId($orderItem->getId())
->setPurchasedId($downloadPurchase->getId());
var_dump($downloadItem->save());
echo('Download Purchase ID: ' . $downloadPurchase->getId() . '<br>');
echo('Download Item ID: ' . $downloadItem->getId() . '<br>');
}
}
}
}
echo('</pre>');
die;
The issue now is that it acts like it is saving the models in the database (I get an ID back and everything for both models), but neither one is actually saving. I'm pretty sure the reason the $downloadItem
may not be saving is because I'm not specifying a link id (even though I'm using the event catalog_product_save_after
the link does not exist in the observer yet, still trying to figure that one out as well). However, I have no idea why the $downloadPurchase
model would not be saving.