1

How to display special price products in Sale category by default

Not like selecting products while creating categories or selecting category while adding products.

0

1 Answer 1

2

We can do this using event > observer. Using catalog_product_prepare_save event of admin we can able to do this possible.

           <adminhtml>
             <events>
                <catalog_product_prepare_save>
                    <observers>
                        <Modulename>
                            <type>model</type>
                            <class>Modulename/observer</class>
                            <method>onProductPrepareSave</method> 
                        </Modulename>
                    </observers>
                </catalog_product_prepare_save>
           </events>    
        </adminhtml>

Use below function in your module observer

class MyCompany_Modulename_Model_Adminhtml_Observer
{
  /**
    * Assign category 'sale' when product having sale price
    * 
    * @param Varien_Event_Observer $observer
    */
    public function onProductPrepareSave(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();     
        $request = $observer->getEvent()->getRequest();     
        $postData = $request->getPost();        
        $categoryIds = array();
        $categoryIds = $product->getCategoryIds();
        if(isset($postData['product']['special_price'])) {
            if(!in_array('ID of Sale Cat',$categoryIds)) {
                $saleCatId = array('ID of Sale Cat');
                $updateCatIds = array_merge($categoryIds,$saleCatId);
                $product->setCategoryIds($updateCatIds);
            }
        }

    }
}

Replace 'Modulename' with your module. Change 'ID of Sale Cat' with your 'Sale' category id.

I hope above code will help you.

0

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.