Magento:以编程方式设置免费送货

时间:2013-05-13 00:32:42

标签: magento

我正在编写一个模块,我需要在某些情况下免费提供报价。我想让这个工作:

$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
$quote->getShippingAddress()->setFreeShipping(1);
但到目前为止还没有成功。有人可以帮忙吗?

编辑:我需要在以下事件中执行此操作:

checkout_cart_update_items_after
checkout_cart_product_add_after
sales_quote_remove_item

基本上在购买任何购物车后,我需要确定运费是否应免费。

编辑:这看起来效果很好。除了我需要禁用免费送货作为结账时的选择。

$address = $quote->getShippingAddress();
$address->setShippingMethod('freeshipping_freeshipping');

4 个答案:

答案 0 :(得分:4)

您可以先确定要为其设置免费送货的活动,并根据该活动设置免费送货作为默认选项。

下面的代码可能对您有所帮助。

app / design / frontend / default / default / template / checkout / onepage / shipping_method / available.phtml:

// find methods loop:
 <?php foreach ($_rates as $_rate): ?>

//  add checking for free shipping method and setting it as default
<?php if ($_rate->getCode()=='freeshipping_freeshipping' && !$this->getAddress()->getShippingMethod()) {
      $this->getAddress()->setShippingMethod($_rate->getCode());
} ?>

答案 1 :(得分:3)

执行此操作的最佳方法是添加您自己的“引用地址总计”,方法与Mage_SalesRule_Model_Quote_Freeshipping类在SalesRule模块中完成相同。要复制它,您需要创建一个扩展Mage_Sales_Model_Quote_Address_Total_Abstract的类,并在其中创建一个名为collect()的方法,如下所示:

class Mycompany_Mymod_Model_Sales_Quote_Address_Total_Freeshipping extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        parent::collect($address);
        if(somecondition){
            $address->setFreeShipping(true);
        }
    }
}

然后在模块config.xml中声明自定义“引用地址总数”:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <global>
        <sales>
            <quote>
                <totals>
                    <customfreeshipping>
                        <class>mymodname/sales_quote_address_total_freeshipping</class>
                        <after>freeshipping</after> <!-- this ensures our custom total is placed after Mage_SalesRule_Model_Quote_Freeshipping so we can override "$address->setFreeShipping(0);" -->
                    </customfreeshipping>
                </totals>
            </quote>
        </sales>
    </global>
</config>

这将声明地址对象的免费送货,该地址对象将$request->setFreeShipping($this->getFreeShipping()); Mage_Sales_Model_Quote_AddressrequestShippingRates()内的$request->getFreeShipping()传递给请求对象,然后可用于任何送货模块正在检查Map<Date, PottyCollection> map = new HashMap<>(); for (PottyCollection pc : originalList) { PottyCollection existing = map.get(pc.dateHappened); if (existing == null) { map.put(pc.dateHappened, pc); } else { map.put(pc.dateHappened, new PottyCollection(pc.dateHappened, pc.pottyUses + existing.pottyUses)); } } Collection<PottyCollection> reduced = map.values();

享受:)

答案 2 :(得分:1)

你不应该保存报价吗?

$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
$quote->getShippingAddress()->setFreeShipping(1);
$quote->save();

答案 3 :(得分:0)

我有以下Magento 2错误

  

请指定送货方式

这段代码解决了这个问题:

$shippingAddress->setCollectShippingRates(true)
                ->collectShippingRates()
                ->setShippingMethod('flatrate_flatrate');

同时检查来自Unable to set Shipping Method in Magento order create script

的正确解决方案