<?php
error_reporting(E_ALL);
set_time_limit(0);
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$categories = array(
"Drying Racks & Hangers",
"Animals & Pet Supplies"
);
foreach($categories as $item){
$category_name = $item;
$save_dir = "product_images/".$category_name;
if(!is_dir($save_dir)){
mkdir($save_dir, 0700);
}
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT t2.entity_id AS product_id,t2.sku,t5.`value` AS img_url FROM catalog_category_product AS t1
LEFT JOIN catalog_product_entity AS t2 ON t2.entity_id=t1.product_id
LEFT JOIN catalog_category_entity_varchar AS t3 ON t3.entity_id=t1.category_id
LEFT JOIN catalog_product_entity_int AS t4 ON t4.entity_id=t2.entity_id
LEFT JOIN catalog_product_entity_varchar AS t5 ON t5.entity_id=t1.product_id
WHERE t3.`value`='{$category_name}' AND t4.attribute_id=96 AND t4.`value`=1
AND t5.attribute_id=85;";
$query = $read->query($sql);
while ($row = $query->fetch()){
$sku = $row['sku'];
$img_url = $row['img_url'];
$old_file ='media/catalog/product'.$img_url;
if(file_exists($old_file)){
$extension = pathinfo($old_file, PATHINFO_EXTENSION);
$file_name = $sku.".".$extension;
$new_file = $save_dir."/".$file_name;
copy($old_file,$new_file);
}else{
echo "file not exist \r\n";
}
}
}