Garp Informatica
Garp Informatica

Reputation: 536

Magento Set Shipping cost when create order programmatically

INFO: Magento 1.7.0.2 CE

I have made a module for importing order from an external feed by cron schedule, i also create a custom carrier.

All works well but i can't set the shipping cost... this is the code:

        $shippingAddress = $quote->getShippingAddress()->addData($addressData);

        $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                ->setShippingMethod('customname_customname')
                ->setShippingAmount('10')
                ->setBaseShippingAmount('10');

        $quote->collectTotals();    
        $quote->save();

$addressData contain the customer infos

I have tried different method but i can't set the shipping cost. HELP!

This is the custom carrier code:

    protected $_code = 'customname';  

    /** 
    * Collect rates for this shipping method based on information in $request 
    * 
    * @param Mage_Shipping_Model_Rate_Request $data 
    * @return Mage_Shipping_Model_Rate_Result 
    */  
    public function collectRates(Mage_Shipping_Model_Rate_Request $request){  
        $result = Mage::getModel('shipping/rate_result');  
        $method = Mage::getModel('shipping/rate_result_method');  
        $method->setCarrier($this->_code);  
        $method->setCarrierTitle($this->getConfigData('title'));
        $method->setMethod($this->_code);  
        $method->setMethodTitle($this->getConfigData('name'));
        $method->setPrice('0.00');
    $method->setCost('0.00');
        $result->append($method);  
        return $result;  
    }  

    /**
     * Get allowed shipping methods
     *
     * @return array
     */
    public function getAllowedMethods()
    {
        return array($this->_code=>$this->getConfigData('name'));
    }
}  

Upvotes: 1

Views: 23786

Answers (4)

sushant kumar
sushant kumar

Reputation: 141

You can update shipping cost through these line of code:-

$order = Mage::getModel('sales/order')->load($incrementId, 'increment_id');
$shippingprice = 30;//custom shipping price
$order->setShippingAmount($shippingprice);
$order->setBaseShippingAmount($shippingprice);
$order->setGrandTotal($order->getGrandTotal() + $shippingprice); //adding shipping price to grand total
$order->save();

It will helpful for you.

Upvotes: 3

Abhilash
Abhilash

Reputation: 63

Please check the collectRates(Mage_Shipping_Model_Rate_Request $request) method of your shipping module. Check how you shipping total is initialized.

Make sure that the shipping total gets updated and saved in $price variable of this method.

    $handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
    $result = Mage::getModel('shipping/rate_result');
    $show = true;
    if($show){

        $method = Mage::getModel('shipping/rate_result_method');
        $method->setCarrier($this->_code);
        $method->setMethod($this->_code);
        $method->setCarrierTitle($this->getConfigData('title'));
        $method->setMethodTitle($this->getConfigData('name'));
        $method->setPrice($price); // Set shipping price here
        $method->setCost($price);
        $result->append($method);

    }else{
        $error = Mage::getModel('shipping/rate_result_error');
        $error->setCarrier($this->_code);
        $error->setCarrierTitle($this->getConfigData('name'));
        $error->setErrorMessage($this->getConfigData('specificerrmsg'));
        $result->append($error);
    }
    return $result;

Upvotes: 1

rosh3000
rosh3000

Reputation: 309

I used a different solution "should" work (and add it to grand total):

Remove:

->setShippingAmount('10')
->setBaseShippingAmount('10');

Add the following line to your adding order script:

Mage::register('shipping_cost', $shippingPrice); 

And, replace the following line in your shipping method:

$method->setPrice('0.00');
$method->setCost('0.00');

with:

if(Mage::registry('shipping_cost')){
    $method->setPrice(Mage::registry('shipping_cost'));
    $method->setCost(Mage::registry('shipping_cost'));
} else {
    $method->setPrice('0.00');
    $method->setCost('0.00');
}

p.s I tested it in Magento 1.6.1, PS you may also need to remove ->setShippingAmount('10') from your order obj or you will add the tax twice

Upvotes: 2

Garp Informatica
Garp Informatica

Reputation: 536

Some solution found.

It was simple, just add shipping amount in the order obj as follow:

        $order->setShippingAmount($shippingprice);
        $order->setBaseShippingAmount($shippingprice);

Now i have a new error, the shipping costs are not added to the grantotal...

Upvotes: 1

Related Questions