3

I need to apply configurable product images to our simple product associated because I need to use simple products in another store where it will only show simple products and not show configurable products.

I use configurable products just to generate simple products but I don't know which setting inside Magento itself will allow applying these images to our simple products.

So, when I put the simple generated products to appear in the store, they appear without images because only the configurable product receives these images.

Is there a setup or module that applies images to configurable products and associated simple products?

PHP URL Code
https://santosmadeiras.com.br/to.php

enter image description here

1 Answer 1

4

I've created a script for the same. You can achieve this by executing the script on your website.

For Magento 2

<?php 

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productCollection = $objectManager->create('Magento\Catalog\Model\Product');

$configProductIds = [1,2,3,4,5,6]; //configurable product ids to update images

foreach ($configProductIds as $product_id) {
    $configProduct = $productCollection->load($product_id);

    $image = $configProduct->getImage();
    $thumbnail = $configProduct->getThumbnail();
    $smallImage = $configProduct->getSmallImage();

    $_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);

    foreach ($_children as $child){
        $childProduct = $productCollection->load($child->getId());
        $childProduct->addImageToMediaGallery($image, ['image', 'small_image', 'thumbnail'], false, false);
        $childProduct->save();
    }
}

For Magento 1

<?php

define('MAGENTO_ROOT', getcwd());

include_once "app/Mage.php";

ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);

Mage::init();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
umask(0);
Mage::app('admin');

set_time_limit(0);
ini_set('memory_limit', '2048M');

try {

    $configProductIds = [1,2,3,4,5,6]; //configurable product ids to update images

    foreach ($configProductIds as $product_id) {
    $configProduct = Mage::getModel('catalog/product')->load($product_id);

    $image = $configProduct->getImage();
    $thumbnail = $configProduct->getThumbnail();
    $smallImage = $configProduct->getSmallImage();

    $childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($configProduct->getId());

    foreach ($childIds as $childId){
        $childProduct = Mage::getModel('catalog/product')->load(childId);
        $childProduct->addImageToMediaGallery($image, array('image', 'small_image', 'thumbnail'), false, false);
        $childProduct->save();
    }

} catch (Exception $e) {
    print_r($e->getMessage());
}

Hope it helps!!!

7
  • I need to convert this from Magento 1.9.x ... Can you help me? Commented Sep 18, 2019 at 11:11
  • 1
    Updated my answer with Magento 1 script.
    – Sumit
    Commented Sep 18, 2019 at 11:39
  • Script for Magento 1.9.x.x work like a charm but the url image don't save on simple associated products. Image and code url on updated question. Commented Sep 18, 2019 at 12:26
  • Issue is with the product attribute name, updated my answer, please check.
    – Sumit
    Commented Sep 18, 2019 at 12:34
  • 1
    Fixed !! For all others that search for this, the absolute $image path its necessary on magento 1.9.x.x script to write path on database. Thanks Sumit. Commented Sep 18, 2019 at 15:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.