Magento1.8 – How to Create Order Programmatically with Downloadable Products

magento-1.8ordersquote

I am trying to make an order programmatically with downloadable products. I don't care about payment, the only thing I do care is that the products will be available without admin has to do anything with those orders.

    require_once 'app/Mage.php';

    Mage::app();



    $id=7; // get Customer Id
    $customer = Mage::getModel('customer/customer')->load($id);

    $storeId = $customer->getStoreId();

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

$quote->assignCustomer($customer);


// add product(s)
$product = Mage::getModel('catalog/product')->load(186);
$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);

/* [adamw] 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',
    'lastname' => 'Bakas',
    'street' => 'Sample Street 10',
    'city' => 'Somewhere',
    'postcode' => '123456',
    'telephone' => '123456',
    'country_id' => 'US',
    'region_id' => 12, // id from directory_country_region table
);

$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);

/*$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
    ->setShippingMethod('flatrate_flatrate')
    ->setPaymentMethod('checkmo');*/


/* [adamw] Free shipping would look like this:
*/
$shippingAddress->setFreeShipping( true )
    ->setCollectShippingRates(true)->collectShippingRates()
    ->setShippingMethod('freeshipping_freeshipping')
    ->setPaymentMethod('checkmo');

$quote->setCouponCode('ABCD');
$quote->getPayment()->importData(array('method' => 'checkmo'));

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

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



printf("Created order %s\n", $order->getIncrementId());

That's the code that finally did it BUT: The downloadable products are not available instantly. If you invoice the order manually the products are available.If you invoice the order programmatically the products are not available.

Any idea why and how to bypass that? Am I doing something wrong here?

Best Answer

After a lot of investigation I find out how to do it. My main problem once again was that the order was created BUT it wasn't available after submiting it.Also if you invoice it programmatically again it wasn't available (When I say available I mean to have the links and how many times you have left to download it..).

So what I notice is that if you go to Mage/Downloadable/Model/Observer.php

you will find out that downloadable product uses observers that makes the links available when you go through the basket.But if you do it programmatically these events are not triggered(At least with my knowledge). If you use the functions : setLinkStatus and saveDownloadableOrderItem you will be able to save the links and the downloadable products will be working fine without the need of doing nothing else.

If I make a mistake or if there is another solution please correct me as I am still on learning process.

Cheers