Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 2 of 10
Trouble creating a quote/order through custom code (programmatically for use with API)
 
daviddripps
Member
 
Total Posts:  47
Joined:  2009-04-15
FL, USA
 

My orders don’t process, I’m running into an issue in the PrepareOrder function while adding products.  The line “$quoteItem=Mage::getModel(’sales/quote_item’)->setProduct($productObj);” seems to be stopping the script from executing any further lines. Any thoughts as to why?

 
Magento Community Magento Community
Magento Community
Magento Community
 
freshwebs
Sr. Member
 
Total Posts:  153
Joined:  2008-05-27
Leicestershire, UK
 

Hi David,
Not sure why, but I found several things while struggling:
1) make sure that you’re script is not outputting any debugging info like echo ‘’;, etc.
2) Where its falling over, enclose the problem code in a try catch stmt like so:

try{
$quoteItem
=Mage::getModel(’sales/quote_item’)->setProduct($productObj);
}catch(Exception $e){
 
echo $e;
}
This should output the cause of your problem
3) Make sure that the include_once stmt is actually only included once! Sounds obvious but easy to overlook.

Cheers,
Eddie

 
Magento Community Magento Community
Magento Community
Magento Community
 
daviddripps
Member
 
Total Posts:  47
Joined:  2009-04-15
FL, USA
 

freshweb -

Thanks for your reply, no luck though.

1) I had echo’s and print_r()’s all over the place so I removed them, but it’s still not working.

2) It threw the following exception:

exception ‘Exception’ with message ‘Notice: Undefined index: in /home/www/magento/app/code/core/Mage/Catalog/Model/Product/Type.php on line 77’ in /home/www/magento/app/code/core/Mage/Core/functions.php:240 Stack trace: #0 /home/www/magento/app/code/core/Mage/Catalog/Model/Product/Type.php(77): mageCoreErrorHandler(8, ‘Undefined index...’, ‘/home/www/magento...’, 77, Array) #1 /home/www/magento/app/code/core/Mage/Catalog/Model/Product.php(189): Mage_Catalog_Model_Product_Type::factory(Object(Mage_Catalog_Model_Product), true) #2 /home/www/magento/app/code/core/Mage/Catalog/Model/Product.php(1213): Mage_Catalog_Model_Product->getTypeInstance(true) #3 /home/www/magento/app/code/core/Mage/Sales/Model/Quote/Item.php(184): Mage_Catalog_Model_Product->getSku() #4 /home/www/magento/MivaUploader/importOrders.php(80): Mage_Sales_Model_Quote_Item->setProduct(Object(Mage_Catalog_Model_Product)) #5 /home/www/magento/MivaUploader/importOrders.php(183): PrepareOrder() #6 {main}

line 77: $productObj=$productModel->load($PartId);
line 78: try{
line 79:  $quoteItem=Mage::getModel(’sales/quote_item’)->setProduct($productObj);
line 80: } catch(Exception $e){
line 81:  echo $e;
line 82: }

I don’t think the $productObj is getting the SKU.  When I did a print_r($productObj) on line 78, the SKU (10403) was nowhere to be found. I think the issue may be that the SKU for these products are assigned in the Custom Options SKU fields of a simple product, not in the SKU field of the simple product itself.

3) I moved the require_once to the top of the file before any functions and removed it from both functions.

- David

 
Magento Community Magento Community
Magento Community
Magento Community
 
freshwebs
Sr. Member
 
Total Posts:  153
Joined:  2008-05-27
Leicestershire, UK
 

Hi David,
Flying blind here but here’s a thought:

Are you sure that your array contains a key/value pair for $PartId?

