我创建了一个在Magento中覆盖discount_amount的扩展程序

时间:2015-04-28 15:04:05

标签: php magento

我正在使用Magento 1.9.0.1。

我创建了一个新的扩展程序,现在当我运行此功能时:

     $coupon = Mage::getModel('salesrule/rule');
        $couponCollection = $coupon->getCollection();
        foreach($couponCollection as $c){
            $CouponDiscount = $c->getDiscountAmount();
        }

变量$CouponDiscount应该给我使用优惠券代码的折扣金额。 但不是这样,它会从我的新分机中获得金额。

这是我的新扩展程序和我认为重要的文件:

/app/code/local/VivasIndustries/PercentPayment/etc/config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_PercentPayment>
      <version>0.1.0</version>
    </VivasIndustries_PercentPayment>
  </modules>
      <adminhtml>
        <layout>
            <updates>
                <percentpayment>
                    <file>percentpayment.xml</file>
                </percentpayment>
            </updates>
        </layout>
    </adminhtml> 
  <global>
        <models>
      <percentpayment>
        <class>VivasIndustries_PercentPayment_Model</class>
        <resourceModel>percentpayment_mysql4</resourceModel>
      </percentpayment>
    </models>
    <helpers>
    <percentpayment>
      <class>VivasIndustries_PercentPayment_Helper</class>
    </percentpayment>
    </helpers>
    <resources>
      <percentpaymentatribute_setup>
        <setup>
          <module>VivasIndustries_PercentPayment</module>
          <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </percentpaymentatribute_setup>
      <percentpaymentatribute_write>
        <connection>
          <use>core_write</use>
        </connection>
      </percentpaymentatribute_write>
      <percentpaymentatribute_read>
        <connection>
          <use>core_read</use>
        </connection>
      </percentpaymentatribute_read>
    </resources>
    <events>
    <sales_order_place_after>
        <observers>
            <clear_session>
                <class>VivasIndustries_PercentPayment_Model_Observer</class>
                <method>clearCheckoutSession</method>
            </clear_session>
        </observers>
    </sales_order_place_after>
    <checkout_type_onepage_save_order_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_type_onepage_save_order_after_discount_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>percentpayment/newordertotalobserver</class> <!-- observers class alias -->
            <method>saveDiscountTotal</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_type_onepage_save_order_after_discount_handler>
        </observers>
      </checkout_type_onepage_save_order_after>     
    <checkout_type_multishipping_create_orders_single> <!-- identifier of the event we want to catch -->
        <observers>     
          <checkout_type_multishipping_create_orders_single_discount_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>percentpayment/newordertotalobserver</class> <!-- observers class alias -->
            <method>saveDiscountTotalForMultishipping</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_type_multishipping_create_orders_single_discount_handler>      
        </observers>
      </checkout_type_multishipping_create_orders_single>
        <sales_model_service_quote_submit_after>
            <observers>
                <clear_session>
                    <class>VivasIndustries_PercentPayment_Observer</class>
                    <method>clearCheckoutSession</method>
                </clear_session>
            </observers>
        </sales_model_service_quote_submit_after>
    </events>   
     <sales>
        <quote>
            <totals>                
                <discount_total>
                    <class>percentpayment/quote_address_total_discount</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </discount_total> 
            </totals>
        </quote>
            <order_invoice>
                <totals>                
                <discount_total>
                    <class>percentpayment/order_invoice_total_discount</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </discount_total> 
                </totals>
            </order_invoice>
            <order_creditmemo>
                <totals>                
                <discount_total>
                    <class>percentpayment/order_creditmemo_total_discount</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </discount_total> 
                </totals>
            </order_creditmemo>
    </sales>
  </global>
</config>  

这是:/app/code/local/VivasIndustries/PercentPayment/Model/Quote/Address/Total/Discount.php

<?php
class VivasIndustries_PercentPayment_Model_Quote_Address_Total_Discount extends Mage_Sales_Model_Quote_Address_Total_Abstract
{

     public function __construct()
    {
         $this -> setCode('discount_total');
         }
    /**
     * Collect totals information about discount
     * 
     * @param Mage_Sales_Model_Quote_Address $address 
     * @return Mage_Sales_Model_Quote_Address_Total_Shipping 
     */

     public function getPercSmall()
    {

        $EnableDiscount = Mage::getStoreConfig('checkout/percentpayment/active');
         $MinPrice = Mage::getStoreConfig('checkout/percentpayment/percentpayment_min_amt');
         $MaxPrice = Mage::getStoreConfig('checkout/percentpayment/percentpayment_max_amt');
         $quote1 = Mage::getModel('checkout/session')->getQuote();
         $quoteData= $quote1->getData();
         $SessionGrandTotal = $quoteData['grand_total'];



        return ($EnableDiscount==1 && Mage::getStoreConfig('checkout/percentpayment/discountusergroup') == Mage::getSingleton('customer/session')->getCustomerGroupId() && $MinPrice <= $SessionGrandTotal && $MaxPrice >= $SessionGrandTotal);
    }


    public function SetPercSession(){

    Mage::getSingleton('core/session')->setPcPayment('pc_payment');

    $PcPay = Mage::getSingleton('core/session')->getPcSession();

        if($PcPay == ""){
            Mage::getSingleton('core/session')->setPcSession('no_redirect');
            header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
            exit;
        }

    }

    public function UnsPercSession(){
    Mage::getSingleton('core/session')->unsPcPayment();
    $PcPay = Mage::getSingleton('core/session')->getPcSession();
        if($PcPay != ""){
        Mage::getSingleton('core/session')->unsPcSession();
            header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
            exit;

        }
    }   


