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