问题描述
我正在使用 Magento 构建一个电子书网站.对于该版本,我们计划提供一些可免费下载的书籍.我们希望可以使用普通的 Magento 'catalog' 功能来添加下面的产品类别.但是,由于这些产品是可免费下载的产品,因此在用户尝试下载时通过结帐将其发送并没有意义.
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.
有谁知道一种方法来创建一个完全绕过结帐的免费可下载产品?我注意到可下载产品有一个免费样品"选项,但如果可以,我宁愿不使用它,因为我计划在添加付费产品时将此字段用于其预期目的.
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.
我注意到你们中的一些人因为问题不明确"而否决了这个问题.为了清楚起见,我想:
I have noticed that some of you have voted this question down for 'lack of question clarity'. For clarity, I want:
- 想知道是否有可能创建一个可下载的产品不需要用户的 Magento通过通常的结帐过程(因为它是免费的)
- 和这不是免费样品"字段可下载产品的
不幸的是,我认为我不能再雄辩地问这个问题了.[/编辑]
Unfortunately I don't think I can ask this any more eloquently. [/EDIT]
推荐答案
此代码将允许已登录的客户下订单"购买免费的虚拟产品,同时绕过结帐并将其直接重定向到我的下载"部分他们的帐户.
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
创建一个新模块:
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);
}
}
您需要更好地处理异常,但这在功能上应该是正确的.
You'll need to handle the Exceptions a little better, but that should be functionally correct.
这篇关于在 Magento 中跳过结帐以获取可下载的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!