Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1.  
  2.     function importImageByFilenames() {
  3.  
  4.         $logFile = Mage::getBaseDir('media') . "/sku_import.log";
  5.         $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
  6.  
  7.         $importPath = Mage::getBaseDir('media') . "/importproducts/images/";
  8.         $catalogPath = Mage::getBaseDir('media') . "/catalog/product";
  9.         $skus = array();
  10.         $linked = array();
  11.  
  12.         $files = scandir($importPath);
  13.  
  14.         foreach ($files as $file) {
  15.             $re = '/(?<sku>.*?)(-|\.|_)(?<index>\d)?/';
  16.             preg_match($re, $file, $matches);
  17.  
  18.             if(empty($matches['sku'])) continue;
  19.  
  20.             $index = empty($matches['index']) ? 1 : (int) $matches['index'];
  21.             $image = array(
  22.                 'fileName' => $file,
  23.                 'index' => $index
  24.             );
  25.             $skus[] = $matches['sku'];
  26.             $linked[$matches['sku']][] = $image;
  27.         }
  28.  
  29.         $uniqueSkus = array_unique($skus);
  30.         $_products = Mage::getResourceModel('reports/product_collection')->addAttributeToSelect('*')
  31.             ->addFieldToFilter('sku',array('in'=>$uniqueSkus));
  32.  
  33. //        $existSkus = array();
  34.  
  35.         foreach ($_products as $_p) {
  36.             $_product = Mage::getModel('catalog/product')->load($_p->getId());
  37.             $gallery = $_product->getMediaGallery();
  38.  
  39.             if (!empty($gallery['images'])) {
  40.                 // need to remove if not empty
  41. //                foreach ($gallery['images'] as $img) {
  42. //                    if ($img['position'] == 1) {
  43. //                        $_product->setImage($img['file']);
  44. //                        $_product->setSmallImage($img['file']);
  45. //                        $_product->setThumbnail($img['file']);
  46. //                        $_product->save();
  47. //                        echo "set as default {$img['file']} \n";
  48. //                    }
  49. //                    print_r($img);
  50. //                    $ph = $catalogPath.''.$img['file'];
  51. //                    if (!file_exists($ph)) {
  52. //                        $mediaApi->remove($_p->getId(), $img['file']);
  53. //                        echo "removed {$img['file']} \n";
  54. //                    }
  55.  
  56.                 }
  57.  
  58.             } else {
  59.                 //add image
  60.                 //echo "{$_p->getSku()}\n";
  61.  
  62.  
  63. //                foreach ($linked[$_p->getSku()] as $item) {
  64. //                    $pathToFile = $importPath .''. $item['fileName'];
  65. //                    $media_attr = null;
  66. //                    if ($item['index'] == 1) $media_attr = array('image', 'small_image', 'thumbnail');
  67. //
  68. //                    if(file_exists($pathToFile)) {
  69. //                        $_product->addImageToMediaGallery ($pathToFile, $media_attr, false, false);
  70. //                        $msg = "Sku: {$_p->getSku()}Image {$item['fileName']} added \n";
  71. //                        echo $msg;
  72. //                        file_put_contents($logFile,$msg,FILE_APPEND);
  73. //                    }
  74. //
  75. //                }
  76. //                try {
  77. //                    $_product->save();
  78. //
  79. //                } catch (Exception $e) {
  80. //                    $msg = "Sku: {$_p->getSku()}Image $e \n";
  81. //                    echo $msg;
  82. //                    file_put_contents($logFile,$msg,FILE_APPEND);
  83. //                }
  84.  
  85.  
  86.             }
  87.  
  88.  
  89. //            $existSkus[] = $_product->getSku();
  90.         }
  91. //        $diff = array_diff($uniqueSkus,$existSkus);
  92. //        print_r(count($_products));
  93. //        print_r(count($linked));
  94. //        print_r(count($uniqueSkus));
  95. //        print_r($diff);
  96.  
  97.  
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement