php - 无法在Magento订单创建脚本中设置送货方式

标签 php magento-1.7

我无法让我的脚本在使用Magento 1.7的实时站点上创建订单。我收到的错误是“请指定运输方式”,更多详细信息是

blockquote Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Please specify a shipping method.' in /home/mysite/public_html/app/Mage.php:594 Stack trace: #0 /home/mysite/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(303): Mage::throwException('Please specify ...') #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): Mage_Sales_Model_Service_Quote->submitAll() #4 {main} thrown in /home/mysite/public_html/app/Mage.php on line 594



我正在尝试使用下面的脚本来创建订单,并且我正在从另一个页面的帖子中传递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);

我相当确定地址设置正确,如果我在屏幕上回显该地址的话。

我尝试阅读文档,查看其他示例代码并更改其中的部分,以查看是否可以设置送货方式,但没有运气。

最佳答案

好吧,主要的解决方案似乎是在服务器重新启动期间解决的问题。但是,我还将发布我们将要使用的最终脚本。出于某种原因,在报价对象上设置运输方式似乎不起作用,因此我还回到了在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());

关于php - 无法在Magento订单创建脚本中设置送货方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20056215/

相关文章:

php - 基数违规 : 1241 Operand should contain 1 column(s) PDO

php - Laravel 调度调用 Controller

php - 添加新数据库条目时如何触发 .php 页面的刷新(重新加载)?

magento-1.7 - 我正在尝试发布数据,但我在 Magento REST API 上不断收到此错误 "code": 400, "message": "The request data is invalid."

php - 在结帐页面 magento 更新产品数量

php - 如何在 magento 中创建新的自定义选项类型?

Magento 1.7 - 从/customer/account/create/中的注册中删除字段

php - (PHP) 在同一页面上处理表单提交与在单独的页面上处理相比更好/更少/同样好吗?

Magento 客户团体税

Php 选择每一种