How to Override Coupon Grid for Magento 1

posted by 1 year ago and updated 1 year ago

Note: This tutorial assumes prior knowledge into building modules, only parts of the code are included to solve a particular problem.

Magento comes a coupon generator that is fairly easy to set up. Distributing voucher codes can increase your sales and create a better customer experience.

To get to the grid of generated coupons, we need to navigate in our admin panel to: Promotions -> Shopping Basket Price Rules and click on Manage Coupon Codes from the left tabs. When it comes to managing the promo codes though we can run into headaches.

How do we extend the coupon grid to include the order that the voucher was used and the customer’s information?

How to extend the coupon grid by rewrite

To overwrite the grid, we first need to rewrite the grid into our own class. In app/code/local/Underthecocotree/Subscribers/etc/config.xmlconfig.xml of your module:

<global>
	<blocks>
		<adminhtml>
        <rewrite>
<promo_quote_edit_tab_coupons_grid>Underthecocotree_Subscribers_Block_Adminhtml_Promo_Quote_Edit_Tab_Coupons_Grid</promo_quote_edit_tab_coupons_grid>
        </rewrite>
    </adminhtml>
	</blocks>
</global>

<promo_quote_edit_tab_coupons_grid/> refers to the grid as it is defined by magento. Underthecocotree_Subscribers_Block_Adminhtml_Promo_Quote_Edit_Tab_Coupons_Grid is the class we are going to create for our module that overrides the original grid class.

Override the grid class

Create a class in your module app/code/local/Underthecocotree/Subscribers/Block/Adminhtml/Promo/Quote/Edit/Tab/Coupons/Grid.php

<?php

class Underthecocotree_Subscribers_Block_Adminhtml_Promo_Quote_Edit_Tab_Coupons_Grid extends Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Coupons_Grid
{
  protected function _prepareCollection()
  {

  }

  protected function _prepareColumns()
  {

  }
}

The above contains the shell of the class. We are extending the original class that handles the grid Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Coupons_Grid to include it’s original functionality. To change the grid we need to change the way the grid collection works by changing the _prepareCollection() function. We also need to add new columns by implementing _prepareColumns()

Implementing prepare collection

We first need to load the correct data for our grid to use, without it we won’t be able to display it in columns.

protected function _prepareCollection()
{
	// from the original class
   $collection = Mage::getResourceModel('salesrule/coupon_collection')
      ->addRuleToFilter($priceRule)
      ->addGeneratedCouponsFilter();

	// get the sales_flat_order table name
   $sales_flat_order_table = Mage::getSingleton('core/resource')->getTableName('sales/order');

   // match coupon to order by joining the current table
   $collection->getSelect()
      ->joinLeft(array('sales' => $sales_flat_order_table),
         'sales.coupon_code = main_table.code', // join tables on these fields
         array('increment_id', 'customer_email') // select these columns from the sales table

	// set the collection
   $this->setCollection($collection);

   // return $this, parent::prepareCollection() was not filtering
   return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}

We have included lots of comments to help you understand the code. We are:

  • Loading the collection of coupons
  • Figure out the sales/order table name
  • We join the salesrule_coupon table with the sales_flat_order table based on the coupon
  • We set the collection to our new collection and return the collection

While trying to filter we run into problems with our code not being called. We found the answer at adminhtml - Magento customer grid _prepareCollection() override is not working - Magento Stack Exchange

Preparing the columns to display our data

The columns are used to display the data we loaded in prepareCollection() function.

protected function _prepareColumns()
{
   $this->addColumnAfter('order_number', array(
      'header' => Mage::helper('underthecocotree_subscribers_helper')->__('Order Number'),
      'width'  => '90px',
      'type'   => 'text',
      'index'  => 'increment_id',
   ), 'code');

      $this->addColumnAfter('customer_email', array(
      'header' => Mage::helper('underthecocotree_subscribers_helper')->__("Order Email"),
      'type'   => 'text',
      'index'  => 'customer_email',
   ), 'order_number');

	// use to reorder the columns by the order defined above
   $this->sortColumnsByOrder();

   return parent::_prepareColumns();
}

You can use $this->addColumn() that add the columns to the end of the grid but we prefer to use the $this->addColumnAfter() method to be able to control the order of the columns. The signature for $this->addColumnAfter() is included in the Mage_Adminhtml_Block_Widget_Grid class.

   /**
     * @param   string $columnId
     * @param   array|Varien_Object $column
     * @param   string $after
     */
    public function addColumnAfter($columnId, $column, $after);

The function receives three arguments:

  • Pass the id for the column
  • Define a column as an array
  • Place it after the column with id

The coupon grid comes with a code column build in to the grid so we enter our increment_id column after the code column.

Summary

In order to override a magenta grid class, you need to understand it’s definition and it’s xml structure. Then in your config.xml file we have to define a rewrite rule to our own class. From there we inherit the original class to maintain the functionality and define our collection followed by our column structure.

A good way to see if your rewrite is working is to:

  • add a log
  • or a die and dump into your constructor of the new class

If you need to set up custom filtering follow our How to filter a grid tutorial.

Tags: