Sunday, November 10, 2013

Remove the products from category products and update or map new product to category in magento programmatically

In some time you need to remove all the products from category or unset all products from category and add or update new product to category at that time you use below code for ex:- you need to run cron job which will run every day and get a product collection of sale attribute and map that products to that category
    define('MAGENTO', '..');
    require_once MAGENTO . '/app/Mage.php';

    //Load product model collecttion filtered by sale attribute
     $proCollection = Mage::getModel('catalog/product')
                        ->addAttributeToFilter('sale', '1');
$writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');

        // Delete Existing Mapped product from Sale Category
        $delQuery = 'Delete from catalog_category_product where category_id ='.$catId;
        $products = array();
        foreach ($proCollection as $product){
                $category_products .=$product->getId().'=1&';            
        parse_str($category_products, $products);    
        $process = Mage::getModel('index/indexer')->getProcessByCode('catalog_category_product');
        echo 'successfully mapped the data<br>';

Download file setSale.php

No comments:

Post a Comment