magento 1.9.3.7
error_reporting(E_ALL); echo "starting...<br>"; require 'app/Mage.php'; $sql = "SELECT * FROM CubeCart_order_sum order by time ASC limit 1"; $res = mysqli_query($db_target,$sql); while($obj = mysqli_fetch_object($res)) { echo "step 1<br>"; Mage::app(1); $customer = Mage::getModel('customer/customer') ->setWebsiteId(1) ->load(410); echo "step 2<br>"; $quote = Mage::getModel('sales/quote')->setStoreId(1); $quote->assignCustomer($customer); $quote->setInventoryProcessed(true); echo "step 3<br>"; $_prod = Mage::getModel('catalog/product')->load(265404); $quote->addProduct($_prod,new Varien_Object(array('qty' => $_POST['qty']))); echo "step 4<br>"; $quote->collectTotals()->save(); echo "step 5<br>"; $service = Mage::getModel('sales/service_quote', $quote); echo "step 6<br>"; $service->submitAll(); echo "step 7<br>"; } ?>
step 7 is not printed on the screen, so I assume something went wrong there... and also no order was created
what am I doing wrong?
Hello @Loginname
Here is the guide to help you with place order programmatically.
@Loginname use below link to create order programmatically.
http://www.blogtreat.com/programmatically-create-new-order-magento/
Thanks
Hi
It almost worked. I get the "There was an error processing your order. Please contact us or try again later." message.:
step 4
step 5
step 6
step 7
step 8
There was an error processing your order. Please contact us or try again later.
Digging into the logging folder I find this errors which was created when I ran the script:
Notice: Array to string conversion
I`ve been looking into where this array is being created withot finding it. This script is being executed on PHP 7.0.33
Any suggestions?
<?php //http://www.blogtreat.com/programmatically-create-new-order-magento/ //https://magento.stackexchange.com/questions/226925/how-to-create-order-programmatically-with-customer-email-in-magento-1-9 error_reporting(E_ALL); echo "starting...<br>"; require 'app/Mage.php'; echo "initializing...<br>"; umask(0); //Mage::app('default'); Mage::app(1); $store = Mage::app()->getStore(); $website = Mage::app()->getWebsite(); // initialize sales quote object //$quote = Mage::getModel('sales/quote')->setStoreId($storeId); $quote = Mage::getModel('sales/quote')->setStoreId(1); // set customer information $test_email = "test@test.com"; $customer_email = $test_email; $customer_firstname = "test"; $customer_lastname = "test"; echo "step 1<br>"; $billingAddress = array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => 'test', 'middlename' => '', 'lastname' => 'test', 'suffix' => '', 'company' => '', 'street' => array( '0' => 'Thunder River Boulevard', // required '1' => 'Customer Address 2' // optional ), 'city' => 'Teramuggus', 'country_id' => 'US', // country code 'region' => 'Alaska', 'region_id' => '2', 'postcode' => '99767', 'telephone' => '123-456-7890', 'fax' => '', 'save_in_address_book' => 1 ); $shippingAddress = array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => 'test', 'middlename' => '', 'lastname' => 'test', 'suffix' => '', 'company' => '', 'street' => array( '0' => 'Thunder River Boulevard', // required '1' => 'Customer Address 2' // optional ), 'city' => 'Teramuggus', 'country_id' => 'US', 'region' => 'Alaska', 'region_id' => '2', 'postcode' => '99767', 'telephone' => '123-456-7890', 'fax' => '', 'save_in_address_book' => 1 ); // check whether the customer already registered or not $customer = Mage::getModel('customer/customer')->setWebsiteId($website->getId())->loadByEmail($test_email); echo "kundeid = " . $customer->getId() . "<br>"; if (!$customer->getId()) { echo "fant ikke kunden, oppretter den nå...<br>"; // create the new customer account if not registered $customer = Mage::getModel('customer/customer'); $customer->setWebsiteId($website->getId()) ->setStore($store) ->setFirstname($customer_firstname) ->setLastname($customer_lastname) ->setEmail($customer_email); try { $password = $customer->generatePassword(); $customer->setPassword($password); // set the customer as confirmed $customer->setForceConfirmed(true); $customer->save(); $customer->setConfirmation(null); $customer->save(); // set customer address $customerId = $customer->getId(); $customAddress = Mage::getModel('customer/address'); $customAddress->setData($billingAddress) ->setCustomerId($customerId) ->setIsDefaultBilling('1') ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); // save customer address $customAddress->save(); // send new account email to customer $storeId = $customer->getSendemailStoreId(); $customer->sendNewAccountEmail('registered', '', $storeId); // set password remainder email if the password is auto generated by magento $customer->sendPasswordReminderEmail(); } catch (Exception $e) { Mage::logException($e); } } // assign the customer to quote $quote->assignCustomer($customer); echo "assigning customer...<br>"; // set currency for the quote $quote->setCurrency(Mage::app()->getStore()->getBaseCurrencyCode()); echo "setting currency<br>"; //$productIds = array(337 => 2, 338 => 3); $productIds = array(265404 => 1); echo "product id...<br>"; echo "<pre>"; print_r($productIds); echo "</pre>"; // add products to quote /* foreach($productIds as $productId => $qty) { try { $product = Mage::getModel('catalog/product')->load($productId); echo $product->getId() . "<br>"; $quote->addProduct($product, $qty); } catch (Exception $e) { echo $e->getMessage() . "<br>"; } } */ echo "<hr>"; $product = Mage::getModel('catalog/product')->load(265404); echo $product->getId() . "<br>"; $quote->addProduct($product, 1); echo "step 2<br>"; // add billing address to quote $billingAddressData = $quote->getBillingAddress()->addData($billingAddress); echo "step 3<br>"; // add shipping address to quote $shippingAddressData = $quote->getShippingAddress()->addData($shippingAddress); echo "step 4<br>"; // collect shipping rates on quote $shippingAddressData->setCollectShippingRates(true)->collectShippingRates(); //orginalt var det false echo "step 5<br>"; // set shipping method and payment method on the quote //$shippingAddressData->setShippingMethod('flatrate_flatrate') // ->setPaymentMethod('paypal_express'); $shippingAddressData->setShippingMethod('flatrate_flatrate'); echo "step 6<br>"; // Set payment method for the quote try { $quote->getPayment()->importData(array('method' => 'paypal_express')); echo "step 7<br>"; // collect totals & save quote $quote->collectTotals()->save(); echo "step 8<br>"; // create order from quote $service = Mage::getModel('sales/service_quote', $quote); $service->submitAll(); echo "step 9<br>"; $increment_id = $service->getOrder()->getRealOrderId(); echo 'Order Id: ' .$increment_id; } catch (Exception $e) { echo $e->getMessage() . "<br>"; Mage::logException($e); }
I forgot to mention that the exception.log file report this in regard to my error:
public_html/app/code/core/Mage/Paypal/Model/Api/Nvp.php(999): Mage::throwException('There was an er...')
This is related to the Array to string conversion error
hope that can give you some clue what is wrong?
@Loginname This issue might be related with php version.
Can you please let me know which version are you currently using?