3

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

当客户完成每笔成功的支付交易时,软件包就可以下载了。

我的问题是:

Version - 1:

客户将购买和Version - 1使用order id #101.

Version - 2:

当我们Version - 2在管理面板中更新相应产品时。我们需要 为id放置Version - 2下载包。order id #101Version - 1

这个选项在 magento CE 1.7.0.0 中是否可行?

现在订单 id #101 仅在客户可下载页面中下载版本 -1 包。

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

请指教。

4

3 回答 3

7

我将以下代码放入 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);
    }
于 2013-07-04T14:24:36.440 回答
2

虽然是一个旧帖子,有一个公认的答案,但我会发布另一个答案作为替代解决方案。

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

这包括:

  1. 只需更新现有的可下载条目(从而更改要下载的链接标题和文件)
  2. 将任何新文件添加到可下载列表(例如随着时间的推移添加补丁文件)
  3. 删除现有条目

任何将您正在调整的产品作为购买项目的过去订单都会将其可下载的产品列表更新为您编辑的任何内容。

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

于 2014-04-26T13:34:43.957 回答
1

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

该算法也有一个bug,它总是取产品的所有链接并删除旧的。这样,链接总是会更新,下载统计信息会丢失。要解决此问题,您应该添加以下代码:

$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();
        }

    }
}

}

于 2014-11-20T11:23:00.107 回答