更新时间:2023-11-30 11:45:58
此代码将允许已登录的客户下订单"购买免费的虚拟产品,同时绕过结帐并将其直接重定向到我的下载"部分他们的帐户.
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.