0

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

我已经安装了它,还有“图像属性处理器”插件。怎么办?sourceforge 上的文档很薄,我似乎在谷歌上也找不到太多。'如何'将不胜感激,或者嘿,甚至是教程的链接。

谢谢你。干杯!

4

2 回答 2

1

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

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

看到你的问题,想起了我的挣扎,我想和你分享。希望它也能帮助你;)

该脚本使用 Magento-db 中的一个表,其中包含图像路径和 sku。它从路径中提取图像,将它们直接导入 Magento db。

在实际站点上触发之前,请小心并在测试环境中进行测试。彻底测试它以确保它不会弄乱您的 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>";
于 2012-09-05T07:09:09.840 回答
0

我遇到过同样的问题。它很脆。我意识到它已经创建了一个新./magento文件夹,并将图像转储在那里。我将它合并到我真正的 magento 文件夹中并运行重新索引来修复它。

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

我的设置是这样的:

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

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

/image.jpg仅供参考在我的电子表格中,我像前面的斜线一样放入了图像列。

于 2013-07-09T12:42:28.997 回答