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

发布于 2024-12-21 19:00:10 字数 274 浏览 1 评论 0原文

已经过去 2 天了,我看到了很多教程、视频和扩展,它们解释了如何对我的产品进行日常更新,但似乎都不起作用。

我有一个网站,其中包含大约 3,000 种具有属性的产品。我的供应商每天更新库存文件,更新后的库存文件保存在我有权访问的远程 ftp 上。

我需要的是以下内容:

  1. 每日库存更新,其中包括价格、数量等
  2. 删除数量为 0 的商品
  3. 根据更新的库存文件添加新商品

我确信有人已经回答了我的问题。所以请尽快回复我。

It’s been 2 days and I came across many tutorials, videos, extension which explains how to do daily updates on my products but none of them seem to work.

I have a website which has around 3,000 products with attributes and all. My supplier updates the inventory file on daily basis and the updated inventory file is kept on a remote ftp to which I have access to.

What I need is the following:

  1. Daily inventory update which will include, prices, quantity, etc
  2. Remove items which has quantity 0
  3. Add new items as per the updated inventory file

I am sure someone out there has answer to my questions. So please get back to me asap.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-12-28 19:00:10

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

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

如果你不介意的话,还有一些教程:

教程:使用 Magento 的导入/导出配置文件

如何使用导入/导出工具添加/编辑/删除产品

Fisrt, by "Remove items which has quantity 0", I think you mean to set products as "out of stock" when the inventory is decreased to 0. This is handled automatically by Magento after you update the inventory.

The built-in import/export function should fulfill your requirements.

Some more tutorials if you don't mind:

Tutorial: Using Magento’s Import/Export Profiles

How to add/edit/remove products using the import/export tool

一身软味 2024-12-28 19:00:10

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

SKU 数量
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 = '[email protected]';

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: [email protected]'. '\n';

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

}
?>  

该脚本由 Deepcodeonline

Here, I will share just inventory update ( but this is not good way ). You should create "tab delimited" file as follows :

SKU QTY
978 34
633 0

Then you can create cron job to run this script.

<?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 = '[email protected]';

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: [email protected]'. '\n';

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

}
?>  

The script by courtesy of the Deepcodeonline

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文