3

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?

3
  • michelem.org/2011/02/23/… refer this link
    – Keyul Shah
    Commented Apr 11, 2014 at 17:02
  • I am doing something similar.The order is created but it's not available instantly.Admin has to invoice the order. And if I set -from the settings- to allow products to be downloaded even if order status is pending it's still not working.If I invoice the order programmatically again it's not working..
    – vbak
    Commented Apr 11, 2014 at 19:50
  • 1
    Your script is very useful for order simple product, can you please add script for grouped and bundle product? and also we can not change the Qty of product for this script.
    – GuRu
    Commented Mar 16, 2016 at 13:49

2 Answers 2

1

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

0

Thanks for your insights on this . To fix I simply invoice the order as admin as a result the events will be triggered.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_ADMINHTML); 

 if($order->canInvoice()) {

$invoiceId = Mage::getModel('sales/order_invoice_api')
  ->create($order->getIncrementId(), array());

$invoice = Mage::getModel('sales/order_invoice')
  ->loadByIncrementId($invoiceId);


  $invoice->capture()->save();
 }   

Where $order is an object for the order .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.