1

I'm using Mangeto 1.9.1 and i'm trying to create a module for 50% discount from the grandtotal price.

I've made some things to the module but when i try to open my http://mymagento.com/checkout/cart/ i've got blank page bellow my main menu. Blank page where my cart items and total should be.

Take a look: https://i.stack.imgur.com/Y5Vtf.png

I've made this module by this answer: https://stackoverflow.com/questions/26831818/magento-issue-with-creating-a-custom-module/26840374#26840374

So here is what i've done:

In 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>
        <models>
      <percentpayment>
        <class>VivasIndustries_PercentPayment_Model</class>
        <resourceModel>percentpayment_mysql4</resourceModel>
      </percentpayment>
    </models>
    <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>
    <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>  

In In 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();
     }
}

In app/code/local/VivasIndustries/PercentPayment/Model/Order/Creditmemo/Total/Discount.php :

        return $this;

        $order = $creditmemo->getOrder();
        $orderDiscountTotal        = $order->getDiscountTotal();

        if ($orderDiscountTotal) {
            $creditmemo->setGrandTotal($creditmemo->getGrandTotal()+$orderDiscountTotal);
            $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal()+$orderDiscountTotal);
        }

        return $this;
    }
}

In app/code/local/VivasIndustries/PercentPayment/Model/Order/Invoice/Total/Discount.php :

<?php
    class VivasIndustries_PercentPayment_Model_Order_Invoice_Total_Discount
    extends Mage_Sales_Model_Order_Invoice_Total_Abstract
    {
        public function collect(Mage_Sales_Model_Order_Invoice $invoice)
        {
            $order=$invoice->getOrder();
            $orderDiscountTotal = $order->getDiscountTotal();
            if ($orderDiscountTotal&&count($order->getInvoiceCollection())==0) {
                $invoice->setGrandTotal($invoice->getGrandTotal()+$orderDiscountTotal);
                $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal()+$orderDiscountTotal);
            }
            return $this;
        }
    }

In 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 collect(Mage_Sales_Model_Quote_Address $address)
    {
         parent :: collect($address);
         $items = $this->_getAddressItems($address);
         if (!count($items)) {
            return $this;
         }
         $quote= $address->getQuote();

         //amount definition

         $discountAmount = 50;

         //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)
    {
         parent :: fetch($address);
         $amount = $address -> getTotalAmount($this -> getCode());
         if ($amount != 0){
             $address -> addTotal(array(
                     'code' => $this -> getCode(),
                     'title' => $this -> getLabel(),
                     'value' => $amount
                    ));
         }

         return $this;
     }

    /**
     * Get label
     * 
     * @return string 
     */
     public function getLabel()
    {
         return Mage :: helper('modulename') -> __('Discount');
    }
}

And in 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();

And finaly i've made this:

In app/etc/modules/VivasIndustries_PercentPayment.xml :

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

And that's all what i've done and i get this blank page with no error.

Can you please help me find the problem and fix it ?

*EDIT

WHen i remove:

 public function getLabel()
{
     return Mage :: helper('modulename') -> __('Discount');
}

Thanks in advance!

6
  • 1
    Did you check your php error logs? Nov 10, 2014 at 15:10
  • Try to temporarily remove your observers from the config.xml file. Does the problem still exists? If no, try to add one-by-one the observer declarations. Also add something like die('111111') in your installer script. It should be skipped if the module has been already installed. Nov 10, 2014 at 15:23
  • Check out this excellent post from Sonassi on debugging Magento. It should at least give you some more info to pass on to us so we can help you fix the issue sonassi.com/knowledge-base/magento-debug-process Nov 10, 2014 at 16:07
  • Please checkout again my question, i've made an edit. I think i found from where the problem comes. It seems i have not created a blank helper but i don't know how and what should i put there. Is that the problem, and how i can fix that ? Nov 10, 2014 at 16:45
  • @TheLex to create a helper please put in your config.xml's global section: <helpers><percentpayment><class>VivasIndustries_PercentPayment _Helper</class></percentpayment></helpers> and create a Helper directory and put there Data.php with empty class like 'class VivasIndustries_PercentPayment_Helper_Data extends Mage_Core_Helper_Abstract {}' Nov 10, 2014 at 16:50

1 Answer 1

1

To create a helper please:

  1. Put in your config.xml's global section:

    <helpers>
       <percentpayment>
          <class>VivasIndustries_PercentPayment_Helper</class>
       </percentpayment>
    </helpers>
    
  2. Create a Helper subdirectory and put there Data.php with empty class:

    <?php
    class VivasIndustries_PercentPayment_Helper_Data extends Mage_Core_Helper_Abstract {}
    
  3. Use it:

    return Mage::helper('percentpayment')->__('Discount');
    

P.S. It is very helpful to turn on the error logging to understand a possible errors behind the blank screens.

4
  • Everything now is okey, but do i have to put something in this helper so when the order is completed and i open the order details in the admin panel i'll be able to see "Discout" row ? Nov 10, 2014 at 17:57
  • @TheLex You have to add layout update to the adminpanel's sales_order_view and add your block there. Nov 10, 2014 at 18:16
  • How i can make this thing? I'm very new to magento. Can you help me with this last thing ? Nov 10, 2014 at 20:17
  • It would be better to post it as a separate question since the 'Blank page' problem is not related to this last stuff.. Nov 10, 2014 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.