我无法让我的脚本在我的使用Magento 1.7的实时网站上创建订单。我得到的错误是“请指定送货方式”更多细节
blockquote致命错误:未捕获的异常“Mage_Core_Exception”,并显示消息“请指定送货方式”。在/home/mysite/public_html/app/Mage.php:594堆栈追踪:#0 /home/mysite/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(303):法师: :throwException('请指定......')#1 /home/mysite/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(222):Mage_Sales_Model_Service_Quote-> _validate()#2 /home/mysite/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(238):Mage_Sales_Model_Service_Quote-> submitNominalItems()#3 /home/mysite/public_html/apitest/magento_order_create.php( 82):第594行/home/mysite/public_html/app/Mage.php中的Mage_Sales_Model_Service_Quote-> submitAll()#4 {main}
我正在尝试使用下面的脚本来创建订单,我正在从另一个页面传递skus和数量。 `
<?php
// Link Mage Class
require ('../app/Mage.php');
// Initialize Magento framework
Mage::app('mysite');
//create a cart
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('mysite')->getId());
//Get Customer by Id
$customer = Mage::getModel('customer/customer')->load('1');
//attach customer to cart
$quote->assignCustomer($customer);
//attach products
foreach ($_POST as $sku=>$qty)
{
$product = Mage::helper('catalog/product')->getProduct($sku,Mage::app()->getStore()->getId(), 'sku');
$buyInfo = array(
'qty' => $qty,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));
}
//get and set customer billing address
//need to work on this encase we use diffrent billing and shipping addresses
$addressData = Mage::getModel('customer/address')->load('1');
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
// set shipping and payment methods. assumes freeshipping and check payment
// have been enabled.
/*
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('freeshipping_freeshipping')
->setPaymentMethod('checkmo');
*/
// THIS IS WHERE THE ERROR SEEMS TO BE
$quote->getShippingAddress()->setShippingMethod('freeshipping_freeshipping');
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
//set payment method
$quote->getPayment()->importData(array('method' => 'checkmo'));
//save cart and check out
$quote->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
printf("Created order %s\n", $order->getIncrementId());
`
上述脚本最初来自http://pastebin.com/8cft4d8v 它在我的测试环境(也是1.7)上就像一个魅力。我已经注释掉了原始的运输方法代码并且已经分解成单独的行。 实际站点有两个站点和两个商店,我确保在后端启用免费送货并测试它在我运行此脚本时显示。
<?php
// Link Mage Class
require ('../app/Mage.php');
// Initialize Magento framework
Mage::app('mysite');
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
var_dump($methods);
我相当确定地址设置正确,如果我回显到屏幕那个地址就在那里。
我已经尝试阅读文档,查看其他示例代码并更改其中的部分内容,看看我是否可以设置运费方法,但没有运气。
答案 0 :(得分:7)
主要解决方案似乎是在服务器重启期间解决的问题。但我也将发布我们将要使用的最终脚本。出于某种原因,在引用对象上设置送货方法似乎不起作用,所以我也回到了http://pastebin.com/8cft4d8v上找到的“原始”脚本。如果它们存在与否,我也摆脱了一些似乎并不重要的线条。希望这可以帮助某人下线
<?php
// Link Mage Class
require ('..\app\Mage.php');
// Initialize Magento framework
Mage::app('my');
//create a cart
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('default')->getId());
//Get Customer by Id
$customer = Mage::getModel('customer/customer')->load('1');
//attach customer to cart
$quote->assignCustomer($customer);
//attach products
foreach ($_POST as $sku=>$qty)
{
$product = Mage::helper('catalog/product')->getProduct($sku, Mage::app()->getStore()->getId(), 'sku');
$buyInfo = array(
'qty' => $qty,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));
}
$billingAddress = $quote->getBillingAddress()->addData();
$shippingAddress = $quote->getShippingAddress()->addData();
// set shipping and payment methods. assumes freeshipping and check payment
// have been enabled.
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate');
$quote->getPayment()->importData(array('method' => 'checkmo'));
//save cart and check out
$quote->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
printf("Created order %s\n", $order->getIncrementId());