I'm working on a mass delete action for customer groups. So far I've copied app/code/core/Mage/Adminhtml/Block/Customer/Group/Grid.php and app/code/core/Mage/Adminhtml/controllers/Customer/GroupController.php to app/code/local/...
I've got the mass delete checkboxes and action to show on the Grid. However when I submit the grid I get a 404 page and I can't figure out why. Here's my code so far:
app/code/local/Mage/Adminhtml/Block/Customer/Group/Grid.php
class Mage_Adminhtml_Block_Customer_Group_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('customerGroupGrid');
$this->setDefaultSort('type');
$this->setDefaultDir('asc');
$this->setSaveParametersInSession(true);
}
/**
* Init customer groups collection
* @return void
*/
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/group_collection')
->addTaxClass();
$this->setCollection($collection);
return parent::_prepareCollection();
}
/**
* Configuration of grid
*/
protected function _prepareColumns()
{
$this->addColumn('time', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'align' => 'right',
'index' => 'customer_group_id',
));
$this->addColumn('type', array(
'header' => Mage::helper('customer')->__('Group Name'),
'index' => 'customer_group_code',
));
$this->addColumn('class_name', array(
'header' => Mage::helper('customer')->__('Tax Class'),
'index' => 'class_name',
'width' => '200px'
));
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('customer_group_id');
$this->getMassactionBlock()->setFormFieldName('group_id');
$this->getMassactionBlock()->addItem('delete', array(
'label'=> Mage::helper('customer')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('customer')->__('Are you sure?')
));
return $this;
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
}
}
app/code/local/Mage/Adminhtml/controllers/Customer/GroupController.php
class Mage_Adminhtml_Customer_GroupController extends Mage_Adminhtml_Controller_Action
{
protected function _initGroup()
{
$this->_title($this->__('Customers'))->_title($this->__('Customer Groups'));
Mage::register('current_group', Mage::getModel('customer/group'));
$groupId = $this->getRequest()->getParam('id');
if (!is_null($groupId)) {
Mage::registry('current_group')->load($groupId);
}
}
/**
* Customer groups list.
*/
public function indexAction()
{
$this->_title($this->__('Customers'))->_title($this->__('Customer Groups'));
$this->loadLayout();
$this->_setActiveMenu('customer/group');
$this->_addBreadcrumb(Mage::helper('customer')->__('Customers'), Mage::helper('customer')->__('Customers'));
$this->_addBreadcrumb(Mage::helper('customer')->__('Customer Groups'), Mage::helper('customer')->__('Customer Groups'));
$this->renderLayout();
}
/**
* Edit or create customer group.
*/
public function newAction()
{
$this->_initGroup();
$this->loadLayout();
$this->_setActiveMenu('customer/group');
$this->_addBreadcrumb(Mage::helper('customer')->__('Customers'), Mage::helper('customer')->__('Customers'));
$this->_addBreadcrumb(Mage::helper('customer')->__('Customer Groups'), Mage::helper('customer')->__('Customer Groups'), $this->getUrl('*/customer_group'));
$currentGroup = Mage::registry('current_group');
if (!is_null($currentGroup->getId())) {
$this->_addBreadcrumb(Mage::helper('customer')->__('Edit Group'), Mage::helper('customer')->__('Edit Customer Groups'));
} else {
$this->_addBreadcrumb(Mage::helper('customer')->__('New Group'), Mage::helper('customer')->__('New Customer Groups'));
}
$this->_title($currentGroup->getId() ? $currentGroup->getCode() : $this->__('New Group'));
$this->getLayout()->getBlock('content')
->append($this->getLayout()->createBlock('adminhtml/customer_group_edit', 'group')
->setEditMode((bool)Mage::registry('current_group')->getId()));
$this->renderLayout();
}
/**
* Edit customer group action. Forward to new action.
*/
public function editAction()
{
$this->_forward('new');
}
/**
* Create or save customer group.
*/
public function saveAction()
{
$customerGroup = Mage::getModel('customer/group');
$id = $this->getRequest()->getParam('id');
if (!is_null($id)) {
$customerGroup->load((int)$id);
}
$taxClass = (int)$this->getRequest()->getParam('tax_class');
if ($taxClass) {
try {
$customerGroupCode = (string)$this->getRequest()->getParam('code');
if (!empty($customerGroupCode)) {
$customerGroup->setCode($customerGroupCode);
}
$customerGroup->setTaxClassId($taxClass)->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customer')->__('The customer group has been saved.'));
$this->getResponse()->setRedirect($this->getUrl('*/customer_group'));
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setCustomerGroupData($customerGroup->getData());
$this->getResponse()->setRedirect($this->getUrl('*/customer_group/edit', array('id' => $id)));
return;
}
} else {
$this->_forward('new');
}
}
/**
* Delete customer group action
*/
public function deleteAction()
{
$customerGroup = Mage::getModel('customer/group');
if ($id = (int)$this->getRequest()->getParam('id')) {
try {
$customerGroup->load($id);
$customerGroup->delete();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customer')->__('The customer group has been deleted.'));
$this->getResponse()->setRedirect($this->getUrl('*/customer_group'));
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->getResponse()->setRedirect($this->getUrl('*/customer_group/edit', array('id' => $id)));
return;
}
}
$this->_redirect('*/customer_group');
}
public function massDeleteAction()
{
Mage::log("In Function");
$groupIds = $this->getRequest()->getParam('group_id');
Mage::log($groupIds);
if(!is_array($groupIds)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('customer')->__('Please select group(s).'));
} else {
$store_id = "1";
$website_id = Mage::app()->getStore()->getWebsiteId();
foreach ($groupIds as $groupId) {
//do something...
}
}
}
protected function _isAllowed()
{
return Mage::getSingleton('admin/session')->isAllowed('customer/group');
}
}
How can I fix this 404 error? There's nothing in var/log/system.log to show anything's wrong and no exception.log
(And yes I know I should extend the code rather than doing a local overide but I tried that and it wasn't working at all and I'm on a deadline.)
Hello denialdesign,
Did you also pass the form_key field value ? What url you are on after redirecting to the mass action right after clicking Yes option on js alert?
Not sure about the form key field. I have this code in my _prepareMassAction function:
$this->setMassactionIdField('customer_group_id');
$this->getMassactionBlock()->setFormFieldName('group_id');The url I'm getting redirected to is <mydomain>/index.php/admin/customer_group/massDelete/key/<keyvalue>/
Hello,
Still not sure why you couldn't override this module in a regular way, but I've noticed that you didn't redirect back to the group list at the end of your controller massDeleteAction() method:
$this->_redirect('*/*/index');
Did you enable error logging in your magento instance? Because in case of missing form key magento by default redirects to the dashboard, not gives you 404, so I believe there is still some error under the hood. Please make sure that you have:
1. error reporting set to E_ALL in your php.ini, index.php and / or .htaccess and set $_SERVER['MAGE_IS_DEVELOPER_MODE'] = true;
2. enabled error logging in magento: System > Configuration > Developer > Log Settings
Still not sure about the answer, but can't see in your _prepareColumns() method of your block columns with name defined as massactioned:
customer_group_id and group_id