1

I need to send my products for Amazon but I created simple products through configurable products. So simple products are no images.
How can I assign the images of configurable products to simple products to send them the Amazon?

1

First, backup your database in case I'm wrong.
Let's say that $configurableProductId is the configurable product id.
Now you can run this script for a configurable product.

//load the configurable product - not a very fast way to do it, but you need a load to fetch all the images
$confProduct = Mage::getModel('catalog/product')->load($configurableProductId);
//get the images
$gallery = $product->getData('media_gallery');
//get the instance for the media_gallery attribute
$mediaGalleryAttribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'media_gallery');
//get access to a model that handles the image association to products
$resource = Mage::getResourceSingleton('catalog/product_attribute_backend_media');
if (isset($gallery['images'])) {
    //get all simple child products ids 
    $simpleProductIds = $conProduct->getTypeInstance()->getUsedProductIds();
    //for each simple product
    foreach ($simpleProductIds as $productId) {
        //for eahc image from the main product
        foreach ($gallery['images'] as $image) {
            //build an array with imag settings
            $data = array();
            $data['entity_id']      = $parentId;
            $data['attribute_id']   = $mediaGalleryAttribute->getId();
            $data['value']          = $image['file'];
            //save the image and get the id
            $lastId = $resource->insertGallery($data);
            //set the label and position for the new images
            $data = array();
            $data['value_id'] = $lastId;
            $data['label']    = $image['label'];
            $data['position'] = (int) $image['position'];
            $data['disabled'] = (int) $image['disabled'];
            $data['store_id'] = (int) $product->getStoreId();
            //finish the job
            $resource->insertGalleryValueInStore($data);
        }
    }
}

If you need to get all the configurable products ids you can do this:

$productsIds = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('type_id', 'configurable')
    ->getAllIds();
0

Marius's solution is great but it needs further modification to Magento backend media class which has a method _removeDuplicates() to remove duplicate entries from catalog_product_entity_media_gallery.

When catalog loads on frontend it triggers Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media::loadGallery () and Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media::loadGallerySet that implements _removeDuplicates(). This method will initially remove duplicate image entries from catalog_product_entity_media_gallery table resulting in images being lost for both configurable and associated products.

To resolve this we need to uncomment _removeDuplicates() method. Best way is to create new plugin that will override Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media.

  1. Create file M2e_Plugin.xml in the following directory In app/etc/modules/M2e_Plugin.xml put the following content:

    <?xml version="1.0"?>
    <config>
        <modules>
            <M2e_Plugin>
                <active>true</active>
                <codePool>local</codePool>
            </M2e_Plugin>
        </modules>
    </config>
    
  2. Create file config.xml and its new directory in app/code/local/M2e/Plugin/etc/config.xml Put the following content:

    <?xml version="1.0"?>
    <config>
        <modules>
            <M2e_Plugin>
                <version>0.1.0</version>
            </M2e_Plugin>
        </modules>
        <global>
            <models>
                <catalog_resource>
                    <rewrite>
                        <product_attribute_backend_media>M2e_Plugin_Model_Resource_Product_Attribute_Backend_Media</product_attribute_backend_media>
                    </rewrite>
                </catalog_resource>
            </models>
            <helpers>
                <m2e_plugin>
                    <class>M2e_Plugin_Helper</class>
                </m2e_plugin>
            </helpers>
        </global>
    </config>
    
  3. Create Data.php file and its directory as follows app/code/local/M2e/Plugin/Helper/Data.php Content for this file:

    <?php
    class M2e_Plugin_Helper_Data extends Mage_Core_Helper_Abstract 
    {}
    
  4. Create Media.php file (and direcotories) which overrides our target file as follows: app/code/local/M2e/Plugin/Model/Resource/Product/Attribute/Backend/Media.php and its content:

    <?php
    /**
     * Class M2e_Plugin_Model_Resource_Product_Attribute_Backend_Media
     */
    class M2e_Plugin_Model_Resource_Product_Attribute_Backend_Media extends Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
    {
        /**
         * Load gallery images for product using reusable select method
         *
         * @param Mage_Catalog_Model_Product $product
         * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
         * @return array
         */
        public function loadGallery($product, $object)
        {
            $eventObjectWrapper = new Varien_Object(
                array(
                    'product' => $product,
                    'backend_attribute' => $object
                )
            );
            Mage::dispatchEvent(
                $this->_eventPrefix . '_load_gallery_before',
                array('event_object_wrapper' => $eventObjectWrapper)
            );
    
            if ($eventObjectWrapper->hasProductIdsOverride()) {
                $productIds = $eventObjectWrapper->getProductIdsOverride();
            } else {
                $productIds = array($product->getId());
            }
    
            $select = $this->_getLoadGallerySelect($productIds, $product->getStoreId(), $object->getAttribute()->getId());
    
            $adapter = $this->_getReadAdapter();
            $result = $adapter->fetchAll($select);
            /** Overriding bellow to avoid duplicate images removal. */
            // $this->_removeDuplicates($result);
            return $result;
        }
    
        /**
         * Get media gallery set for given product IDs
         *
         * @param array $productIds
         * @param $storeId
         * @return array
         */
        public function loadGallerySet(array $productIds, $storeId)
        {
            $select = $this->_getLoadGallerySelect($productIds, $storeId, $this->_getAttributeId());
    
            $adapter = $this->_getReadAdapter();
            $result = $adapter->fetchAll($select);
            /** Overriding bellow to avoid duplicate images removal. */
            // $this->_removeDuplicates($result);
            return $result;
        }
    }
    

I had similar situation as my client is using M2e Pro to list on amazon. M2E pro has no option to chose which product image is used for listing when submitting configurable type. Many magento owners, who deal with configurable products, do not even bother creating images for simple associated products, so option to use configurable product image for children would be very useful.

Thanks to Marius for pointing in right direction.

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.