After overriding the magento order grid, pagination is not working? - php

I have overridden the Mage_Adminhtml_Block_Sales_Order_Grid to add additionl 3 columns as follows.
Customer Email
Payment Type
Products ordered
My extended grid class is as follows.
<?php
class Wowmall_ExtendedGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()
->joinLeft('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id','method')
->join('customer_entity', 'main_table.customer_id = customer_entity.entity_id','email')
->join('sales_flat_order_item', 'main_table.entity_id = sales_flat_order_item.order_id','name')->distinct(true);
$collection->getSelect()->group('main_table.entity_id');
$this->setCollection($collection);
return $this;
}
protected function _prepareColumns()
{
// rest code...
$this->addColumn('email', array(
'header' => Mage::helper('sales')->__('Customer Email'),
'index' => 'email',
'type' => 'text',
));
$this->addColumn('method', array(
'header' => Mage::helper('sales')->__('Payment Type'),
'index' => 'method',
'type' => 'options',
'options' => array('verisign' => 'Credit Card', 'checkmo' => 'Check', 'purchaseorder' => 'Purchase Order'),
));
$this->addColumn('name', array(
'header' => Mage::helper('sales')->__('Product(s) Ordered'),
'index' => 'name',
'type' => 'text',
));
// rest code...
But the pagination is not working. All the records are loading in a single page.
Please any suggestions?

Found the solution.
The issue occurred due to the following statement.
$collection->getSelect()->group('main_table.entity_id');
I edited the lib/Varien/Data/Collection/Db.php
My Db.php file located in app/code/local/Varien/Data/Collection/Db.php
Following is the code.
.....//rest code
public function getSelectCountSql()
{
$this->_renderFilters();
$countSelect = clone $this->getSelect();
$countSelect->reset(Zend_Db_Select::ORDER);
$countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
$countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
$countSelect->reset(Zend_Db_Select::COLUMNS);
if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) {
$countSelect->reset(Zend_Db_Select::GROUP);
$countSelect->distinct(true);
$group = $this->getSelect()->getPart(Zend_Db_Select::GROUP);
$countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
} else {
$countSelect->columns('COUNT(*)');
}
return $countSelect;
}
....//rest code
Then after clearing the cache and session it worked .. :)

Related

magento saveAction - for beginners

