您的位置:首页 > 编程语言

【代码片段】根据分类名称,拷贝产品主图到指定目录

2018-02-27 09:21 411 查看
<?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;

// 图片将来要存放的目录
// 先手动创建product_images目录
$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;";
//echo $sql;exit;
$query = $read->query($sql);

// 遍历
while ($row = $query->fetch()){
//var_dump($row);

$sku = $row['sku'];
$img_url = $row['img_url'];

//echo $sku.'==>'.$img_url.'<br/>';

// 老文件
$old_file ='media/catalog/product'.$img_url;
//echo $old_file;exit;

if(file_exists($old_file)){
// 文件扩展
$extension = pathinfo($old_file, PATHINFO_EXTENSION);
$file_name = $sku.".".$extension;

// 新路径
$new_file = $save_dir."/".$file_name;
//echo $new_file;exit;
copy($old_file,$new_file);

}else{
echo "file not exist \r\n";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