Magento - 创建自定义模块的问题

时间:2014-11-09 18:38:11

标签: php magento

我正在使用Magento 1.9.1而我正在尝试创建一个模块,该模块可以从总计中获得50%的折扣。

这是我的代码:

/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:

<?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:

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

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

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

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

当我打开我的error_log时,我可以看到这件事:

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

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

修改总数一直是困难的部分。除非我在我的系统中设置了该模块,否则很难找到代码中的问题。无论如何,我在分享过程中共享了一些修改总数的代码。我尽可能地解释了。

为此你需要更新很多东西。 报价,地址,订单,Creditmemo,发票,运费,多种运费等等。好的,我们走了, 首先,我们需要创建一个名为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 open core_resources表转到您的数据库。并删除您的模块条目(如果有)。就是这样。
如果您有任何疑问,请在此发表评论。