通过远程FTP文件更新每日库存

时间:2011-12-16 11:17:51

标签: magento

已经过了两天,我遇到了许多教程,视频和扩展,它解释了如何对我的产品进行每日更新,但似乎没有一个更新。

我的网站有大约3,000种具有属性的产品。我的供应商每天更新库存文件,更新的库存文件保存在我可以访问的远程ftp上。

我需要的是以下内容:

  1. 每日库存更新,包括价格,数量等
  2. 删除数量为0的项目
  3. 根据更新的广告资源文件添加新项目
  4. 我相信有人会回答我的问题。所以请尽快回复我。

2 个答案:

答案 0 :(得分:0)

Fisrt,通过“删除数量为0的商品”,我认为您的意思是在库存减少到0时将产品设置为“缺货”。这是由Magento在您更新库存后自动处理的。

内置导入/导出功能应满足您的要求。

如果你不介意的话,还有一些教程:
Tutorial: Using Magento’s Import/Export Profiles
How to add/edit/remove products using the import/export tool

答案 1 :(得分:0)

在这里,我将分享库存更新(但这不是好方法)。您应该创建“制表符分隔”文件,如下所示:

SKU QTY
978 34
633 0

然后您可以创建cron作业来运行此脚本。

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
set_time_limit(0);
ini_set('memory_limit','1024M');

include('app/Mage.php');
Mage::App('default');

//get magento db connection for query
$db_query = Mage::getSingleton('core/resource')->getConnection('core_read');

//get magento db connection for update, add, delete sql statements
$db_update = Mage::getSingleton('core/resource')->getConnection('core_write');

//mail receipent
$to = 'youremail@yoursite.com';

if ($handle = opendir('./var/export'))
{
    while (false !== ($file = readdir($handle)))
    {
        $sku_success = '';
        if ($file != '.' && $file != '..')
        {
            $filename = './var/export/'.$file;
            $handle_file = fopen($filename,'r');
            $counter = 1;
            while (($data = fgetcsv($handle_file, 1000, '\t'))!== FALSE) //read tab delimited
            {
                if($counter!=1)
                {
                    $sku= $data[0];
                    $qty = $data[1];

                    $exists = $db_query->query("SELECT COUNT(sku) cnt FROM catalog_product_entity WHERE sku = '$sku' LIMIT 1");
                    $found = (($exists->fetchObject()->cnt) > 0) ? true : false;

                    if ($found == true) //product found, update
                    {
                        $entity_id = getEntityIdBySku($db_query, $sku);
                        updateStockQty ($db_update, $entity_id, $qty);
                        echo 'product $sku inventory updated?';
                        $sku_success.= $sku;
                    } else {
                        //sku not found, notify by email
                        $body = "SKU#:".$sku." of ".$file." with a qty of ".$qty." was not found in the the inventory";
                        sendMsg("SKU#:".$sku." not found!",$to,$body);
                    }

                }
                $counter++;
            }

            fclose($handle_file);

            $body = "<strong>SKU?S IMPORTED</strong>".$sku_success."<br/>";
            sendMsg($file." successfully imported!",$to,$body);

        }
    }
    closedir($handle);
}

function getEntityIdBySku($db_connection, $sku) //get entity id by its sku
{
    $entity_row = $db_connection->query("SELECT entity_id FROM catalog_product_entity p_e WHERE p_e.sku = '$sku'")->fetchObject();
    $entity_id  = $entity_row->entity_id;
    return $entity_id;
}

function updateStockQty($db_connection, $entity_id, $qty) //updates the stock quantity
{
     $db_connection->query("UPDATE cataloginventory_stock_item s_i, cataloginventory_stock_status s_s
         SET   s_i.qty = '$qty', s_i.is_in_stock = IF('$qty'>0, 1,0),
               s_s.qty = '$qty', s_s.stock_status = IF('$qty'>0, 1,0)
         WHERE s_i.product_id = '$entity_id' AND s_i.product_id = s_s.product_id ");
}

function sendMsg($subject, $to, $body)//sends email
{
    $headers  = 'MIME-Version: 1.0' . '\n';
    $headers .= 'Content-type: text/html; charset=iso-8859-1'. '\n';
    $headers .= 'To: '.$to. '\n';
    $headers .= 'From: webmaster@yoursite.com'. '\n';

    if (mail($to, $subject, $body,$headers))
    {
        echo('Message sent!');
    } else {
        echo('Message failed.');
    }

}
?>  

该脚本由Deepcodeonline

提供