3

I have a huge catalog of products which all have about 4 images per product with the same naming structure being something like "xxxxxx_FR.jpg" "xxxxxx_BK.jpg" "xxxxxx_FL.jpg" where the x's represent the product's ID and FR, BK, FL stand for front, back, full length etc.

Currently Base, Small and Thumbnail are all assigned to the FR images, but what I'm trying to do is change just the Thumbnail image for each product to the FL image from the gallery programatically. I am approaching this by building a custom product collection, going through the products and then loading the following:

foreach ($_productCollection as $product){
   $workingProduct = Mage::getModel('catalog/product')->load($productID);
   $gallery = $workingProduct->getMediaGalleryImages();  
   $cameraAngle = str_replace(".jpg","",explode("_",$image->getUrl())); //This results in string that is just FR, BK, FL etc.

if (strtolower($cameraAngle[1]) == 'fl') {
//I have tried both of the below methods
$workingProduct->setThumbnail($image->getUrl());
$product->addImageToMediaGallery($image->getUrl(),array('thumbnail'),false,false);
 $workingProduct->save();
}    
}

The first method doesn't do anything and the second method adds a new image into the image gallery (which I don't want) but it doesn't change the thumbnail image either. I am thinking that there is more potential in method 1 as I do not want to import or create a new image in the gallery, but I'm not sure what I'm doing wrong.

2 Answers 2

1

If I understand you correctly, you just want to change FR to FL for the thumbnail?

Just do this:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('thumbnail);
foreach($collection as $product) {
    $product->setThumbnail(str_replace('_FR', '_FL', $product->getThumbnail());
}
$collection->save();
0

My Solution was this for anyone else looking...

   foreach ($gallery as $image){
        $cameraAngle = str_replace(".jpg","",explode("_",$image->getUrl()));

        if (strtolower($cameraAngle[1]) == 'fl') {               
            Mage::getSingleton('catalog/product_action')->updateAttributes(array($workingProduct->getId()), array('thumbnail'=>$image['file']), 0);

        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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