使用Magmi进行批量图像导入

时间:2012-09-04 14:18:19

标签: image magento plugins import magmi

我的问题:你如何使用Magmi将批量图像导入Magento?

我已经安装了它,还获得了“图像属性处理器”插件。怎么办?关于so​​urceforge的文档很薄,我似乎也无法在谷歌上找到太多。一个'如何'将被赞赏,或者嘿,甚至是教程的链接。

谢谢。干杯!

2 个答案:

答案 0 :(得分:1)

为了转换为Magento,我开始使用Magmi来导入产品数据和图像。半天后,我停止了图像方面因为我无法让它正常工作。

做了一些调查和谷歌搜索,并通过一个脚本来导入图像。不知道从哪里了,但道具给基地的创造者!

看到你的问题并记住我的挣扎,我想与你分享。希望它也可以帮助你;)

该脚本使用Magento-db中的一个表来保存图像路径和sku。它从路径中提取图像,将它们直接导入Magento数据库。

在测试环境中小心并测试它,然后在现场点击它。彻底测试,以确保它不会弄乱您的magento数据库。你也可能想检查image_attribute_id是否正确,因为根据我注意到的Magento版本,这些是不同的。

希望有所帮助:)

############################################################################
#
#  Change these variables to suit your store!
#
$_mediaBase = realpath('../media/catalog/product').'/';
$_imagesSrc = realpath('../media/import/_osc_media').'/';
// Image attribute_id's: image=79, small_image=80, thumbnail=81, gallery=82
$imgAttrIds = array(79,80,81,88);
$imageGalId = array(82);
require_once '../app/Mage.php';
#
############################################################################
#
#  Debug (true/false)
#
    $debug = false;
#
############################################################################

umask(0);
error_reporting(E_ALL);
ini_set('display_errors', '1');
Mage::setIsDeveloperMode(true);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$conn = Mage::getSingleton('core/resource')->getConnection('core_read');
$connW = Mage::getSingleton('core/resource')->getConnection('core_write');
//$images = scandir($_imagesSrc);
/*
*    Relocate the images into Magento friendly format
*/
$imgArr = array();
$imgSkuArr[] = array();
$imgSrcArr[] = array();

$sql = "SELECT sku, image_path
          FROM aa_image_paths";
$images = $conn->fetchAll($sql);
/**
 * $images =
 * [8] => Array(
 *   [sku] => ST-P006
 *   [image_path] => 47/ST-P006/Carying bag Heli P005A.jpg
 * )
 */
#debug
if($debug == true){
    echo '<p>Images: <pre>';
    print_r($images);
    echo '</pre>';
}//end: debug

//iterator
$y = $z = 0;

