Magento在一页结账时更改运费以获取COD

时间:2014-03-11 01:26:56

标签: php magento

我的问题很简单。一旦用户在一页结帐中选择COD作为付款方式,我需要将一页结账时的运费成本更改为“0”。我想我可以和观察者一起轻松地做到这一点。你能帮忙知道我应该观察哪些事件吗?

我可以使用checkout_controller_onepage_save_shipping_method吗?任何形式的帮助都将受到高度赞赏!!

OP作为回答发布的详细信息

我重写Mage_Checkout_Model_Type_Onepage类并修改savePayment方法。然后我这样做了:

1.-第一次尝试:

public function savePayment($data)
{
.....
....
$payment = $quote->getPayment();
$payment->importData($data);
$quote->save();

//My custom code
if($data['method']=='cashondelivery'){
        $shipping = $quote->getShippingAddress();
        $address = $quote->getBillingAddress();
        $shipping->addData($address)->setShippingMethod('flatrate_flatrate');
    }

我的flatrate_flatrate为0.不幸的是它效果不佳。此片段设置为0格兰特总计。

我的第二次尝试是:

public function savePayment($data)
{
.....
....
$payment = $quote->getPayment();
$payment->importData($data);
$quote->save();

//My custom code
if($data['method']=='cashondelivery'){
        $this->getQuote()->getShippingAddress()->setShippingAmount(0);
        $this->getQuote()->getShippingAddress()->setBaseShippingAmount(0);
    }

此代码有效,但只有几次,我不明白为什么?? ..有人可以帮忙解决这个问题吗?任何形式的帮助将受到高度赞赏。

亲切的问候!


确定。现在我有了这个并且它可以工作,但它不会更新总金额。即使订单也按正常运费处理。我的意思是,最后它不会更新任何东西。有人可以帮我解决这个问题吗?。

     if($data['method']=='cashondelivery'){
        $quote->getShippingAddress()->setShippingMethod('flatrate_flatrate');
        $quote->getShippingAddress()->setShippingAmount(0);
        $quote->getShippingAddress()->setBaseShippingAmount(0);
        $quote->getShippingAddress()->setShippingInclTax(0);
        $quote->getShippingAddress()->setBaseShippingInclTax(0);
        $quote->collectTotals(); 
        $quote->save();
    }

气候变暖!


好的,我做了一些疯狂的事情,但它似乎有效(至少部分)。我也覆盖Mage_Checkout_OnepageController并添加一个新的方法调用actualizaShippingMethodAction。

看起来像这样:

public function actualizaShippingMethodAction()
{
    if ($this->_expireAjax()) {
        return;
    }
    if ($this->getRequest()->isPost()) {
        $data = $this->getRequest()->getPost('shipping_method', '');
        $result = $this->getOnepage()->saveShippingMethod($data);
        // $result will contain error data if shipping method is empty
        if (!$result) {
            $this->getOnepage()->getQuote()->collectTotals();
        }
        $this->getOnepage()->getQuote()->collectTotals()->save();
    }
}

现在我的原始课程重写(Mage_Checkout_Model_Type_Onepage)我这样做了。

if($data['method']=='cashondelivery'){
        $cliente = new Varien_Http_Client();
        $_configuracion = array('maxredirects'=>5, 'timeout'=>30);
        $llamada = Mage::getBaseUrl()."checkout/onepage/actualizaShippingMethod/";
        $cliente->setUri($llamada)
        ->setConfig($_configuracion)
        ->setMethod(Zend_Http_Client::POST)
        ->setParameterPost('shipping_method','flatrate_flatrate');

        $respuesta = $cliente->request();
        $quote->getShippingAddress()->setShippingAmount(0);
        $quote->getShippingAddress()->setBaseShippingAmount(0);
        $quote->getShippingAddress()->setShippingInclTax(0);
        $quote->getShippingAddress()->setBaseShippingInclTax(0);
        $quote->collectTotals()->save();
    }

    $this->getCheckout()
        ->setStepData('payment', 'complete', true)
        ->setStepData('review', 'allow', true);

    return array();

现在订单没有按规定运费,但订单审核(摘要)并未更新最终费用。我认为这个解决方案有一个错误,因为如果用户返回并选择另一种付款方式,则运费再次为0。

任何形式的帮助,无论如何,都将受到高度赞赏,

谢谢

1 个答案:

答案 0 :(得分:0)

最后我有一个解决方案,我认为是原始问题的答案。如果没有请原谅我。

忘记在Mage_Checkout_Model_Type_Onepage上覆盖savePayment($ data)。只需专注于重写Mage_Checkout_OnepageController,并将以下代码添加到savePaymentAction()方法中。

            $data = $this->getRequest()->getPost('payment', array());

        /*Custom code*/
        if($data['method']=='cashondelivery'){//Only if payment method is cashondelivery
            $result = $this->getOnepage()->savePayment($data);
            // get section and redirect data
            $redirectUrl = $this->getOnepage()->getQuote()->getPayment()->getCheckoutRedirectUrl();
            if (empty($result['error']) && !$redirectUrl) {
                $this->loadLayout('checkout_onepage_review');
                $result['goto_section'] = 'review';
                $result['update_section'] = array(
                    'name' => 'review',
                    'html' => $this->_getReviewHtml()
                );
            }
            if ($redirectUrl) {
                $result['redirect'] = $redirectUrl;
            }

            //Update totals for Shipping Methods
            $resultado = $this->getOnepage()->saveShippingMethod('flatrate_flatrate');
            $this->getOnepage()->getQuote()->collectTotals()->save();


        }else{

            $result = $this->getOnepage()->savePayment($data);
            // get section and redirect data
            $redirectUrl = $this->getOnepage()->getQuote()->getPayment()->getCheckoutRedirectUrl();
            if (empty($result['error']) && !$redirectUrl) {
                $this->loadLayout('checkout_onepage_review');
                $result['goto_section'] = 'review';
                $result['update_section'] = array(
                    'name' => 'review',
                    'html' => $this->_getReviewHtml()
                );
            }
            if ($redirectUrl) {
                $result['redirect'] = $redirectUrl;
            }

            //Return the original values according with the initial user sellection
            //I use a session variable to store the original selection
            $shippingMethod = Mage::getSingleton('core/session')->getShippingInicial();
            $resultado = $this->getOnepage()->saveShippingMethod($shippingMethod);
            $this->getOnepage()->getQuote()->collectTotals()->save();

        }


    } catch (Mage_Payment_Exception $e) {
        if ($e->getFields()) {
  ...

然后你必须覆盖saveShippingMethodAction()并添加以下代码。

...

 $result = $this->getOnepage()->saveShippingMethod($data);

        //We store the Shipping Method initial selection

        Mage::getSingleton('core/session')->setShippingInicial($data);

        // $result will contain error data if shipping method is empty
        if (!$result) { ...

如果您对此进行测试,您会发现它按预期工作。但现在需要2个加载时间才能正确显示评论部分的总成本。我读过这个问题(IWD one page checkout extension Magento: review refresh),但它没有多大帮助。

有人可以指出我正确的方向两次更新评论部分吗?。

温暖的注意!

有人可以知道如何实现这个目标吗?此致!