Php 无法在Magento订单创建脚本中设置发货方式
我很难让我的脚本在使用Magento 1.7的live站点上创建订单。我得到的错误是“请指定装运方式”,更多详细信息如下 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):Mage::throweexception('请指定…)php(222):Mage_Sales_Model_Service_Quote->u 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):Mage_Sales_Model_Service_Quote->submitAll()#4{main}在第594行输入/home/mysite/public_html/app/Mage.php 我正在尝试使用下面的脚本创建订单,我正在从另一个页面传递帖子中的SKU和数量`Php 无法在Magento订单创建脚本中设置发货方式,php,magento-1.7,Php,Magento 1.7,我很难让我的脚本在使用Magento 1.7的live站点上创建订单。我得到的错误是“请指定装运方式”,更多详细信息如下 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):Mage::
<?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());
`
主要的解决方案似乎是在服务器重新启动时解决的问题。但我也将发布我们将要使用的最终脚本。由于某些原因,在quote对象上设置shipping方法似乎不起作用,因此我也回到了在上找到的“原始”脚本。我还删除了一些似乎并不重要的行,如果它们存在或不存在。希望这能帮助其他人
<?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());
I get:Uncaught argumentcounter错误:函数Varien_Object::addData()的参数太少,已传递0
<?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());