     public function getPerc()
    {

        $EnableDiscount = Mage::getStoreConfig('checkout/percentpayment/active');
         $MinPrice = Mage::getStoreConfig('checkout/percentpayment/percentpayment_min_amt');
         $MaxPrice = Mage::getStoreConfig('checkout/percentpayment/percentpayment_max_amt');
         $customer_id = Mage::getSingleton('customer/session')->getId();

         $SessionGrandTotal = Mage::getModel('sales/quote')->loadByCustomer($customer_id)->getGrandTotal();

        $PcPayment = Mage::getSingleton('core/session')->getPcPayment();

        return ($MinPrice <= $SessionGrandTotal && $MaxPrice >= $SessionGrandTotal && $PcPayment == 'pc_payment');
    }

     public function collect(Mage_Sales_Model_Quote_Address $address)
    {
         parent :: collect($address);
         $items = $this->_getAddressItems($address);
         if (!count($items)) {
            return $this;
         }
         $quote= $address->getQuote();



         //amount definition


         if($this->getPerc())
            {




         $discount = Mage::getStoreConfig('checkout/percentpayment/percentpayment_percent');
         $setFee = Mage::getStoreConfig('checkout/fee/active');

         $customer_id = Mage::getSingleton('customer/session')->getId();
         $SessionGrandTotal = Mage::getModel('sales/quote')->loadByCustomer($customer_id)->getSubtotal() + VivasIndustries_PercentShipping_Model_Quote_Address_Total_Discount::getShippingPercent();


         $discountAmount = (-$SessionGrandTotal * $discount / 100);


            $address->setDiscountAmount($discountAmount);
            $address->setBaseDiscountAmount($discountAmount);

            $quote->setDiscountAmount($discountAmount);      

         //amount definition

         $discountAmount = $quote -> getStore() -> roundPrice($discountAmount);
         $this -> _setAmount($discountAmount) -> _setBaseAmount($discountAmount);
         $address->setData('discount_total',$discountAmount);

         return $this;
         }
     }

    /**
     * Add discount totals information to address object
     * 
     * @param Mage_Sales_Model_Quote_Address $address 
     * @return Mage_Sales_Model_Quote_Address_Total_Shipping 
     */
     public function fetch(Mage_Sales_Model_Quote_Address $address)
    {

         $EnableDiscount = Mage::getStoreConfig('checkout/percentpayment/active');
         $MinPrice = Mage::getStoreConfig('checkout/percentpayment/percentpayment_min_amt');
         $MaxPrice = Mage::getStoreConfig('checkout/percentpayment/percentpayment_max_amt');
         $customer_id = Mage::getSingleton('customer/session')->getId();

         $SessionGrandTotal = Mage::getModel('sales/quote')->loadByCustomer($customer_id)->getGrandTotal();

         if($this->getPerc())
            {
         parent :: fetch($address);
         $amount = $address -> getTotalAmount($this -> getCode());
         if ($amount != 0){
             $address -> addTotal(array(
                     'code' => $this -> getCode(),
                     'title' => $this -> getLabel(),
                     'value' => $amount
                    ));
         }

         return $address;
         }
     }

     public function getLabel()
    {
         return Mage::helper('percentpayment')->__('50% Предплата');
    }

    /**
     * Get label
     * 
     * @return string 
     */

}

以下是我的内容:/app/code/local/VivasIndustries/PercentPayment/Model/Newordertotalobserver.php:

<?php
class VivasIndustries_PercentPayment_Model_Newordertotalobserver
{
     public function saveDiscountTotal(Varien_Event_Observer $observer)
    {
         $order = $observer -> getEvent() -> getOrder();
         $quote = $observer -> getEvent() -> getQuote();
         $shippingAddress = $quote -> getShippingAddress();
         if($shippingAddress && $shippingAddress -> getData('discount_total')){
             $order -> setData('discount_total', $shippingAddress -> getData('discount_total'));
             }
        else{
             $billingAddress = $quote -> getBillingAddress();
             $order -> setData('discount_total', $billingAddress -> getData('discount_total'));
             }
         $order -> save();
     }

     public function saveDiscountTotalForMultishipping(Varien_Event_Observer $observer)
    {
         $order = $observer -> getEvent() -> getOrder();
         $address = $observer -> getEvent() -> getAddress();
         $order -> setData('discount_total', $shippingAddress -> getData('discount_total'));
         $order -> save();
     }
}

以下是我所拥有的:/app/code/local/VivasIndustries/PercentPayment/sql/percentpaymentatribute_setup/mysql4-install-0.1.0.php:

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("quote_address", "discount_total", array("type"=>"varchar"));
$installer->addAttribute("order", "discount_total", array("type"=>"varchar"));
$installer->endSetup();

请告诉我在新扩展中需要更改的内容,以便可以达到这两个变量。我希望使用默认的getDiscountAmount()功能来应用优惠券代码的金额。

你能告诉我在新的扩展中我需要改变什么吗?

1 个答案:

答案 0 :(得分:1)

快一点......

    $coupon = Mage::getModel('salesrule/rule');
    $couponCollection = $coupon->getCollection();
    $CouponDiscount = 0
    foreach($couponCollection as $c){
        $CouponDiscount += $c->getDiscountAmount();
    }

快速浏览一下,您将$ CouponDiscount重置为foreach最后一次迭代所指示的内容。当你好像想要获得总折扣时。我能正确理解你的问题吗?

在您的自定义模块之外,您可以检索每件商品的折扣金额值。

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

foreach ($quote->getAllItems() as $item){

     echo $item->getDiscountAmount();

} 

折扣保存在discount_amount和base_discount_amount列下的sales_flat_quote_item表中