2

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.

  • You'd need to write a little script that loops through all configurable products, finds their children, loops through each child, sets the visibility, then saves each child. Let me know if you need some example code. – BlueC Sep 11 '16 at 7:40
2

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);
        }
    }
}
  • I have updated this answer to work with both grouped and configurable products and also to ensure the CurrentStore is set appropriately. Please let me know if it works and accept the answer if it does. – BlueC Sep 12 '16 at 15:00
  • Hi @bluec I haven't had a chance to run this yet but I have a few questions. Do I need to replace ADMIN_STORE_ID with the appropriate store I'd? Can you run this so it changes all stores products not just the selected store? Also I don't have shell access could I run this through the browser? – Pragman Sep 12 '16 at 18:08
  • @ZUBU You can leave Mage_Core_Model_App::ADMIN_STORE_ID exactly as it is since without this the product changes won't save. It will already change the visibility of all products from all stores. However, if you have product visibility set differently for different store scopes and wish to override this too then this would need some changes. You could run it in the browser but I don't recommend it. To do so you'd need to move it out of ./shell/ into the root directory and then change include_once '../app/Mage.php'; to include_once 'app/Mage.php'; – BlueC Sep 12 '16 at 19:56
  • I was somewhat successful with the script but the script seemed to stop and not loop through the entire catalog??? Also when I re ran the script it had the exact same output as before i.e. changing the visibility of products it had already changed. I tried clearing cache and reindexing to no avail. So after trying multiple clearing cache etc after running the script for the tenth time it still returns the same as the first time running it. I am running the script through the browser. Any ideas? – Pragman Sep 13 '16 at 7:51
  • 1
    Ok just tried clearing the storage cahce and rerunning and it got about twice as far. Leads me to believe it might be a memory or time out issue??? – Pragman Sep 13 '16 at 7:54

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.