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

<?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";
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值