Magento ProductController将categoryId附加到产品中

时间:2015-05-08 15:13:53

标签: php magento

我正在添加一个群发动作来添加一个类别。我大部分时间都只有一个功能可以解决。

CLR \ Categorymassaction \控制器\ Adminhtml \目录\ 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 \模型\观察     

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',

                        )
                    )
                ));
        };
    }


}

最后一件事

 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;
        }

我在这里遇到麻烦,因为我正在重构,Flagbit_changeattributeset和Vuleticd_AdminGridCategoryFilter。我知道我需要做什么(至少我认为我这样做)我只是不知道如何完成这个。如果您阅读全部,请感谢您的眼睛和耳朵。

更新:来自Vuleticd_AdminGridCategoryFilter的观察者有这个额外的代码

                    '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;
}

这用于将过滤器应用于网格。我想要做的不是使用下拉列表来过滤列字段;使用下拉列表触发ProductController将所选项目传递给新的categoryid。

1 个答案:

答案 0 :(得分:0)

https://magento.stackexchange.com/questions/67234/productcontroller-for-mass-action在magento的stackexchange上提出这个问题时,我会在这里发布链接给后人。