Unable to set Shipping Method in Magento order create script

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 13:36:10

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());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!