I am a Magento beginner so please bear with me...
I am creating a simple extension for my site to add a custom field to my Tags in adminhtml. The custom field is just a number which I need to identify a specific Z-block (cms block extension) so that I can access it as a widget and show it on the frontend in the Tag "category".
I have created a custom module which is working: I set a field in the form using $fieldset and have extended TagController.php, both of which are being used (I made a simple trial to see whether or not they had been recognized). However, I do not know how to go about saving my custom field to DB (whether amending saveAction is enough, and I haven't done it properly, or if I need to add a custom Model or sql install).
Sorry for the "basic" question but I'm new at this, and have mostly done frontend dev (so my extension knowledge is simply limited).
Thank you to anyone who can help...
Claudia
NEW TAG FORM:
public function __construct()
{
parent::__construct();
$this->setId('tag_form');
$this->setTitle(Mage::helper('tag')->__('Block Information'));
}
/**
* Prepare form
*
* #return Mage_Adminhtml_Block_Widget_Form
*/
protected function _prepareForm()
{
$model = Mage::registry('tag_tag');
$form = new Varien_Data_Form(
array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post')
);
$fieldset = $form->addFieldset('base_fieldset',
array('legend'=>Mage::helper('tag')->__('General Information')));
if ($model->getTagId()) {
$fieldset->addField('tag_id', 'hidden', array(
'name' => 'tag_id',
));
}
$fieldset->addField('form_key', 'hidden', array(
'name' => 'form_key',
'value' => Mage::getSingleton('core/session')->getFormKey(),
));
$fieldset->addField('store_id', 'hidden', array(
'name' => 'store_id',
'value' => (int)$this->getRequest()->getParam('store')
));
$fieldset->addField('name', 'text', array(
'name' => 'tag_name',
'label' => Mage::helper('tag')->__('Tag Name'),
'title' => Mage::helper('tag')->__('Tag Name'),
'required' => true,
'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
));
$fieldset->addField('zblock', 'text', array(
'name' => 'zblock_id',
'label' => Mage::helper('tag')->__('Z-Block Id'),
'title' => Mage::helper('tag')->__('Z-Block Id'),
'required' => true,
'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
));
$fieldset->addField('status', 'select', array(
'label' => Mage::helper('tag')->__('Status'),
'title' => Mage::helper('tag')->__('Status'),
'name' => 'tag_status',
'required' => true,
'options' => array(
Mage_Tag_Model_Tag::STATUS_DISABLED => Mage::helper('tag')->__('Disabled'),
Mage_Tag_Model_Tag::STATUS_PENDING => Mage::helper('tag')->__('Pending'),
Mage_Tag_Model_Tag::STATUS_APPROVED => Mage::helper('tag')->__('Approved'),
),
'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
));
$fieldset->addField('base_popularity', 'text', array(
'name' => 'base_popularity',
'label' => Mage::helper('tag')->__('Base Popularity'),
'title' => Mage::helper('tag')->__('Base Popularity'),
'after_element_html' => ' ' . Mage::helper('tag')->__('[STORE VIEW]'),
));
if (!$model->getId() && !Mage::getSingleton('adminhtml/session')->getTagData() ) {
$model->setStatus(Mage_Tag_Model_Tag::STATUS_APPROVED);
}
if ( Mage::getSingleton('adminhtml/session')->getTagData() ) {
$form->addValues(Mage::getSingleton('adminhtml/session')->getTagData());
Mage::getSingleton('adminhtml/session')->setTagData(null);
} else {
$form->addValues($model->getData());
}
$this->setForm($form);
return parent::_prepareForm();
}
NEW CONTROLLER:
public function saveAction()
{
if ($postData = $this->getRequest()->getPost()) {
if (isset($postData['tag_id'])) {
$data['tag_id'] = $postData['tag_id'];
}
$data['name'] = trim($postData['tag_name']);
$data['zblock'] = $postData['zblock_id'];
$data['status'] = $postData['tag_status'];
$data['base_popularity'] = (isset($postData['base_popularity'])) ? $postData['base_popularity'] : 0;
$data['store'] = $postData['store_id'];
if (!$model = $this->_initTag()) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Wrong tag was specified.'));
return $this->_redirect('*/*/index', array('store' => $data['store']));
}
$model->addData($data);
if (isset($postData['tag_assigned_products'])) {
$productIds = Mage::helper('adminhtml/js')->decodeGridSerializedInput(
$postData['tag_assigned_products']
);
$model->setData('tag_assigned_products', $productIds);
}
try {
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The tag has been saved.'));
Mage::getSingleton('adminhtml/session')->setTagData(false);
if (($continue = $this->getRequest()->getParam('continue'))) {
return $this->_redirect('*/tag/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId(), 'ret' => $continue));
} else {
return $this->_redirect('*/tag/' . $this->getRequest()->getParam('ret', 'index'));
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setTagData($data);
return $this->_redirect('*/*/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId()));
}
}
return $this->_redirect('*/tag/index', array('_current' => true));
}
The custom field I'm trying to add is "zblock"...thanks and, again, bear with me! :)
First add the field in database table.
For example if you want to add in your custom table.
ALTER TABLE myCustomModuleTable ADD COLUMN 'myCustomField' int(10);
Thenafter, In your controller action take the model object of that table and set the field.
If you are adding data in existing table row:
$value = 6;
$rowInWhichIWantToSave = Mage:getModel('companyname/modulename')->load($rowId);
$rowInWhichIWantToSave->setData('myCustomField',$value)->save();
If you are adding a new row:
$value = 6;
$rowInWhichIWantToSave = Mage:getModel('companyname/modulename');
$rowInWhichIWantToSave->setData('myCustomField',$value)->save();
Hope this helps!!

Magento/PHP - Get Phones, Emails, Names for all customers in a selected customer group

I am working on a custom Magento(1.9.0.1) extension.
Let me show you what i have done already.
Here is the code for this Adminhtml form:
<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Sendmass_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
public function _prepareLayout()
{
$ExtensionPath = Mage::getModuleDir('js', 'VivasIndustries_SmsNotification');
$head = $this->getLayout()->getBlock('head');
$head->addJs('jquery.js');
$head->addJs('vivas.js');
return parent::_prepareLayout();
}
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
));
$fieldset = $form->addFieldset('edit_form', array('legend'=>Mage::helper('smsnotification')->__('SMS Information')));
$CustomerGroups = Mage::getResourceModel('customer/group_collection')->toOptionArray();
$smsprice_value = Mage::getStoreConfig('vivas/smsprice/smsprice_value');
$smsprice_tag = Mage::getStoreConfig('vivas/smsprice/smsprice_tag');
$customerArray=array();
foreach($CustomerGroups as $each){
$count=Mage::getResourceModel('customer/customer_collection')
->addAttributeToFilter('group_id',$each['value'])->getSize();
$SMSPrice = $count * $smsprice_value;
$customerArray[]=array('value'=> $each['value'],'label'=> $each['label'].' - ('.$count.' Members) - ('.$SMSPrice.' '.$smsprice_tag.')');
}
$CustomerGroups = array_merge(array('' => ''), $customerArray);
$fieldset->addField('customergroups', 'select',
array(
'name' => 'customergroups',
'label' => Mage::helper('smsnotification')->__('Customer Group'),
'class' => 'required-entry',
'after_element_html' => '<br><small>If customer group is not selected the SMS will be sended<br> to all store members!</small>',
'values' => $CustomerGroups
)
);
$fieldset->addField('smstext', 'textarea', array(
'label' => Mage::helper('smsnotification')->__('SMS Text'),
'class' => 'required-entry',
'required' => true,
'name' => 'smstext',
'onclick' => "",
'onkeyup' => "CheckLetterSize(this)",
'after_element_html' => '<br><b style="color:brown;"><span id="charNum"></span><span id="charNum1"></span></b><br><small>SMS text must <b>NOT</b> be longer then 160 characters!</small>',
'tabindex' => 1
));
if ( Mage::getSingleton('adminhtml/session')->getsmsnotificationData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getsmsnotificationData());
Mage::getSingleton('adminhtml/session')->setsmsnotificationData(null);
} elseif ( Mage::registry('smsnotification_data') ) {
$form->setValues(Mage::registry('smsnotification_data')->getData());
}
// Add these two lines
$form->setUseContainer(true);
$this->setForm($form);
////
return parent::_prepareForm();
}
}
When this form is submitted i want to get all Names, phones and emails of the members in the selected group. The second thing is, when no customer group is selected it must give me names, phones and emails for all customers in the store.
I used to get information about the user from Order information like this:
class VivasIndustries_SmsNotification_Model_Observer
{
public function orderSaved(Varien_Event_Observer $observer)
{
/** **/
$CustomerPhone = $observer->getOrder()->getBillingAddress()->getTelephone();
$CustomerName = $observer->getOrder()->getBillingAddress()->getName();
$CustomerEmail = $observer->getOrder()->getBillingAddress()->getEmail();
}
}
I know that this is not even close to what i need but i don't know how to get this information.
Can you please help me out ?
Thanks in advance!
In your admin controller, where the form is posted:
$groupId = $this->getRequest()->getPost('customergroups', '');
if (!empty($groupId)) {
//Get customers from a group
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('group_id', $groupId);
} else {
//Get all customers
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*');
}

