How to Set All Child Products to Not Visible Individually in Magento 1.9

magento-1.9PHPproduct

How to set all associated products in the entire store to 'not visible individually' I've been scrolling through other answers but I can't seem to find a solution.

Best Answer

Here is an example script that sets the children of both grouped and configurable products to Not Visible Individually.

Save this in ./shell/makechildrennotvisible.php and run from shell.

<?php
include_once '../app/Mage.php';
Mage::app()->setCurrentStore( Mage_Core_Model_App::ADMIN_STORE_ID );

// Get all configurable and grouped products
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('type_id', array('in' => array('configurable', 'grouped')));

// Loop each configurable product
foreach ($products as $_product) {
    // Get the child products for this product
    if ($_product->getTypeId() == 'configurable') {
        $simpleProducts = $_product->getTypeInstance(true)->getUsedProductIds($_product);
    } elseif ($_product->getTypeId() == 'grouped') {
        $simpleProducts = $_product->getTypeInstance(true)->getAssociatedProductIds($_product);
    }
    printf("Setting children of Product ID %s\r\n", $_product->getId());
    // Loop each child product
    foreach($simpleProducts as $_simpleID) {
        try {
            // Set associated product as not visible
            $_simple = Mage::getModel('catalog/product')->load($_simpleID);
            $_simple->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
            $_simple->save();
            printf("Product ID %s saved as 'Not Visible Individually'\r\n", $_simple->getId());
        } catch ( Exception $e ) {
            print_r($e);
        }
    }
}

EDIT (browser version to process 10 products at a time)

Run this script in the browser as shell/makechildrennotvisible.php?page=1 and increment the page value each time until all products have been procesed. E.g.

www.example.com/shell/makechildrennotvisible.php?page=1
www.example.com/shell/makechildrennotvisible.php?page=2
www.example.com/shell/makechildrennotvisible.php?page=3

<?php
include_once '../app/Mage.php';
Mage::app()->setCurrentStore( Mage_Core_Model_App::ADMIN_STORE_ID );

// Get the current page from $_GET
$curPage = intval(Mage::app()->getRequest()->getParam('page'));
$curPage = $curPage < 1 ? 1 : $curPage;

// Get all configurable and grouped products
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('type_id', array('in' => array('configurable', 'grouped')))
    ->setPageSize(10);

$curPage = $curPage > $products->getLastPageNumber() ? $products->getLastPageNumber() : $curPage;
$products->setCurPage($curPage);

printf("Processing page %s of %s<br/>", $curPage, $products->getLastPageNumber());

// Loop each configurable product
foreach ($products as $_product) {
    // Get the child products for this product
    if ($_product->getTypeId() == 'configurable') {
        $simpleProducts = $_product->getTypeInstance(true)->getUsedProductIds($_product);
    } elseif ($_product->getTypeId() == 'grouped') {
        $simpleProducts = $_product->getTypeInstance(true)->getAssociatedProductIds($_product);
    }
    printf("Setting children of Product ID %s<br/>", $_product->getId());
    // Loop each child product
    foreach($simpleProducts as $_simpleID) {
        try {
            // Set associated product as not visible
            $_simple = Mage::getModel('catalog/product')->load($_simpleID);
            $_simple->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
            $_simple->save();
            printf("Product ID %s saved as 'Not Visible Individually'<br/>", $_simple->getId());
        } catch ( Exception $e ) {
            print_r($e);
        }
    }
}
Related Topic