問題描述
我正在使用 Magento 構(gòu)建一個(gè)電子書網(wǎng)站.對(duì)于該版本,我們計(jì)劃提供一些可免費(fèi)下載的書籍.我們希望可以使用普通的 Magento 'catalog' 功能來添加下面的產(chǎn)品類別.但是,由于這些產(chǎn)品是可免費(fèi)下載的產(chǎn)品,因此在用戶嘗試下載時(shí)通過結(jié)帳將其發(fā)送并沒有意義.
I am using Magento to build an eBooks site. For the release, we plan to have a number of free downloadable books. We were hoping that it would be possible to use the normal Magento 'catalog' functionality to add categories with products underneath. However, since these are free downloadable products, it doesn't really make sense to send users through the checkout when they try to download.
有誰知道一種方法來創(chuàng)建一個(gè)完全繞過結(jié)帳的免費(fèi)可下載產(chǎn)品?我注意到可下載產(chǎn)品有一個(gè)免費(fèi)樣品"選項(xiàng),但如果可以,我寧愿不使用它,因?yàn)槲矣?jì)劃在添加付費(fèi)產(chǎn)品時(shí)將此字段用于其預(yù)期目的.
Does anyone know of a way to create a free downloadable product which bypasses the checkout altogether? I have noticed that there is a 'free sample' option for downloadable products, but I would prefer not to use this if I can as I plan to use this field for its intended purpose when I add paid products.
我注意到你們中的一些人因?yàn)閱栴}不明確"而否決了這個(gè)問題.為了清楚起見,我想:
I have noticed that some of you have voted this question down for 'lack of question clarity'. For clarity, I want:
- 想知道是否有可能創(chuàng)建一個(gè)可下載的產(chǎn)品不需要用戶的 Magento通過通常的結(jié)帳過程(因?yàn)樗敲赓M(fèi)的)
- 和這不是免費(fèi)樣品"字段可下載產(chǎn)品的
不幸的是,我認(rèn)為我不能再雄辯地問這個(gè)問題了.[/編輯]
Unfortunately I don't think I can ask this any more eloquently. [/EDIT]
推薦答案
此代碼將允許已登錄的客戶下訂單"購(gòu)買免費(fèi)的虛擬產(chǎn)品,同時(shí)繞過結(jié)帳并將其直接重定向到我的下載"部分他們的帳戶.
This code will allow logged-in customers to "place an order" for a Free, Virtual product while bypassing checkout and redirect them straight to the My Downloads section of their account.
將以下行添加到您的 catalog/product/list.phtml
中所需的位置.
Add the following line to your catalog/product/list.phtml
in the spot you want it.
<?php if($_product->isVirtual() && $_product->getFinalPrice()==0) { ?>
<a href="/modulename/checkout/purchase/id/<?php echo $_product->getId()?>"><?php echo $this->__('Download and Install') ?></a>
<?php } ?>
然后使用包含以下代碼的 controllers/CheckoutController.php
創(chuàng)建一個(gè)新模塊:
Then create a new module with a controllers/CheckoutController.php
containing this code:
public function purchaseAction() {
if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
$this->_redirectUrl(Mage::getBaseUrl().'customer/account');
return;
}
$request = $this->getRequest();
$id = $request->getParam('id');
$product = Mage::getModel('catalog/product')
->load($id)
->setStoreId(Mage::app()->getStore()->getId());
if(!($product->getIsVirtual() && $product->getFinalPrice() == 0)){
Mage::getSingleton('checkout/session')->addError($this->__('Method only available for Free Downloadable Items'));
return $this;
}
$onepage = Mage::getSingleton('checkout/type_onepage');
/* @var $onepage Mage_Checkout_Model_Type_Onepage */
try{
$quote = $onepage->getQuote();
/* @var $quote Mage_Sales_Model_Quote */
$quote->addProduct($product);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$onepage->initCheckout();
$payment=array('method'=>'free');
$onepage->savePayment($payment);
$onepage->saveOrder();
$this->getResponse()->setRedirect('/downloadable/customer/products');
}
catch(Exception $e){
$result = $e->getMessage();
Mage::getSingleton('checkout/session')->addError($result);
}
}
您需要更好地處理異常,但這在功能上應(yīng)該是正確的.
You'll need to handle the Exceptions a little better, but that should be functionally correct.
這篇關(guān)于在 Magento 中跳過結(jié)帳以獲取可下載的產(chǎn)品的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!