Displaying Virtual Attribute In CGridView

I have used a from_date & to_date attributes to search my data and have placed it in the Model as safe attributes. Now I want to display the same from_date and to_date in the CGridview with other data from the model.
Shouldn't I just be able to use $data->from_date in my CGridView?
Model
public $from_date;
public $to_date;
public function rules() {
array('from_date, to_date', 'safe', 'on'=>'search'),
}
public function search(){
//....
if(!empty($this->to_date) && !empty($this->from_date))
{
$criteria->addCondition("date($created_date) >= '$this->from_date' and date($created_date) <= '$this->to_date'");
}
else if(!empty($this->from_date) && empty($this->to_date))
{
$criteria->addCondition("date($created_date) >= '$this->from_date'");
}
else if(!empty($this->to_date) && empty($this->from_date))
{
$criteria->addCondition("date($created_date) <= '$this->to_date'");
}
//....
}
Controller
$model = new Tickets('search');
if (!empty($_GET)) {
$model->ticket_id = isset($_GET['ticket_id']) ? $_GET['ticket_id'] : '';
$model->from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
$model->to_date = isset($_GET['to_date']) ? $_GET['to_date'] : '';
}
$this->render('search', array(
'model' => $model
));
View
$this->widget('bootstrap.widgets.TbGridView', array(
'type' => 'striped bordered condensed',
'dataProvider' => $model->search(),
'columns' => array(
array(
'name' => 'From date',
'type' => 'html',
'value' => '$data->from_date',
),
),
));
Try the following adjustments. I don't see where you specified how the $_GET attributes were being set, so I included the standard way of doing it with CGridView. Let me know if it works.
Controller:
$model = new Tickets('search');
//remove any default values
$model->unsetAttributes();
//set the attributes based on the standard syntax of how CGridview populates GET
if (!empty($_GET['Tickets'])) {
$model->attributes = $_GET['Tickets'];
}
$this->render('search', array(
'model' => $model
));
Model: add ticket_id to rules, to automatically process set it if present.
public function rules() {
array('from_date, to_date, ticket_id', 'safe', 'on'=>'search'),
}
View:
$this->widget('bootstrap.widgets.TbGridView', array(
'type' => 'striped bordered condensed',
'dataProvider' => $model->search(),
'filter'=>$model,//should provide default filtering
'columns' => array(
array(
'name' => 'From date',
'type' => 'html',
'value' => '$data->from_date',
),
),
));
Side note: Your model's search method had huge SQL injection vulnerabilities, but let's solve one problem at a time first.

