dtby67541 2012-12-04 21:43
浏览 36
已采纳

Magento为客户提供可下载产品

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.

  • 写回答

2条回答 默认 最新

  • dselp3944 2012-12-05 18:22
    关注

    Turns out my die statement was preventing the saving of the models' data. When I took that out, the link to the downloadable product was getting put in correctly, and customers who have purchased the downloadable product can instantly see the new revision from their account screen.

    I'm not sure why that would be the case, I would think that the moment I call save() would be when the database insertion would happen, but apparently that's not how the Lords of Magento view the world.

    I still have some issues as far as data integrity is concerned. Like I said above, the download_link entry is not created at the moment my observer is being called, but at least users can now see the links in their account.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助