Sunday, October 23, 2016

Add Order Details Column in Customer Grid Magento


Many Magento website used for e-commerce website and for store owner customers are most important for any website. so they check all customer details who is online and who purchase maximum orders from the website. So store admin analysis stores customer for any offers or selling orders.
So here display basic code that you can overwrite exiting core block grid using new extension or you can directly customize admin customer grid using some code display here.
First open 'app/code/core/Mage/Adminhtml/Block/Customer/Grid.php' file or you can copy that file on local pool and than customize it. find function `_prepareCollection()` change code as below.

protected function _prepareCollection()
{
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
  //Add below code for last order date
        $sql ='SELECT MAX(o.created_at) FROM '.Mage::getSingleton('core/resource')->getTableName('sales/order').' AS o WHERE o.customer_id = e.entity_id';
  $expr = new Zend_Db_Expr('(' . $sql . ')');
  $collection->getSelect()->from(null, array('last_order_date'=>$expr));
  //Add below code for count total orders
  $sql ='SELECT COUNT(*) FROM '.Mage::getSingleton('core/resource')->getTableName('sales/order').' AS o WHERE o.customer_id = e.entity_id';
  $expr = new Zend_Db_Expr('(' . $sql . ')');
  $collection->getSelect()->from(null, array('orders_count'=>$expr));
  $this->setCollection($collection);
        return parent::_prepareCollection();
}

As below code you get the last order date and total customers orders from order table. In Magento `sales_flat_order` there is column for `customer_id` which is stored register customer id of that store.
Add below two fields in function `_prepareColumns ()` where you want to display fields.

$this->addColumn('last_order_date', array(
 'header'    => Mage::helper('customer')->__('Last Order Date'),
 'type'      => 'datetime',
 'align'     => 'center',
 'index'     => 'last_order_date',
 'filter'    => false, //For remove search column
 'sortable'  => false, //For remove sorting column
));
$this->addColumn('orders_count', array(
 'header'    => Mage::helper('customer')->__('Orders Count'),
 'index'     => 'orders_count',
 'type'  => 'number',
));
In below code you can use any `type` for show in grid and if you remove sorting and filtering functionality for this column than it's OK but if you want to short and filter of below column than you must create custom grid filter as below.

$this->addColumn('last_order_date', array(
 'header'    => Mage::helper('customer')->__('Last Order Date'),
 'type'      => 'datetime',
 'align'     => 'center',
 'index'     => 'last_order_date',
 'filter_condition_callback' => array($this, '_orderfiltersearch')
));
Than create new function on same file for search call callback function as below.

public function _orderfiltersearch($collection, $column)
{
 if (!$value = $column->getFilter()->getValue()){
  return $this;
 }
 if($value['orig_from'] == '' || $value['orig_to'] == ''){
  return $this;
 }
 $fromDate = date('Y-m-d H:i:s', strtotime($value['orig_from']));
 $toDate = date('Y-m-d H:i:s', strtotime($value['orig_to']));
 $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
 foreach($orders as $order)
 {
  $customerids[] = $order->getCustomerId();
 }
 $customerids = array_unique($customerids);
 $this->getCollection()->addFieldToFilter('entity_id', array('in' => $customerids));
 return;
}
You can use same search callback function for, finding others with getting order collection from the database according to customer id and use that id in `addFieldToFilter` for filter Magento collection.

Thursday, October 13, 2016

Remove Review Page in Magento PayPal Express Checkout


Magento is most used PHP based e-commerce platform which is used for create shopping website and for that payment system is most important part of any e-commerce system. Magento also provide some most used payment methods like PayPal, Authorized, COD, Check payment also also many extensions available for easy implement new payment method in website.
When you use the PayPal payment method than mostly used 'PayPal Express Checkout' for fast, secure and easy checkout. but after complete PayPal checkout it's redirecting to Magento payment review page.
If you use 'PayPal standard checkout' them after complete payment from PayPal, it's redirecting to a checkout success page and if your Magento version is greater than 1.9 than you can skin review page from the admin panel by set `Skip Order Review Step` YES.
If your Magento version is less than 1.9 than you can skin that page after changes on below files.
First open `app\code\core\Mage\Paypal\Model\Config.php` file and changes below in getExpressCheckoutStartUrl function from

public function getExpressCheckoutStartUrl($token)
{
    return $this->getPaypalUrl(array(
        'cmd'   => '_express-checkout',
        'token' => $token,
    ));
}
to
public function getExpressCheckoutStartUrl($token)
{
    return $this->getPaypalUrl(array(
        'cmd'   => '_express-checkout',
        'useraction' => 'commit',
        'token' => $token,
    ));
}
 
Than create observe event in global scope as below in any custom extension's `confix.xml` file or create own.
 
<controller_action_predispatch_paypal_express_review>
  <observers>
    <newsinfo_removepaypalexpressreview>
      <type>singleton</type>
      <class>Newsinfo_Modulename_Model_Observer</class>
      <method>controllerPaypalExpressReview</method>
    </newsinfo_removepaypalexpressreview>
  </observers>
</controller_action_predispatch_paypal_express_review>
<controller_action_predispatch_paypal_express_placeOrder>
  <observers>
    <newsinfo_removepaypalexpressreview>
      <type>singleton</type>
      <class>Newsinfo_Modulename_Model_Observer</class>
      <method>controllerPaypalExpressPlaceOrder</method>
    </newsinfo_removepaypalexpressreview>
  </observers>
</controller_action_predispatch_paypal_express_placeOrder>

Than create 'Observer.php' file in Model folder and add below code(You put below functions in exiting Observe.php file)

class Newsinfo_Modulename_Model_Observer //Set Class According to Your Extension
{
 public function controllerPaypalExpressReview(Varien_Event_Observer $observer)
 {
  Mage::app()->getResponse()->setRedirect(Mage::getUrl('*/*/placeOrder'));
 }
 public function controllerPaypalExpressPlaceOrder(Varien_Event_Observer $observer)
 {
  $agreements = Mage::helper('checkout')->getRequiredAgreementIds();
  $postagreements = array_fill_keys($agreements, 1);
  Mage::app()->getRequest()->setPost('agreement', $postagreements);
 }
}