Magento Admin Grid Date Format

I'd like to be able to have a date/calendar selector in one of my admin grids when editing/adding an item. How do I code a calendar-style format?
I've tried changing the "format" parameter to "date" with no success.
I will include code and snapshots for clarification. Some of the code comes from the existing project, some comes from tutorials. I don't feel like a master of this code by any means, so any suggestions are welcome.
PHP 5.4.25 Magento 1.8.1.0
The search section on my index seems to be working:
But I'd like to change the text field for "close date" to a calendar-picker:
Here is my code for ../Party/Block/Adminhtml/Party/Edit/Tab/Form.php:
class Foobar_Party_Block_Adminhtml_Party_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {
protected function _prepareForm() {
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset("party_form", array("legend" => Mage::helper("party")->__("Party information")));
/* Code for other columns */
$fieldset->addField("party_close_date", "date", array(
"label" => Mage::helper("party")->__("Close Date"),
'format' => 'date',
'name' => 'party_close_date',
'time' => 'true',
));
if (Mage::getSingleton("adminhtml/session")->getPartyData()) {
$form->setValues(Mage::getSingleton("adminhtml/session")->getPartyData());
Mage::getSingleton("adminhtml/session")->setPartyData(null);
} elseif (Mage::registry("party_data")) {
$form->setValues(Mage::registry("party_data")->getData());
}
return parent::_prepareForm();
}
}
Here is my code for ../Party/Block/Adminhtml/Party/Edit/Form.php:
<?php
class Foobar_Party_Block_Adminhtml_Party_Edit_Form extends Mage_Adminhtml_Block_Widget_Form {
protected function _prepareForm() {
$form = new Varien_Data_Form(array(
"id" => "edit_form",
"action" => $this->getUrl("*/*/save", array("id" => $this->getRequest()->getParam("id"))),
"method" => "post",
"enctype" => "multipart/form-data",
)
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
Here is my code for ../Party/Block/Adminhtml/Party/Grid.php:
<?php
class Foobar_Party_Block_Adminhtml_Party_Grid extends Mage_Adminhtml_Block_Widget_Grid {
public function __construct() {
parent::__construct();
$this->setId("partyGrid");
$this->setDefaultDir("ASC");
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection() {
$collection = Mage::getModel("party/party")->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
/* Code for other columns */
$this->addColumn("party_close_date", array(
"header" => Mage::helper("party")->__("Close Date"),
"type" => "datetime",
"index" => "party_close_data",
));
$this->addExportType('*/*/exportCsv', Mage::helper('party')->__('CSV'));
$this->addExportType('*/*/exportExcel', Mage::helper('party')->__('Excel'));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl("*/*/edit", array("id" => $row->getId()));
}
}
I found the answer to my question!
I added 3 lines to my ..Edit/Tab/Form.php file.
I changed these lines...
$fieldset->addField("party_close_date", "date", array(
"label" => Mage::helper("party")->__("Close Date"),
'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
'name' => 'party_close_date',
'time' => 'true',
));
...to this:
$fieldset->addField("party_close_date", "date", array(
"label" => Mage::helper("party")->__("Close Date"),
'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'name' => 'party_close_date',
'time' => 'true',
));
There's a handy tutorial here that helped me out loads.

Magento Grid : Better way of using addColumn Actions?

This function sets up the Magento Grid to display a list of filenames with a corresponding 'Delete' action.
The problem is the Delete action never passes the parameter, 'filename.' (See http://www.premasolutions.com/content/magento-manage-category-product-grid-edit-link) I have TESTDUMP for verification but it never prints on the next page.
Is 'params' a legitimate action of 'addColumn->actions->url'?
Update: Added the construct and prepare collection for controller. Maybe it's because of the type of Collection I'm using?
class Rogue_Googlemerchant_Block_Adminhtml_Exporter_Grid
Rogue_Googlemerchant_Block_Adminhtml_Exporter_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('googlemerchantGrid');
// This is the primary key of the database
$this->setDefaultSort('filename');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$basePath = Mage::getBaseDir('base');
$feedPath = $basePath . '/opt/googlemerchant/';
$errPath = $basePath . '/var/log/googlemerchant/';
$flocal = new Varien_Io_File();
$flocal->open(array('path' => $feedPath));
$dataCollection = new Varien_Data_Collection();
foreach ($flocal->ls() as $item) {
$dataObject = new Varien_Object();
$dataObject->addData(
array(
'filename' => $item['text'],
'size' => $item['size'] / 1000 . ' kb',
'date_modified'=> $item['mod_date']
)
);
$dataCollection->addItem($dataObject);
}
$this->setCollection($dataCollection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('filename', array(
'header' => Mage::helper('googlemerchant')->__('File'),
'align' =>'left',
'index' => 'filename',
'width' => '200px',
));
$this->addColumn('action', array(
'header' => Mage::helper('googlemerchant')->__('Action'),
'width' => '50px',
'type' => 'action',
// 'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('googlemerchant')->__('Delete'),
'url' =>
array(
'base' => '*/*/delete',
'params' => array('filename' => 'TESTDUMP')
),
'field' => 'filename'
)
),
'filter' => false,
'sortable' => false,
// 'index' => 'filename',
// 'is_system' => true,
));
}
}
class Rogue_Googlemerchant_Adminhtml_ExporterController
class Rogue_Googlemerchant_Adminhtml_ExporterController extends Mage_Adminhtml_Controller_Action
{
public function deleteAction()
{
$filename = $this->getRequest()->getParam('filename');
$basePath = Mage::getBaseDir('base');
$feedPath = $basePath . '/opt/googlemerchant/';
$errPath = $basePath . '/var/log/googlemerchant/';
$flocal = new Varien_Io_File();
$flocal->open(array('path' => $feedPath));
d($filename);
if ($filename) {
try{
$flocal->rm($filename);
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('googlemerchant')->__('The file has been deleted.'));
$this->_redirect('*/*/');
}
catch (Mage_Core_Exception $e) {
$this->log($e);
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/index');
return;
}
}
die('here');
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find the file to delete.'));
$this->_redirect('*/*/');
}
}
The getter of the action column is used on the collection items to retrieve the argument value for the field parameter.
I'm not sure why you are specifying the filename hardcoded or if that should work, but if you add the column configuration
'getter' => 'getFilename'
and remove the params from the action it should work.

Categories