如何更新magento中已下订单的新可下载产品附件

时间:2013-07-04 11:15:00

标签: magento download magento-1.7

我的magento商店里有可下载的商品。 我的可下载产品是付费软件工具。 我们根据每个版本的变化更新了这些可下载的软件工具,并增强了软件工具的新功能。

当客户完成每次成功的付款交易时,软件包即可下载。

我的问题是:

Version - 1:

客户将购买并使用Version - 1的{​​{1}}个包。

order id #101

当我们在管理面板中更新相应产品的Version - 2: 时。我们需要放置 Version - 2的{​​{1}}下载包,而不是Version - 2

magento CE 1.7.0.0中是否可以使用此选项?

现在订单ID#101只有版本-1包可供客户下载页面下载。

但我们需要为每个购买的订单打包文件吗?

请告知。

3 个答案:

答案 0 :(得分:6)

我将以下代码放入Magento根目录中的.php脚本中。您可以通过CRON或网络浏览器进行呼叫。

<?php

    require('app/Mage.php');
    umask(0);
    Mage::app();

    $select = getDb()->select()
        ->from(getTable('downloadable/link'), array('link_id', 'link_file', 'product_id'));

    if (($links = getDb()->fetchAll($select)) !== false) {
        foreach($links as $link) {
            $linkId = $link['link_id'];
            $productId = $link['product_id'];
            $file = $link['link_file'];

            $select = getDb()->select()
                ->from(getTable('downloadable/link_title'), 'title')
                ->where('link_id=?', $linkId)
                ->limit(1);

            $title = getDb()->fetchOne($select);

            echo sprintf("Updating %s to %s<br />", $file, $title);

            $cond = getDb()->quoteInto('product_id=?', $productId);

            getDb()->update(getTable('downloadable/link_purchased_item'), array('link_id' =>$linkId,'link_title'=>$title,'link_file'=>$file), $cond);
        }
    }

    function getDb($conn = 'core_read') {
        return Mage::getSingleton('core/resource')->getConnection($conn);
    }

    function getTable($table) {
        return Mage::getSingleton('core/resource')->getTableName($table);
    }

答案 1 :(得分:2)

虽然是一篇旧帖子,但接受了答案,我会发布另一个答案作为替代解决方案。

为解决我的问题,我创建了一个扩展程序,将过去的订单更新为产品可下载信息中的任何产品。

这包括:

  1. 只更新现有的可下载条目(从而更改链接标题和文件以供下载)
  2. 将任何新文件添加到可下载列表中(示例随着时间的推移添加补丁文件)
  3. 删除现有条目
  4. 任何过去的订单都包含您作为购买商品调整的产品,然后将其可下载的产品列表更新为您编辑的内容。

    可通过github:https://github.com/ProxiBlue/UpdateDownloadLinks

    获取

答案 2 :(得分:1)

proxiblue提供的解决方案可以,但不完整。

算法也有一个错误,它总是占用产品的所有链接并删除旧的链接。通过这种方式,链接始终会更新,并且下载统计信息将丢失。 要解决此问题,您应该添加以下代码:

$currentPurchasedItemsT = $linkPurchasedItems->getItems();
$currentPurchasedItems=array();
foreach( $currentPurchasedItemsT as $c){
    $currentPurchasedItems[$c["link_id"]] = $c;
}    

另一个不便之处是,即使付款与否,所有新订单都会添加到所有订单中。 默认情况下,付费链接会添加到所有订单中,如果客户没有付款,则不会发生这种情况。

$data = $newFile->getData();
if ($linkPurchased->getOrderItemId() && $data["price"]=="0.0000") {

完整的代码是:

class ProxiBlue_UpdateDownloadLinks_Model_Observer {
    public function catalog_product_save_after($observer) {
        $product = $observer->getEvent()->getProduct();
        $product = $observer->getProduct();
        return;
        if ($product->getTypeId() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) {
            // get all the purchased items that match the product
            $linkPurchasedItems = Mage::getModel('downloadable/link_purchased_item')->getCollection()
                            ->addFieldToFilter('product_id', $product->getId())->load();
            $currentPurchasedItemsT = $linkPurchasedItems->getItems();
            $currentPurchasedItems=array();
            foreach( $currentPurchasedItemsT as $c){
                $currentPurchasedItems[$c["link_id"]] = $c;
            }
        $files = $product->getTypeInstance(true)->getLinks($product);
        //build a list of purchase objects (orders) that were used to buy this product
        $productId = $product->getId();
        $collection = Mage::getResourceModel('sales/order_item_collection')
            ->addAttributeToFilter('product_id', array('eq' => $productId))
            ->load();
        $purchaseObjects = array();
        foreach($collection as $orderItem) {
            $purchaseObject = mage::getModel('downloadable/link_purchased')->load($orderItem->getOrderId(),'order_id');
            if($purchaseObject->getId()) {
                $purchaseObjects[$purchaseObject->getId()] = $purchaseObject;
            }    
        }
        //determine and add any new files to the orders that have the product

        $newFiles = array_diff_key($files, $currentPurchasedItems);


        foreach ($newFiles as $newFile) {
            //attach each new file to the purchase

            foreach ($purchaseObjects as $linkPurchased) {
                $data = $newFile->getData();

                if ($linkPurchased->getOrderItemId() && $data["price"]=="0.0000") {

                    $linkHash = strtr(base64_encode(microtime() . $linkPurchased->getId() . $linkPurchased->getOrderItemId()
                                    . $product->getId()), '+/=', '-_,');
                    $linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item');
                    $linkPurchasedItem->setData($newFile->getData());
                    $linkPurchasedItem->unsItemId();                        
                    $linkPurchasedItem->setPurchasedId($linkPurchased->getId())
                            ->setOrderItemId($linkPurchased->getOrderItemId())
                            ->setLinkHash($linkHash)
                            ->setLinkTitle($newFile->getTitle())
                            ->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE);
                            //->setUpdatedAt(now())
                    $linkPurchasedItem->save();
                }
            }
            //exit;
        }

        // determine what is no longer attached as files and remove from the download links
        $noLongerAttachedAsFiles = array_diff_key($currentPurchasedItems,$files);

        //var_dump(array_diff_key($currentPurchasedItems,$files));
        //echo "<br/><br/>";
        //exit;
        foreach ($noLongerAttachedAsFiles as $purchasedLink) {
            //$purchasedLink->delete();
        }

    }
}

}