Magento-创建自定义模块的问题 [英] Magento - Issue with creating a custom Module

查看:54
本文介绍了Magento-创建自定义模块的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Magento 1.9.1,并且试图创建一个模块,该模块可以提供总计50%的折扣.

I'm using Magento 1.9.1 and i'm trying to create a module which is giving a 50% discount from the grand total.

这是我的代码:

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

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

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_PercentPayment>
      <version>0.1.0</version>
    </VivasIndustries_PercentPayment>
  </modules>
    <global>
    <sales>
                <quote>
                    <totals>
                        <discount>
                            <class>VivasIndustries_PercentPayment_Model_Discount</class>
                            <after>subtotal</after>
                        </discount>
                    </totals>
                </quote>

                <order_invoice>
                    <totals>
                        <discount>
                            <class>VivasIndustries_PercentPayment_Model_Invoice</class>
                            <after>subtotal</after>
                        </discount>
                    </totals>
                </order_invoice>

                <order_creditmemo>
                    <totals>
                        <discount>
                            <class>VivasIndustries_PercentPayment_Model_Creditmemo</class>
                            <after>subtotal</after>
                        </discount>
                    </totals>
                </order_creditmemo>
            </sales>
    </global>
</config> 

/app/code/local/VivasIndustries/PercentPayment/controllers/IndexController.php:

/app/code/local/VivasIndustries/PercentPayment/controllers/IndexController.php :

<?PHP
class VivasIndustries_PercentPayment_Model_Discount extends Mage_Sales_Model_Quote_Address_Total_Abstract {

    public function collect(Mage_Sales_Model_Quote_Address $address) {
            if ($address->getData('address_type') == 'billing')
                return $this;

    $discount = 50; //your discount percent

                $grandTotal = $address->getGrandTotal();
                $baseGrandTotal = $address->getBaseGrandTotal();

                $totals = array_sum($address->getAllTotalAmounts());
                $baseTotals = array_sum($address->getAllBaseTotalAmounts());

                $address->setFeeAmount(-$totals * $discount / 100);
                $address->setBaseFeeAmount(-$baseTotals * $discount / 100);

                $address->setGrandTotal($grandTotal + $address->getFeeAmount());
                $address->setBaseGrandTotal($baseGrandTotal + $address->getBaseFeeAmount());
        return $this;
    }

    public function fetch(Mage_Sales_Model_Quote_Address $address) {

            if ($address->getData('address_type') == 'billing')
                return $this;

            $amt = $address->getDiscountAmount();
            if ($amt != 0) {


                $address->addTotal(array(
                    'code' => 'Discount',
                    'title' => 'Discount',
                    'value' => $amt
                ));
            }

        return $address;
    }

}
?>

/app/etc/modules/VivasIndustries_PercentPayment.xml:

/app/etc/modules/VivasIndustries_PercentPayment.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <VivasIndustries_PercentPayment>
        <active>true</active>
        <codePool>local</codePool>
        </VivasIndustries_PercentPayment>
    </modules>
</config>

这是我为新模块创建的所有文件.我所做的一切.

This are all files which i've created for my new module. All what i've done.

我正在使用此指南: http://magento.ikantam. com/qa/how-add-discount-total-magento

现在,当我尝试打开结帐页面时,出现错误-There has been an error processing your request

Now when i try to open my checkout page i got error - There has been an error processing your request

当我打开error_log时,我可以看到以下内容:

When i open my error_log i can see this thing:

[09-Nov-2014 18:22:35 UTC] CSRF state token does not match one provided.

能帮我解决这个问题,并使新模块按预期工作吗?

Can you please help me fix this problem and make my new Module works like intended ?

提前谢谢!

推荐答案

修改总数始终是困难的部分.除非我在系统中设置了该模块,否则很难在您的代码中找到问题所在.无论如何,我在这里共享了一些代码,用于在结帐过程中修改总计.我尽我所能解释.

Modifying total is always difficult part. And it's hard find out the problem in your code unless I have set up of that module in my system. Anyway here I have shared some codes for modifying total during checkout process. I explained as much I can.

为此,您必须更新很多东西. 报价,地址,订单,Creditmemo,发票,运输,多次运输等.好的,我们开始吧, 首先,我们需要创建名为config.xml的配置文件, config.xml

For this you have to update lot of things. Quote, Address, Order, Creditmemo , Invoice, shipping, multi shipping etc. Okay here we go, First we need to create our configuration file that's called config.xml, config.xml

更新:

<config>
  <modules>
    <VivasIndustries_PercentPayment>
      <version>0.1.0</version>
    </VivasIndustries_PercentPayment>
  </modules>
  <global>
    <helpers>
      <percentpayment>
        <class>VivasIndustries_PercentPayment_Helper</class>
      </percentpayment>
    </helpers>
    <models>
      <percentpayment>
        <class>VivasIndustries_PercentPayment_Model</class>
        <resourceModel>percentpayment_mysql4</resourceModel>
      </percentpayment>
    </models>
    <resources>
      <salesattribute1416562342_setup>
        <setup>
          <module>VivasIndustries_PercentPayment</module>
          <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </salesattribute1416562342_setup>
      <salesattribute1416562342_write>
        <connection>
          <use>core_write</use>
        </connection>
      </salesattribute1416562342_write>
      <salesattribute1416562342_read>
        <connection>
          <use>core_read</use>
        </connection>
      </salesattribute1416562342_read>
    </resources>
    <events>
    <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>
    </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> 

根据此配置更改其他内容.在此之前,请使用phpmyadmin打开core_resources表进入数据库.并删除您的模块条目(如果有的话).就是这样.
如有任何疑问,请在这里评论.

change other things as per this configuration. before that go to your database using phpmyadmin open core_resources table. and delete your module entry if it is there. that's it.
Please comment here, if you have any doubt.

这篇关于Magento-创建自定义模块的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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