使用按钮总计添加自定义折扣订单 [英] Add custom discount order in total with button

查看:63
本文介绍了使用按钮总计添加自定义折扣订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的自定义折扣模块,没关系.

I have my module with my custom discount and it's OK.

config.xml:

config.xml:

<sales>
   <quote>
       <totals>
             <aver> 
                <class>Dani_Prueba_Model_Total_Aver</class> 
                <after>subtotal</after> 
             </aver>
        </totals>
    </quote>
</sales>

我的模块:

<?php
class Dani_Prueba_Model_Total_Aver extends Mage_Sales_Model_Quote_Address_Total_Abstract{

    public function collect(Mage_Sales_Model_Quote_Address $address){

        $baseDiscount = 2.5;
        $discount = Mage::app()->getStore()->convertPrice($baseDiscount);

        $address->setCustomDiscount($baseDiscount);

        $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);
        $address->setGrandTotal($address->getGrandTotal() - $discount);

        return $this;
    }

    public function fetch(Mage_Sales_Model_Quote_Address $address){
        $this->setCode('aver');
        $amount = $address->getCustomDiscount();
            if ($amount != 0){
                $address->addTotal(array(
                'code'  => $this->getCode(),
                'title' => 'Custom Discount',
                'value' => $amount
            ));
        }
        return $this;
    }
}

这没关系,当我将产品添加到购物车时,会自动应用我的自定义折扣.

This it is OK and when I add a product to cart, automatically apply my custom discount.

但是现在我需要用一个按钮来做.当我将产品添加到购物车时,请不要应用折扣并拥有正确的总额.但是,当我单击一个按钮时,请应用我的自定义折扣,然后使用其他按钮取消",取消该折扣.我需要类似优惠券代码的功能.

But now I need do it with a button. When I add products to cart not apply discount and have the correct total. But when I click a button, apply my custom discount, and with other button "Cancel", cancel the discount. I need some similar like the function a coupon code.

我该怎么做?

推荐答案

为此,您将需要向sales/quote表(可能还有sales/order表)添加另一个属性/列.

To do this, you will need to add another attribute/column to the sales/quote table (and possibly sales/order table).

因此,在您的安装脚本中,执行此操作(我也包括了sales/order table/entity属性):

So, in your install script, execute this (I included the sales/order table/entity attribute as well):

$installer->addAttribute('order', 'use_special_coupon', array('type' => 'int', 'grid' => true, 'source' => 'adminhtml/system_config_source_yesno'));
$installer->addAttribute('quote', 'use_special_coupon', array('type' => 'int', 'grid' => true, 'source' => 'adminhtml/system_config_source_yesno'));
$installer->getConnection()->addColumn($installer->getTable('sales/quote'), 'use_special_coupon', 'TINYINT(1) unsigned default 0');
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'use_special_coupon', 'TINYINT(1) unsigned default 0');

然后,在您的控制器中,执行以下操作:

Then, in your controller, do something like this:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote
    ->setUseSpecialCoupon(true)
    ->save();

或者相反,在您的removeAction中:

Or, the opposite, in your removeAction:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote
    ->setUseSpecialCoupon(false)
    ->save();

然后,最后,在您的总体模型中,将其修改为:

And, then finally, in your total model, modify it to be this:

<?php
class Dani_Prueba_Model_Total_Aver extends Mage_Sales_Model_Quote_Address_Total_Abstract{

    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        if ($address->getQuote()->getUseSpecialCoupon()) {
            $baseDiscount = 2.5;
            $discount = Mage::app()->getStore()->convertPrice($baseDiscount);

            $address->setCustomDiscount($baseDiscount);

            $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);
            $address->setGrandTotal($address->getGrandTotal() - $discount);
        }
        return $this;
    }

    public function fetch(Mage_Sales_Model_Quote_Address $address){
        if ($address->getQuote()->useSpecialCoupon()) {            
            $this->setCode('aver');
            $amount = $address->getCustomDiscount();
            if ($amount != 0){
                $address->addTotal(array(
                    'code'  => $this->getCode(),
                    'title' => 'Custom Discount',
                    'value' => $amount
                ));
            }
        }
        return $this;
    }
}

这篇关于使用按钮总计添加自定义折扣订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