5

is there any way that if a user creates or edit a new product on admin backend the selection of its category in category tab will be required? I searched already in google and unluckily I can't find any clues.

4

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:

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.

2

In addition to Marius' answer, you can automatically catch the validation before postback if you add the required-entry class to the category Id's input box.

This is located in app/design/adminhtml/default/default/template/catalog/product/edit/categories.phtml which would need to be extended.

Just add a class="required-entry" to the element <input type="hidden" name="category_ids" id="product_categories" value="<?php echo $this->getIdsString() ?>">

This will flag up an empty value and highlight the category tab on the product edit screen before posting when no category is selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.