【问题标题】:Magento Set Shipping cost when create order programmaticallyMagento 在以编程方式创建订单时设置运费
【发布时间】:2013-06-25 09:30:21
【问题描述】:

信息: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'));
    }
}  

【问题讨论】:

    标签: magento


    【解决方案1】:

    您可以通过以下代码行更新运费:-

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

    对你有帮助。

    【讨论】:

      【解决方案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 我在 Magento 1.6.1 中测试过,PS 你可能还需要从你的订单 obj 中删除 ->setShippingAmount('10') 否则你会加两次税

      【讨论】:

        【解决方案3】:

        找到了一些解决方案。

        很简单,只需在订单obj中添加运费如下:

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

        现在我有一个新错误,运费未添加到赠款中...

        【讨论】:

        • 总计不是小计和运费的总和。你找到解决这个问题的方法了吗?
        • 您是否找到了解决方案,遇到了同样的问题,设置并显示了价格,但没有正确计算总计。
        • 在您的流程结束时的某个地方,您应该有总计和总计部分,只需在此处添加您的运费即可。类似的东西:$order->setSubtotal($subTotal) ->setBaseSubtotal($subTotal) ->setGrandTotal($subTotalWithShipping) ->setBaseGrandTotal($subTotalWithShipping);
        【解决方案4】:

        请检查您的运输模块的 collectRates(Mage_Shipping_Model_Rate_Request $request) 方法。检查您的运费总额是如何初始化的。

        确保运费总额得到更新并保存在此方法的 $price 变量中。

            $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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-01
          • 1970-01-01
          • 2013-05-05
          • 1970-01-01
          相关资源
          最近更新 更多