toannk
9/4/2015 - 3:08 AM

magento_update_products.php

<?php
set_time_limit(0);
require_once('app/Mage.php');
umask(0);
Mage::app();

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('type_id', array('eq' => 'configurable'))
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED));

$outOfStockConfis = array();

$_productModel = Mage::getModel('catalog/product');
$_productTypeConfigurableModel = Mage::getModel('catalog/product_type_configurable');
$_productStatusModel = Mage::getModel('catalog/product_status');

$i = 1;
$j = 1;
foreach ($collectionConfigurable as $_configurableProduct) {
    $product = $_productModel->load($_configurableProduct->getId());
    echo $i . '. ' . $product->getName() . '<br/>';

    /*if (!$product->getData('is_salable')) {
        $outOfStockConfis[] = $product->getId();
    }*/

    /**
     * Get children products (all associated children products data)
     */
    /*$childProducts = $_productTypeConfigurableModel->getUsedProducts(null,$product);
    foreach($childProducts as $child) {
        echo '---' . $child->getName() . '<br/>';
    }*/

    $conf = $_productTypeConfigurableModel->setProduct($product);
    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    if (sizeof($simple_collection) > 0) {
        foreach($simple_collection as $simple_product){
            //echo '---' . $simple_product->getSku() . " - " . $simple_product->getName() . " - <strong>" . $simple_product->getStatus() . "</strong><br/>";
            if ($simple_product->getStatus() !== Mage_Catalog_Model_Product_Status::STATUS_DISABLED) {
                $_productStatusModel->updateProductStatus($simple_product->getId(), 1, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
                echo '---' . $simple_product->getSku() . " - " . $simple_product->getName() . ' updated to ' . $simple_product->getStatus() . '<br/>';
                $j++;
            }
        }
    }

    $i++;
}

/*public function updateProductStatus($productId, $status, $store = null)
{
    if (is_null($store)) {
        foreach (Mage::app()->getStores() as $store) {
            $this->updateProductStatus($productId, $status, $store->getId());
        }
        return $this;
    }

    if ($status == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
        $this->_getResource()->updateProduct($productId, $store);
        $this->_getResource()->updateChildrenDataFromParent($store, $productId);
    }
    else { //IF the product is disabled it is removed from the flat tables
        $this->_getResource()->removeProduct($productId, $store);
    }

    return $this;
}*/