How can I update the selected Item price in manage categories?
-
16-10-2019 - |
Question
I am new to magento, In magento admin panel -> manage categories-> I have added the new action as Update Price. when I given a amount and submit the button the selected item price will be update/decrease?
but it's not working for me can any one tell me how can I do this?
this is my code
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('price', array(
'label'=> Mage::helper('catalog')->__('Update Price'),
'url' => $this->getUrl('*/*/massPrice', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'price',
'type' => 'text',
'class' => 'required-entry',
'label' => Mage::helper('catalog')->__('Price'),
'values' => $statuses
)
)
));
see image
thanks in advance.
Solution
I think you mean 'manage products' not 'manage categories'.
You need to implement the method massPriceAction
either in the admin product controller (by overwriting it), or your custom controller and change 'url' => $this->getUrl('*/*/massPrice', array('_current'=>true)),
to the appropriate value. The in the massPriceAction
put a code similar to this (you may need to tweak it a little ):
$productIds = $this->getRequest()->getParam('product');
$priceChange =$this->getRequest()->getParam('price');//or however the price you send field is named
if (!is_array($productIds)) {
$this->_getSession()->addError($this->__('Please select product(s).'));
} elseif (empty($priceChange)) {
$this->_getSession()->addError($this->__('Fill in the price change field'));
}
if (!empty($productIds)) {
try {
$action = Mage::getModel('catalog/resource_product_action');
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('price')->addAttributeToFilter('entity_id', array('in'=>$productIds));
foreach ($collection as $product) {
$action->updateAttributes(array($product->getId()), array('price' => $product->getPrice() + $priceChange ), 0);
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) have been deleted.', count($productIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
}
$this->_redirect('*/catalog_product/index');
Thanks @SanderMangel for suggesting the faster version.