且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-30 08:30:28

主要解决方案似乎是在服务器重新启动期间解决的.但是我还将发布我们将要使用的最终脚本.由于某种原因,在报价对象上设置送货方式似乎无效,因此我还回到了在 http://上找到的原始"脚本: //pastebin.com/8cft4d8v .我还摆脱了一些似乎并不重要的行,无论它们是否存在.希望这可以帮助某人

Well the main solution seems to be something that was solved during a server reboot. But I am also going to post the final script that we are going to use. For some reason setting shipping method on a quote object does not seem to work, so I also went back to the "orginal" script that I had found on http://pastebin.com/8cft4d8v. I also got rid of some lines that did not seem to matter if they existed or not. Hope this may help someone down the line

<?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());