magento 1.9.3.7
I have an issue with the code below, I get the Caught exception: Please specify a shipping method message
<?php
//https://magento.stackexchange.com/questions/223220/creating-order-programatically-showing-wrong-grand-total
error_reporting(E_ALL);
require 'app/Mage.php';
$storeId = Mage::app()->getStore()->getId();
$Quote = Mage::getModel('sales/quote')->setStoreId($storeId);
$Customer = Mage::getModel('customer/customer')->load(410); // insert customer ID
$_product = Mage::getModel('catalog/product')->load(168789);
// Assign Customer To Sales Order Quote
$Quote->assignCustomer($Customer);
$address = $Customer->getDefaultBillingAddress();
$Quote->getBillingAddress()->importCustomerAddress($address);
$Quote->getShippingAddress()->importCustomerAddress($address);
$Quote->setSendCconfirmation(0);
// Loop to add order. $Item contains the Sku, Qty, Price
try
{
$Quote->addProduct($_product, new Varien_Object(array('qty' => 1)));
$Quote->save();
$QuoteItem = $Quote->getItemByProduct($_product);
$QuoteItem->setOriginalCustomPrice(49);
$QuoteItem->setCustomPrice(49);
$QuoteItem->setIsSuperMode(true);
$QuoteItem->save();
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "<br>";
print "Product ID: " . $_product->getId() . "<br>\n";
print "Product Sku: " . $_product->getSku() . " - " . $_product->getName() . "<hr>\n";
}
//***********************************'
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
echo "<pre>";
print_r($methods);
echo "</pre>";
//******************************************
//saving order
try
{
echo "step1<br>";
$Quote->collectTotals()->save();
$Quote->getShippingAddress()->collectTotals();
// Collect Rates and Set Shipping & Payment Method
$Quote->getShippingAddress()
->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('amtable');
$Quote->getPayment()->setMethod('paypal_express');
echo "step2<br>";
// Create Order From Quote
$service = Mage::getModel('sales/service_quote', $Quote);
$service->submitAll();
echo "step3<br>";
$increment_id = $service->getOrder()->getRealOrderId();
}
catch (Exception $e)
{
echo '<blockquote>Caught exception: ', $e->getMessage(), "</blockquote>";
}
?>Output is this
Array
(
[amtable] => Amasty_Table_Model_Carrier_Table Object
(
[_code:protected] => amtable
[_rates:protected] =>
[_numBoxes:protected] => 1
[_freeMethod:protected] => free_method
[_isFixed:protected] =>
[_customizableContainerTypes:protected] => Array
(
)
[_debugReplacePrivateDataKeys:protected] => Array
(
)
[_data:protected] => Array
(
[id] => amtable
[store] =>
)
[_hasDataChanges:protected] => 1
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
)
step1
step2
Caught exception: Please specify a shipping method.I think the error is between step2 and step3, notica that step3 is not displayed. I used a script to get an array of active carriers. Just to make sure it is fine to use 'amtable'. I have shipping method from amasty
any idea what I do wrong?
I enabled flatrate shipping and updaed the script, below you see the section I made a change:
//saving order
try
{
echo "step1<br>";
$Quote->collectTotals()->save();
$Quote->getShippingAddress()->collectTotals();
// Collect Rates and Set Shipping & Payment Method
$Quote->getShippingAddress()
->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate');
$Quote->getPayment()->setMethod('paypal_express');
echo "step2<br>";
// Create Order From Quote
$service = Mage::getModel('sales/service_quote', $Quote);
$service->submitAll();
echo "step3<br>";
$increment_id = $service->getOrder()->getRealOrderId();
}
catch (Exception $e)
{
echo '<blockquote>Caught exception: ', $e->getMessage(), "</blockquote>";
}and output is:
step1
step2
Caught exception: There was an error processing your order. Please contact us or try again later.
anyone having an idea what I do wrong?
step1 step2 Caught exception: There was an error processing your order. Please contact us or try again later.