1

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 enter image description here

thanks in advance.

1
  • can you also post the code that is doing the actual update? Sep 27, 2013 at 7:02

1 Answer 1

3

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.

6
  • The whole category thing kinda left me puzzled. Updating products makes more sense :) To speed up your code above I would recommend using some method I found out a while back (magento.stackexchange.com/questions/8199/…) as it doesn't require you to load the whole product. It's actually used by Magento for the mass attribute update Sep 28, 2013 at 20:29
  • @SanderMangel I agree. It's faster to use the mass update. But in this case you still need to load the product to get the original price. I've updated the answer to use both.
    – Marius
    Sep 29, 2013 at 8:12
  • Ah you're right. wouldn't it be faster in this case to get a collection, filter on ID's and just add the price field as addAttributeToSelect then iterate through that? Sorry for carrying on but I gfind these cases interresting :) Sep 29, 2013 at 8:32
  • @SanderMangel D'oh!....you're right. I will change the answer.
    – Marius
    Sep 29, 2013 at 8:34
  • I'll leave you alone now ;) Sep 29, 2013 at 8:38

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.