foreach($images as $image)
{
    $pathChunks = explode('/', $image['image_path']);
    $imgFile = end($pathChunks);

    $firstDir = strtolower($_mediaBase . substr($imgFile,0,1));
    $secondDir = strtolower($firstDir.'/' . substr($imgFile,1,1));
    $imageEntityDir = strtolower('/' . substr($imgFile,0,1) . '/' . substr($imgFile,1,1) . '/' . $imgFile);

    #debug
    if($debug == true){
        echo '<p>imgFile: ' . $imgFile . '<br />imageEntityDir: ' . $imageEntityDir  . '</p>';
    }//end: debug

    /**
     * Get the product_entity details
     */
    $sql = "SELECT *
          FROM catalog_product_entity
          WHERE sku = '" . $image['sku'] . "'";
    $_products = $conn->fetchAll($sql);

    #debug
    if($debug == true){
        echo '<p>SQL: ' . $sql . '<br />';
        echo '_products:<br />';
        echo '<pre>';
        print_r($_products);
        echo '</pre></p>';

        echo (isset($_products)) ? '<h6>products isset() = true</h6>' : '<h6>products isset() = false</h6>';
        echo (!empty($_products))? '<h6>products empty() = false</h6>' : '<h6>products empty() = true</h6>';
    }//end:debug

    if(!empty($_products))
    {
        if(file_exists($_imagesSrc . $image['image_path'])){

            $path = strtolower($secondDir . '/' . $imgFile);
            if(!file_exists($path)) {
                echo "Making $secondDir and copying to $path";
                /**
                 * If NOT debug than do copy and mkdir stuff!
                 */
                if($debug == false)
                {
                    if (!file_exists($secondDir)){
                        mkdir($secondDir, 0777, true);
                    }
                    copy($_imagesSrc . $image['image_path'], $path);

                }//end: debug FALSE


            }//end:if(!file_exists($path)) {

            #debug
            if($debug == true){
                echo '<p>_imagesSrc + image_path: ' . $_imagesSrc . $image['image_path'] . '<br />';
                echo 'image[image_path]' . $image['image_path'] . '<br />';
                echo 'path: ' . $path . '<br />';
                echo 'image[sku]: ' . $image['sku'] . '<br />';
                echo 'imgEntDir: ' . $imageEntityDir . '<br />';
                echo '</p>';
            }//end: debug

            /**
             * Create the array for the product with the data.
             * @var $product
             */
            $productData = array();
            $productData['sku'] = $image['sku'];
            $productData['entity_id'] = $_products[0]['entity_id'];
            $productData['imgSrc'] = $imageEntityDir;

            #debug
            if($debug == true){
                echo 'productData: ' .             $productData . '<br />';
                echo 'productData[sku]: ' .        $productData['sku'] . '<br />';
                echo 'productData[entity_id]: ' .  $productData['entity_id'] . '<br />';
                echo 'productData[imgSrc]: ' .     $productData['imgSrc'] . '<br />';
                echo 'productData: <pre>';
                var_dump($productData);
                echo '</pre></p>';
            }//end:debug

            /**
             *    Check the existing images
             */
            // General Base, small and thumb images
            $sql = "SELECT *
                FROM catalog_product_entity_varchar
                WHERE entity_id = '" . $productData['entity_id'] . "'
                AND attribute_id IN (".implode(",",$imgAttrIds).")";
            $_imageAssoc = $conn->fetchAll($sql);

            $existingImgs = array();
            foreach($_imageAssoc as $img)
            {
                $existingImgs[$img['entity_id']][$img['attribute_id']] = $img;
            }

            // Gallery Images
            $sql = "SELECT *
                    FROM catalog_product_entity_media_gallery
                    WHERE entity_id = '" . $productData['entity_id'] . "'";
            $_galleryImgs = $conn->fetchAll($sql);

            $existingGall = array();
            foreach($_galleryImgs as $img)
            {
                $existingGall[$img['entity_id']][$img['attribute_id']] = $img;

                #debug
                if($debug == true){
                    echo '<p>img print_r: <br /><pre>';
                    print_r($img);
                    echo '</pre></p>';
                }//end:debug
            }

            #debug
            if($debug == true){
                # print existingImg
                echo '<p>existingImgs print_r: <br /><pre>';
                print_r($existingImgs);
                echo '</pre></p>';
                # print existingGal
                echo '<p>existingImgs print_r: <br /><pre>';
                print_r($existingImgs);
                echo '</pre></p>';
            }//end:debug

            /**
             *    Then associate to the product itself.
             */
            //$insertData = array();
            //$skusToInsert = array();
            foreach($_products as $productArrId=>$product) {
                $missingImgs = $imgAttrIds;

                //$imageName = strtolower('/'.$product['sku'][0].'/'.$product['sku'][1].'/'.$product['sku'].'.jpg');
                $imageName = $productData['imgSrc'];

                #debug
                if($debug == true){
                    echo '<p>Imagename print_r: <br /><pre>';
                    print_r($imageName);
                    echo '</pre></p>';
                }//end:debug

                // Check if it has an image associated already ...
                if ( array_key_exists( $productData['entity_id'], $existingImgs) ) {
                    // Check which images exists and remove those already set
                    foreach ($imgAttrIds as $id=>$val) {
                        if ( array_key_exists( $val , $existingImgs[$productData['entity_id']] ) ) {
                            //if ( $existingImgs[$productData['entity_id']][$val]['value'] == "no_selection" ) {
                                $sql = "DELETE FROM catalog_product_entity_varchar WHERE value_id = '".$existingImgs[$productData['entity_id']][$val]['value_id']."'";

                                #debug
                                if($debug == true)
                                {
                                    echo 'SQL base images: <pre>' . $sql . '</pre>';
                                }
                                else
                                {
                                    //Execute the Query
                                    $connW->query($sql);
                                }

                            //} else {
                                unset($missingImgs[$id]);
                            //}

                        }//end:f ( array_key_exists( $val

                    }//end: foreach ($imgAttrIds as $id=>$val) {

                }//end;if ( array_key_exists( $productData['entity_id'], $existingImgs) ) {

                // Check if it has Gallery images associated already ...
                if ( array_key_exists( $productData['entity_id'], $existingGall) ) {
                    // Check which images exists and remove those already set
                    foreach ($imageGalId as $id=>$val) {
                        if ( array_key_exists( $val , $existingGall[$productData['entity_id']] ) ) {
                            //if ( $existingImgs[$productData['entity_id']][$val]['value'] == "no_selection" ) {
                            $sql = "DELETE FROM catalog_product_entity_media_gallery_value WHERE value_id = '".$existingGall[$productData['entity_id']][$val]['value_id']."'";

                            #debug
                            if($debug == true)
                            {
                                echo 'SQL gallery Value: <pre>' . $sql . '</pre>';
                            }
                            else
                            {
                                //Execute the Query
                                $connW->query($sql);
                            }

                            $sql = "DELETE FROM catalog_product_entity_media_gallery WHERE value_id = '".$existingGall[$productData['entity_id']][$val]['value_id']."'";

                            #debug
                            if($debug == true)
                            {
                                echo 'SQL gallery: <pre>' . $sql . '</pre>';
                            }
                            else
                            {
                                //Execute the Query
                                $connW->query($sql);
                            }

                            //} else {
                            unset($missingImgs[$id]);
                            //}

                        }//end:f ( array_key_exists( $val

                    }//end: foreach ($imgAttrIds as $id=>$val) {

                }//end;if ( array_key_exists( $productData['entity_id'], $existingImgs) ) {

                foreach ($imgAttrIds as $id=>$val) {

                    //Update the Media gallery items in DB
                    $sql = "INSERT INTO catalog_product_entity_varchar (entity_type_id, attribute_id, store_id, entity_id, value) VALUES " .
                           "(4, " . $val . ", 0, " . $productData['entity_id'] . ", '" . $imageName . "')";

                    #debug
                    if($debug == true)
                    {
                        echo 'SQL Insert1: <pre>' . $sql . '</pre>';
                    }
                    else
                    {
                        //Execute the Query
                        $connW->query($sql);
                    }
                    $y++;
                }//end:foreach ($imgAttrIds as $id=>$val) {

                foreach ($imageGalId as $id=>$val) {
                    //Update the
                    $sql = "INSERT INTO catalog_product_entity_media_gallery (attribute_id, entity_id, value) VALUES " .
                           "(".$val.", ".$productData['entity_id'].", '".$imageName."')";
                        //implode(",",$skusToInsert) . ";";

                    #debug
                    if($debug == true)
                    {
                        echo 'SQL Insert1: <pre>' . $sql . '</pre>';
                    }
                    else
                    {
                        //Execute the Query
                        $connW->query($sql);
                    }

                    //Update the Gallery value
                    /*
                    $sql = "INSERT INTO catalog_product_entity_media_gallery_value (value_id, store_id, position, disabled) VALUES " .
                            "((SELECT value_id FROM catalog_product_entity_media_gallery
                                WHERE attribute_id = '".$imageGalId."'
                                AND entity_id = '".$productData['entity_id']."'
                                AND value = '".$imageName."'),0, 1, 0)";
                    */
                    $sql = "INSERT INTO catalog_product_entity_media_gallery_value (value_id, store_id, position, disabled) " .
                            "SELECT value_id, 0 store_id, 1 position, 0 disabled
                                FROM catalog_product_entity_media_gallery
                                WHERE attribute_id = '".$val."'
                                AND entity_id = '".$productData['entity_id']."'
                                AND value = '".$imageName."'";
                    #debug
                    if($debug == true)
                    {
                        echo 'SQL Insert1: <pre>' . $sql . '</pre>';
                    }
                    else
                    {
                        //Execute the Query
                        $connW->query($sql);
                    }
                    $z++;
                }//end:foreach ($imgAttrIds as $id=>$val) {

            }//end:foreach($_products as $productArrId=>$product) {

        }//end: if(file_exists($_imagesSrc . $image['image_path']))

    }//end: if(!empty($_products))

    #debug
    if($debug == true){
        echo '<hr>';
    }//end:debug

}//end:foreach($images as $image)

echo "<h5>Updated ". $y ." images for products. </h5>";
echo "<h5>Updated ". $z ." images in MediaGallery</h5>";

答案 1 :(得分:0)

我有同样的问题。它非常脆弱。我意识到它已经创建了一个新的./magento文件夹,并将图像转储到那里。我把它合并到我真正的magento文件夹&amp;运行reindex来解决它。

然后,我看到“Magento的路径”设置为../../magento/,将其更改为../../../magento/

我的设置是这样的:

./magento/...files..
./magmi/...files...

它已将图像转储到./magmi/magento/media/的文件夹中,因为在告诉magmi我的Magento文件夹的路径时,我错过了../。这不起作用,因为现在它说它无法在./media/import中找到源图像。唯一可行的解​​决方案是在每次导入后手动将magento文件夹合并到一起。真是一团糟。

FYI在我的电子表格中,我将图像列/image.jpg放入上面的斜杠中。