简体   繁体   中英

Magento ProductController append categoryId to products

I am adding a mass action to add a category. I am most of the way there I only have one function left to figure out.

Clr\\Categorymassaction\\controllers\\Adminhtml\\Catalog\\ProductController.php

    class Clr_Categorymassaction_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Controller_Action
    {
        public function massCategoryAction()
        {
            $productIds = $this->getRequest()->getParam('product');
            $cat = $this->getRequest()->getParam('Category');
            if (!is_array($productIds)) {
                $this->_getSession()->addError($this->__('Please select product(s).'));
                $this->_redirect('*/*/index');
            }
            else {
                    $cat = $category['label']->getCategoryId();
                    foreach($productIds as $product) {
                        //Process $cat into categoryId append categoryId to $productId
                       $cat->setPostedProducts($product);

                    }
              //Save product
              $cat->save();
            }



}
}

Clr\\Categorymassaction\\Model\\Observer

class Clr_Categorymassaction_Model_Observer {
    public function addCategoryMassAction(Varien_Event_Observer $observer)
    {
        $block = $observer ->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid) {
            $block->getMassactionBlock()->addItem('Clr_Categorymassaction', array(
                'label'     =>  Mage::helper('catalog')->__('Add to Category'),
                'url'       =>  $block->getUrl('*/*/massCategory', array('_current' => true)),
                'additional'=> array(
                    'visibility'    => array(
                        'name'          =>'Category',
                        'class'         =>'required-entry',
                        'label'         =>Mage::helper('catalog')->__('Categories'),
                        'type'          => 'select',
                        'values'        => Mage::getModel('Categorymassaction/system_config_source_category')->toOptionArray(),
                        'renderer'      => 'Categorymassaction/catalog_product_grid_render_category',

                        )
                    )
                ));
        };
    }


}

One last thing

 class Clr_Categorymassaction_Model_System_Config_Source_Category
    {
        public function toOptionArray($addEmpty = true)
        {
            $options = array();
            foreach ($this->load_tree() as $category) {
                $options[$category['value']] =  $category['label'];
            }

            return $options;
        }

I am mostly in trouble here because I am refactoring, Flagbit_changeattributeset and Vuleticd_AdminGridCategoryFilter. I know what I need to do (at least I think I do) I just don't know how to finish this off. Thanks for your eyes and ears if you read it all.

UPDATE: The observer from Vuleticd_AdminGridCategoryFilter had this additional code

                    'filter_condition_callback' => array($this, 'filterCallback'),
                    )
                )
            ));
    };
}
public function filterCallback($collection, $column)
{
    $value = $column->getFilter()->getValue();
    $_category = Mage::getModel('catalog/category')->load($value);
    $collection->addCategoryFilter($_category);

    return $collection;
}

This was used to apply the filter to the grid. What I am trying to do is instead of using the dropdown to filter column fields; use the dropdown to trigger the ProductController to pass the selected items a new categoryid.

https://magento.stackexchange.com/questions/67234/productcontroller-for-mass-action在magento的stackexchange上问了这个问题,我想在此张贴链接以供后代参考。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM