Magento: 设置category类别为必填 How to make product category selection required

You can rewrite the method Mage_Adminhtml_Catalog_ProductController::validateAction() and check if the product is new and category_ids hasn’t been sent through POST then throw an exception.

Something like this: app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

public function validateAction()
{
    $response = new Varien_Object();
    $response->setError(false);

    try {
        $productData = $this->getRequest()->getPost('product');

        if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) {
            $productData['stock_data']['use_config_manage_stock'] = 0;
        }
        /* @var $product Mage_Catalog_Model_Product */
        $product = Mage::getModel('catalog/product');
        $product->setData('_edit_mode', true);
        if ($storeId = $this->getRequest()->getParam('store')) {
            $product->setStoreId($storeId);
        }
        if ($setId = $this->getRequest()->getParam('set')) {
            $product->setAttributeSetId($setId);
        }
        if ($typeId = $this->getRequest()->getParam('type')) {
            $product->setTypeId($typeId);
        }
        if ($productId = $this->getRequest()->getParam('id')) {
            $product->load($productId);
        }
        //==>start my changes 这里添加开始
        $postedCategories = $this->getRequest()->getParam('category_ids');
        if ($product->getId()) { //if edit mode 
            //if product does not have categories and no categories were sent...
            if (!$product->getCategoryIds() && empty($postedCategories)){
                throw new Mage_Catalog_Exception('Fill in categories'); //translate if needed
            }
        }
        else { //if new mode
            if (empty($postedCategories)){
                throw new Mage_Catalog_Exception('Fill in categories'); //translate if needed
            }
        }
        //<=== end my changes 这里添加结束

        $dateFields = array();
        $attributes = $product->getAttributes();
        foreach ($attributes as $attrKey => $attribute) {
            if ($attribute->getBackend()->getType() == 'datetime') {
                if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != ''){
                    $dateFields[] = $attrKey;
                }
            }
        }
        $productData = $this->_filterDates($productData, $dateFields);

        $product->addData($productData);
        $product->validate();
    }
    catch (Mage_Eav_Model_Entity_Attribute_Exception $e) {
        $response->setError(true);
        $response->setAttribute($e->getAttributeCode());
        $response->setMessage($e->getMessage());
    } catch (Mage_Core_Exception $e) {
        $response->setError(true);
        $response->setMessage($e->getMessage());
    } catch (Exception $e) {
        $this->_getSession()->addError($e->getMessage());
        $this->_initLayoutMessages('adminhtml/session');
        $response->setError(true);
        $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
    }

    $this->getResponse()->setBody($response->toJson());
}

I copied the default validateAction method and added some code. See ===>start my changes.
There are also these events catalog_product_validate_before and catalog_product_validate_after but, unfortunately thy don’t take into account the category ids. The product arrives in the validation method without the category ids even if you set them.

参考:http://magento.stackexchange.com/a/11199

如果出现下面两个iconSelection_075

你可以修改 skin/adminhtml/default/default/boxes.css

after line 31: 添加

.messages li.error-msg ul li { background:#FAEBE7; }

 

本文:Magento: 设置category类别为必填 How to make product category selection required

 

Leave a Reply