1

信息:Magento 1.7.0.2 CE

我已经制作了一个模块,用于通过 cron 计划从外部提要导入订单,我还创建了一个自定义运营商。

一切正常,但我无法设置运费......这是代码:

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

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

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

$addressData 包含客户信息

我尝试了不同的方法,但我无法设置运费。帮助!

这是自定义运营商代码:

    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'));
    }
}  
4

4 回答 4

3

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.

于 2017-12-05T05:36:45.620 回答
2

我使用了不同的解决方案“应该”工作(并将其添加到总数中):

消除:

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

将以下行添加到添加订单脚本中:

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

并且,在您的运输方式中替换以下行:

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

和:

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

于 2013-08-20T22:30:07.180 回答
1

找到了一些解决方案。

这很简单,只需在订单 obj 中添加运费金额,如下所示:

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

现在我有一个新错误,运费没有添加到grantotal ...

于 2013-06-25T11:44:02.057 回答
1

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;
于 2014-09-08T15:26:03.220 回答