0

I have created a custom module and need to add checkboxes to the grid and be able to delete multiple values with a mass action. Tried to use number of different articles but nothing worked. Can anyone help me to do this or maybe you know a good working article about this?

Here is the code in controller file:

    public function massDeleteAction() {
       $requestIds = $this->getRequest()->getParam('id');
       if(!is_array($requestIds)) {
       Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select reqeust(s)'));
       } else {
       try {
        foreach ($requestIds as $requestId) {
            $RequestData = Mage::getModel('cpstest_productcomment/cps_collection')->load($requestId);                    
            $RequestData->delete();                    
        }
        Mage::getSingleton('adminhtml/session')->addSuccess(
            Mage::helper('adminhtml')->__(
                'Total of %d record(s) were successfully deleted', count($requestIds)
            )
        );
        } catch (Exception $e) {
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        }
        $this->_redirect('*/*/');
        }
5
  • can you get your solution?? Commented Apr 4, 2017 at 11:22
  • Please add code for your grid file and controller file Commented Apr 4, 2017 at 12:53
  • @Jaimin Sutariya thank you guys for responding, I added the codes. Commented Apr 4, 2017 at 17:22
  • 1
    Are you sure you are using correct Model here Mage::getModel('cpstest_productcomment/cps_collection') IT seems like the model is not found. Commented Apr 4, 2017 at 17:25
  • @Jaimin Sutariya you are right, I just changed the model name and it worked :)))) Thank you all guys, you're amazing!! Commented Apr 4, 2017 at 17:36

2 Answers 2

5

You can also try this code.
Put below code into your grid.php

protected function _prepareMassaction()
{
    $this->setMassactionIdField('id');
    $this->getMassactionBlock()->setFormFieldName('your_field');

    $this->getMassactionBlock()->addItem('delete', array(
         'label'    => Mage::helper('your_helper')->__('Delete'),
         'url'      => $this->getUrl('*/*/massDelete'),
         'confirm'  => Mage::helper('your_helper')->__('Are you sure?')
    ));

    return $this;
}

Put below code into your admin controller

public function massDeleteAction() {
    $requestIds = $this->getRequest()->getParam('your_field');
    if(!is_array($requestIds)) {
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select reqeust(s)'));
    } else {
        try {
            foreach ($requestIds as $requestId) {
                $RequestData = Mage::getModel('your_model')->load($requestId);                    
                $RequestData->delete();                    
            }
            Mage::getSingleton('adminhtml/session')->addSuccess(
                Mage::helper('adminhtml')->__(
                    'Total of %d record(s) were successfully deleted', count($requestIds)
                )
            );
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
    }
    $this->_redirect('*/*/');
}
3
  • Hi and thanks so much for your answer. I used these codes and got an error. Please take a look at the print screen I'm adding now. Commented Apr 4, 2017 at 17:11
  • I made a mistake in the model name, just made a correction and it worked :)))) Thanks so much. By the way, I also wanted to say that your solution is the best, I tried so many different codes ,they were more complicated and didn't work.. Commented Apr 4, 2017 at 17:40
  • It's my pleasure @bestwebdevs Commented Apr 6, 2017 at 5:15
1

You just need to add a function in your grid file in which you have written code for adding grid columns. I am providing you an example for mass action of catalog grid on admin panel. Please check below code:

protected function _prepareMassaction()
{
    $this->setMassactionIdField('entity_id');
    $this->getMassactionBlock()->setFormFieldName('product');

    $this->getMassactionBlock()->addItem('delete', array(
         'label'=> Mage::helper('catalog')->__('Delete'),
         'url'  => $this->getUrl('*/*/massDelete'),
         'confirm' => Mage::helper('catalog')->__('Are you sure?')
    ));

    $statuses = Mage::getSingleton('catalog/product_status')->getOptionArray();

    array_unshift($statuses, array('label'=>'', 'value'=>''));
    $this->getMassactionBlock()->addItem('status', array(
         'label'=> Mage::helper('catalog')->__('Change status'),
         'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
         'additional' => array(
                'visibility' => array(
                     'name' => 'status',
                     'type' => 'select',
                     'class' => 'required-entry',
                     'label' => Mage::helper('catalog')->__('Status'),
                     'values' => $statuses
                 )
         )
    ));

    if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')){
        $this->getMassactionBlock()->addItem('attributes', array(
            'label' => Mage::helper('catalog')->__('Update Attributes'),
            'url'   => $this->getUrl('*/catalog_product_action_attribute/edit', array('_current'=>true))
        ));
    }

    Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this));
    return $this;
}

Your Answer

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

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