2

我的问题:

我想在结帐页面开始之前使用观察者,因为根据购物车中的产品,如果它不符合某些条件,我想禁用运输。

我正在使用 [controller_action_predispatch_checkout_onepage_index]事件观察器,这基本上是在结帐页面开始加载之前调用...我能够获取所有产品和报价信息,但没有找到任何禁用运输的方法。

我在寻找什么,

从观察者那里,是否可以通过调用某些 magento 方法或任何其他解决方案来禁用运输?

覆盖 collectRates()

在收到很少的回复后,我尝试使用下面给出的代码覆盖 collectRates() 方法

$method = Mage::getModel('shipping/rate_result_method');

        $method->setCarrier('flatrate');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('flatrate');
        $method->setMethodTitle($this->getConfigData('name'));

        if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
            $shippingPrice = '0.00';
        }


        $method->setPrice($shippingPrice);
        $method->setCost($shippingPrice);

        $result->append($method);

虽然,我也不想启用统一运费方式。我只想禁用运输,或者选择回复类似

  • 免运费 $0.00

用户可以选择继续下一步。请从这里帮助我.. 我应该在 $method->setCarrier('??????'); 中使用什么 或者我需要在上面的代码中做哪些更改?

4

2 回答 2

1

我认为覆盖或子类化各个 Carrier 模型可能会更好。

所有运营商都实现了一个方法“Mage_Shipping_Model_Carrier_Abstract::collectRates()”来返回结果。可以在此方法中获取有关当前报价的信息以修改返回的费率/选项。

也就是说,如果有办法让观察者做到这一点,它可能会更清洁/更容易。

于 2013-01-27T19:15:49.373 回答
0

最后我有覆盖运输方法,它是一个两步代码,但如果你愿意,你可以将它减少到一步。这是我的两步解决方案。在我们开始之前,我们的运输类需要扩展和实现

extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface

现在,我们创建一个受保护的方法

protected function _createMethod($request, $method_code, $title, $price, $cost)
{
    $method = Mage::getModel('shipping/rate_result_method');

    $method->setCarrier('australiapost'); // in my case its australia post, it can be any other whatever you are using
    $method->setCarrierTitle($this->getConfigData('title'));

    $method->setMethod($method_code);
    $method->setMethodTitle($title);

    $method->setPrice($this->getFinalPriceWithHandlingFee($price));
    $method->setCost($cost);

    return $method;
}

现在只需使用下面的代码创建免费送货的新方法并绕过现有的运费计算器,此代码将进入 collectRates(Mage_Shipping_Model_Rate_Request $request)方法

   // PROCESS WILL RETURN FREE SHIPPING
   if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
            $shippingPrice = '0.00';
         }

    $shipping_method = 'Free Shipping';
    $method = $this->_createMethod($request, $shipping_method, 'Shipping Disabled', '0.00', '0.00');


         $result->append($method);

         return $result;

通过这样做,您可以在结帐时获得如下结果,用户可以轻松单击继续下一步。

  • 运输禁用 $0.00
于 2013-02-03T02:17:21.377 回答