2014-02-05 102 views
0

我已成功地使用自定義控制器創建訂單,並且一切正常。我的問題是我需要添加能夠使用可下載產品創建訂單。我只想指出從哪裏開始。 控制器正在處理所有設置和訂單保存。有沒有行動或我需要擊中的客戶能夠訪問他/她的下載?Magento以可編程的方式通過可下載的產品創建訂單

+0

我能夠得到這個工作。 – Rounder

+0

發佈信息的機會如何?# – vbak

回答

0

請嘗試下面的代碼,請發表評論。

<?php 
require_once 'app/Mage.php'; 
Mage::app(); 

$customer = Mage::getModel("customer/customer") 
     ->setWebsiteId(Mage::app()->getStore()->getWebsiteId()) 
     ->loadByEmail($data['email']); 
if(!$customer->getId()) { 
    $customer->setEmail($email); //enter Email here 
    $customer->setFirstname($fname); //enter Lirstname here 
    $customer->setLastname($lname); //enter Lastnamre here 
    $customer->setPassword(password); //enter password here 
    $customer->save(); 
} 



$storeId = $customer->getStoreId(); 

$quote = Mage::getModel('sales/quote') 
->setStoreId($storeId); 

$quote->assignCustomer($customer); 


// add product(s) 
$product = Mage::getModel('catalog/product')->load(186); //product id 
/*$buyInfo = array(
'qty' => 1, 
'price'=>0 
// custom option id => value id 
// or 
// configurable attribute id => value id 
);*/ 
$params = array(); 
$links = Mage::getModel('downloadable/product_type')->getLinks($product); 
$linkId = 0; 
foreach ($links as $link) { 
$linkId = $link->getId(); 
} 

//$params['product'] = $product; 
$params['qty'] = 1; 
$params['links'] = array($linkId); 
$request = new Varien_Object(); 
$request->setData($params); 
//$quoteObj->addProduct($productObj , $request); 

/* Bundled product options would look like this: 
$buyInfo = array(
"qty" => 1, 
"bundle_option" = array(
    "123" => array(456), //optionid => array(selectionid) 
    "124" => array(235) 
) 
); */ 
//$class_name = get_class($quote); 
//Zend_Debug::dump($class_name); 

$quote->addProduct($product, $request); 

$addressData = array(
'firstname' => 'Vagelis', //you can also give variables here 
'lastname' => 'Bakas', 
'street' => 'Sample Street 10', 
'city' => 'Somewhere', 
'postcode' => '123456', 
'telephone' => '123456', 
'country_id' => 'US', 
    'region_id' => 12, // id from directory_country_region table if it Country ID is IN (India) region id is not required 
); 
$billingAddress = $quote->getBillingAddress()->addData($addressData); 
$shippingAddress = $quote->getShippingAddress()->addData($addressData); 
/*$shippingAddress->setCollectShippingRates(true)->collectShippingRates() 
->setShippingMethod('flatrate_flatrate') 
->setPaymentMethod('checkmo');*/ 

/* Free shipping would look like this: */ 
$shippingAddress->setFreeShipping(true) 
->setCollectShippingRates(true)->collectShippingRates() 
->setShippingMethod('freeshipping_freeshipping') 
->setPaymentMethod('checkmo'); /* shipping details is not required for  downloadable product */ 
$quote->setCouponCode('ABCD'); // if required 
/* Make Sure Your Check Money Order Method Is Enable */ 
/*if the product value is zero i mean free product then the payment method is free array('method' => 'free')*/ 
$quote->getPayment()->importData(array('method' => 'checkmo')); 

$quote->collectTotals()->save(); 

$service = Mage::getModel('sales/service_quote', $quote); 
    $service->submitAll(); 
$order = $service->getOrder(); 

printf("Order Created Successfully %s\n", $order->getIncrementId()); 
相關問題