I need to implement some bulk actions in the grid. I implement one delete action as follows:

protected function _prepareMassaction() { $this->setMassactionIdField('entity_id'); $this->getMassactionBlock()->setFormFieldName('entity_id'); $this->getMassactionBlock()->addItem('delete', array( 'label' => $this->__('Delete'), 'url' => $this->getUrl( '*/*/massDelete'), )); return $this; } 

In the controller, I process the array and everything works correctly.

But how to make some massive action?

For example, if you do this:

 protected function _prepareMassaction() { $this->setMassactionIdField('entity_id'); $this->getMassactionBlock()->setFormFieldName('entity_id'); $this->getMassactionBlock()->addItem('delete', array( 'label' => $this->__('Delete'), 'url' => $this->getUrl( '*/*/massDelete'), )); $this->getMassactionBlock()->addItem('submit', array( 'label' => $this->__('Aprove'), 'url' => $this->getUrl( '*/*/massAprove'), )); return $this; } 

That turns out nonsense. Delete will always be processed.

How can this opportunity be realized?

MassAproveAction code

 public function massAproveAction() { $products = $this->getRequest()->getParam('entity_id', null); if (is_array($products) && sizeof($products) > 0){ try{ foreach ($products as $product) { Mage::getModel('catalog/product')->setId($product)->setAprove(1); } }catch (Exception $e){ $this->_getSession()->addError($e->getTraceAsString()); } }else{ $this->_getSession()->addError('Please select product'); } } 
  • it should work this way, if you select Aprove to approve will be massAprove correct to massAprove - Naumov
  • @Naumov also hoped that it would work. But when choosing aprove - everything is deleted! - Maybe_V 4:05 pm
  • The action code is copy in question. And fix the id of the element to approve and not submit - Naumov
  • @Naumov, added code - Maybe_V 4:16 pm
  • And where is load () or collection product? and the save method has gone somewhere. What version of magento? - Naumov

1 answer 1

try this

 $productCollection = Mage::getModel('catalog/product')->getCollection() $product = $productCollection ->addAttributeToSelect(array('aprove')) // тут могу ошибаться может быыть метод addFieldToSelect ->addAttributeToFilter('entity_id',array( 'in' => $products )); ->load(); // в верху сформировали коллекцию продуктов foreach($product as $_product) { $_product ->setAprove(1) // ставим approve ->save(); // и сохраняем } 

To check, put die('approveAction') and die('deleteAction') or breakpoints in xdebug , to ensure that rout mass does not work correctly.

Next, you need to replace the id field of the submit with aprove to eliminate the id repetition, since the form of massaction to submit js can be errors and here.

  • thanks for the help!!! - Maybe_V 5:06
  • In action was the problem, or id @Maybe_V? - Naumov
  • there was a problem in id, inattention + little practice! Last question if you can: addAttributeToFilter('entity_id',array( 'in' => $products )); Should it be in or id ? - Maybe_V pm
  • @Maybe_V is the type of request stackoverflow.com/questions/12724192/magento-collection-filter i.e. in => array(1,2,3,4) on mysql will be IN(1,2,3,4) - Naumov