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?
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:
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.
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.
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:
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.
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.
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?
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.
@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
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
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?
@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..
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?
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 ...