cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically create sales order using quote with custom shipping method

Programmatically create sales order using quote with custom shipping method

I am using Magento 1.9.x and I would like to create sales orders programmatically using quotes with my own custom shipping method, shipping price & title.

 

This is my custom shipping method model:

 

<?php

class Mycompany_Mymodule_Model_Carrier
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{
    protected $_code = 'icw_shipping';

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!Mage::registry($this->_code)) {
            return false;
        }

        $info = Mage::registry($this->_code);
        $method = Mage::getModel('shipping/rate_result_method');
        $method->setCarrier($this->_code);
        $method->setMethod($this->_code);
        $method->setCarrierTitle($info['shippingCarrier']);
        $method->setMethodTitle($info['shippingTitle']);
        $method->setPrice($info['shippingPrice']);
        $method->setCost($info['shippingPrice']);
        $result = Mage::getModel('shipping/rate_result');
        $result->append($method);
        Mage::unregister($this->_code);
        return $result;
    }

    public function getAllowedMethods()
    {
        return array(
            $this->_code => 'ICW Shipping',
        );
    }
}

 

I am trying to use it like this in my order creator:

 

// Method to save sales order quote
private function saveSalesOrderQuote()
{
    Mage::register($this->_settings['ShippingMethod'], array(
        'shippingCarrier' => 'Custom',
        'shippingTitle'   => $this->_orderData->ShippingMethod,
        'shippingPrice'   => $this->_orderData->ShippingAmount
    ));
    $this->_quote->getShippingAddress()->setShippingMethod(
        $this->_settings['ShippingMethod']
    );
    $this->_quote->getShippingAddress()->setCollectShippingRates(
        true
    );
    $this->_quote->getShippingAddress()->collectShippingRates();
    $this->_quote->collectTotals();
    $this->_quote->reserveOrderId();
    $this->_quote->save();
}

 

But it does not appear to be working correctly. When the order is created, everything is correct expect for the shipping method, title & price. This is what I see in the backend:

 

 

Here's my full code so far: http://pastebin.com/jUTM0VbD

 

Any idea what I am doing wrong here? How do I use my own custom shipping method and set custom price and title?