Magento以可编程方式使用可下载产品创建订单

时间:2014-02-05 21:30:21

标签: magento

我已成功使用Custom控制器创建订单,一切正常。我的问题是我需要添加使用可下载产品创建订单的功能。我想指出从哪里开始。 控制器正在处理所有设置和订单保存。是否有一项行动或我需要为客户提供能够访问他/她的下载的内容?

1 个答案:

答案 0 :(得分: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());