Try var_dump($array) to make sure that your array has PartId. My own csv file had a field ‘product_id’ and in the resulting array was structured like
Order =>array("ITEMS" =>array("product_id" => “123”, “price” =>"123") ......, etc etc. I then looped through the array something like this:

foreach($ITEMS as $item{

        
foreach($item as $k=>$v$$k=$v}

        $productObj
=$productModel->load($product_id);
        
$quoteItem=Mage::getModel('sales/quote_item')->setProduct($productObj);
        
$quoteItem->setQuote($quoteObj);
....

Cheers,
Eddie

 
Magento Community Magento Community
Magento Community
Magento Community
 
daviddripps
Member
 
Total Posts:  47
Joined:  2009-04-15
FL, USA
 

Eddie -

yeah, the variable is definitely assigned.  I’m thinking it may be a version issue.  Even if I add the product SKU directly into the $productModel->load() it returns the same error, and I know that that particular SKU is just a simple product with nothing fancy to it.  I’ll just keep pouring through the class files hoping to find something.  When I figure it out, I’ll be sure to post it for everyone.  Thanks again for your suggestions.

- David

 
Magento Community Magento Community
Magento Community
Magento Community
 
daviddripps
Member
 
Total Posts:  47
Joined:  2009-04-15
FL, USA
 

For anyone who comes across a similar issue...I was just overlooking a very minuscule detail.  I was trying to add the product by the SKU I had assigned, not the product ID assigned by Magento.  Eddie, thanks again for your help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
CVinson86
Member
 
Avatar
Total Posts:  40
Joined:  2008-06-18
 

Hey,
I’m using a slightly modified version of jesse_dev’s code (thanks Jesse!), but I’ve run into a snag.  Inside the loop:

foreach($PartCart as $part{
        
foreach($part as $k=>$v$$k=$v}
        $productObj
=$productModel->load($PartId);
        
$quoteItem=Mage::getModel('sales/quote_item')->setProduct($productObj);
        
$quoteItem->setQuote($quoteObj);
        
$quoteItem->setQty($Quantity);
        
$quoteObj->addItem($quoteItem);
    
}

I’ve added $quoteItem->setPrice($price).  After the call to $quoteObj->collectTotals, the price is set back to it’s default price.  Any ideas on how to fix this?

Thanks,
-Colin

 
Magento Community Magento Community
Magento Community
Magento Community
 
LL
Member
 
Total Posts:  47
Joined:  2008-09-23
 

Thanks to everyone for posting their work. It’s greatly appreciated!

I used Jesse_dev’s second bit of code and it worked with v1.1.6.

http://www.magentocommerce.com/boards/viewthread/28426/#t128780

Thanks Jesse!

LL

 
Magento Community Magento Community
Magento Community
Magento Community
 
nasha
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2009-04-15
 

Hi all
i found getdefaultbilling address that return billing address of customer.i want to set billing address and shipping address of customer using script.help me out.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jesse_dev
Jr. Member
 
Total Posts:  27
Joined:  2009-03-03
 

I’m glad everyone’s able to use the functions!!

@nasha - not sure if it’s possible to over-ride a customers default shipping/billing info. give this a try, you will need to construct the address objects manually :

$billing_address=$quoteObj->getBillingAddress(); //try constructing a custom Address object here
$shipping_address=$quoteObj->getShippingAddress(); //here too

$convertQuoteObj=Mage::getSingleton('sales/convert_quote');
$order_billing_address=$convertQuoteObj->addressToOrderAddress($billing_address);
$order_shipping_address=$convertQuoteObj->addressToOrderAddress($shipping_address);

$orderObj->setBillingAddress($order_billing_address);
$orderObj->setShippingAddress($order_shipping_address);

also, definitely catch exceptions and use the logger instead of debug echo’s. freshwebservices picked that up, kudos to them.
just make sure logging is enabled and working.. System->Configuration->Developer

function ConfirmOrder($quoteId{
try {
....
catch (Exception $e{
$msg
=$e->getMessage();
$file=$e->getFile().':'.$e->getLine()."\n";
$trace=$e->getTraceAsString();
$str="\n$msg\n$file\n$trace\n";
Mage::log($str);
}
}
//end function

also, the functions I posted only do purchase orders, and much of the order needs to be completed via the admin. I’m currently working on pushing the order through and shipping it with authorize.net payments.. so far it looks like the same class that converts quotes to orders will convert an order to an invoice and a shipment

 
Magento Community Magento Community
Magento Community
Magento Community
 
jesse_dev
Jr. Member
 
Total Posts:  27
Joined:  2009-03-03
 

I bring more code, here’s some code for invoicing an order. I just tested it and it works. It loads the order by the entity id , not increment id.

function InvoiceOrder($orderId{
try {
    
require_once '../app/Mage.php';
    
$app Mage::app();
    
Mage::register('isSecureArea'true);

    
$orderObj=Mage::getModel('sales/order')->load($orderId);
    
$convertOrderObj=Mage::getSingleton('sales/convert_order');
        
$invoiceObj=$convertOrderObj->toInvoice($orderObj);

        foreach (
$orderObj->getAllItems() as $item{

        $invoiceItem 
$convertOrderObj->itemToInvoiceItem($item);

        if (
$item->getParentItem()) {
            $invoiceItem
->setParentItem($invoiceObj->getItemById($item->getParentItem()->getId()));
        
}
            $invoiceItem
->setQty($item->getQtyToInvoice());
            
$invoiceObj->addItem($invoiceItem);
        
}

        $invoiceObj
->collectTotals();
        
$invoiceObj->register();

        
$orderPaymentObj=$orderObj->getPayment();
        
$orderPaymentObj->pay($invoiceObj);

        
$invoiceObj->getOrder()->setIsInProcess(true);
        
$transactionObj Mage::getModel('core/resource_transaction');
        
$transactionObj->addObject($invoiceObj);
        
$transactionObj->addObject($invoiceObj->getOrder());
        
$transactionObj->save();

        
$invoiceObj->save();
        
$invoiceId=$invoiceObj->getId();
    return 
$invoiceId;
catch (Exception $e{

$msg
=$e->getMessage();
$file=$e->getFile().':'.$e->getLine()."\n";
$trace=$e->getTraceAsString();
$str="\n$msg\n$file\n$trace\n";
Mage::log($str);

}
}

also here’s some code for canceling an order:

$orderObj=Mage::getModel('sales/order')->load($entity_id);
    if (
$orderObj->canCancel()) {
        $orderObj
->cancel();
        
$orderObj->save();
        
$success=true;
    
else 
        $success
=false
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
freshwebs
Sr. Member
 
Total Posts:  153
Joined:  2008-05-27
Leicestershire, UK
 

Hi jess_dev,
Thanks for this - I’m sure I’ll be using it shortly - I have some 10K historic orders to transition from ‘pending’ to ‘invoiced’ status & was wondering how I might do it. This will be a good building block - so thanks again.

On a slight side note: while uploading orders, I tried to add discounts within the $quoteItem loop but these were ignored & the full product price was used. Do you have any ideas on how to apply discounts while programmatically uploading orders?

Cheers,
Eddie

 
Magento Community Magento Community
Magento Community
Magento Community
 
jesse_dev
Jr. Member
 
Total Posts:  27
Joined:  2009-03-03
 

@Eddie - Yea we have our site set up to give a certain customer group free shipping. I haven’t looked at the code yet, but in the admin we’re doing this under Promotions->Shopping Cart Price Rules. The way it works is we have 2 rules with different priorities. The first one gives a 15 percent discount, then the 2nd one makes the shipping free. I might have some code for that in the next week or 2..

Cheers,
Jesse

 
Magento Community Magento Community
Magento Community
Magento Community
 
justSelling
Jr. Member
 
Avatar
Total Posts:  2
Joined:  2009-06-15
Germany
 

Hi, first of all - thanks to all for the great code here in the topic to create a new orders!

I have a problem when creating orders including download products. Everything is fine, but the links will not be attached to the order and at least the downloads are not possible. Did anybody manage to create an order including download products already? Which code has to be added to set up the purchased links correctly?

Thanks
Michael

 
Magento Community Magento Community
Magento Community
Magento Community
 
jesse_dev
Jr. Member
 
Total Posts:  27
Joined:  2009-03-03
 

i finally figured out how to attach a shipping method. it’s best to do it in the quote and then convert the quote to an order.
you just need to figure out the codes for everything ie ups_GND , ups_1DA, ups_3DS ...

$quoteObj->getShippingAddress()->setShippingMethod("ups_GND");
    
$quoteObj->getShippingAddress()->setCollectShippingRates(true)->save();
    
$quoteObj->collectTotals(); ///calls address->collectTotals
    
$quoteObj->save();
    
$quoteId=$quoteObj->getId();
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 2 of 10