dongyounai6281 2014-08-14 14:40
浏览 53
已采纳

如何以编程方式导入magento产品时填充库存表

I have a Magento input script that I have written to parse a csv file that I have been provided with into magento. It succeeds in passing the products into magento, but they appear as "out of stock" on the site, even though they are marked as "in stock" and "enabled" in the admin area.

After convincing myself it is not the settings in the inventory tab for the imported products I've been looking through the magento code, and have identified that "is_salable" is both defined and returning null for every product. Further investigation concluded that the cataloginventory_stock_status table is empty.

I've been googling this issue to determine why the stock tables are not being set within the database despite the import script does not seem to be turning up any errors, but can't find an answer.

Can anybody see what I'm missing here, thanks in advance for any help or advice anybody can offer. Here is the main section of the code responsible for importing products:

$product = Mage::getModel('catalog/product');

$product
    ->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()))
    ->setAttributeSetId(4)
    ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
    ->setCreatedAt(strtotime('now'))
    ->setUpdatedAt(strtotime('now'))
    ->setSku($data[2])
    ->setName($data[5])
    ->setWeight(4.0000)
    ->setStatus(1)
    ->setTaxClassId(4)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setPrice($data[21])
    ->setCost($data[21])
    ->setSpecialPrice($data[21])
    ->setSpecialFromDate('06/1/2014')
    ->setSpecialToDate('06/30/2014')
    ->setMsrpEnabled(1)
    ->setMsrpDisplayActualPriceType(1)
    ->setMsrp($data[21])
    ->setMetaTitle('test meta title 2')
    ->setMetaKeyword('test meta keyword 2')
    ->setMetaDescription('test meta description 2')
    ->setDescription($data[7])
    ->setShortDescription($data[6])
    ->setMediaGallery (array('images'=>array(), 'values'=>array()))
    ->setStockData(array(
        'use_config_manage_stock' => 1,
        'qty' => 100,
        'min_qty' => 0,
        'use_config_min_qty'=>1,
        'min_sale_qty' => 0,
        'use_config_min_sale_qty'=>1,
        'max_sale_qty' => 9999,
        'use_config_max_sale_qty'=>1,
        'is_qty_decimal' => 0,
        'backorders' => 0,
        'notify_stock_qty' => 1,
        'is_in_stock' => 1
    ))
    ->setCategoryIds(array(3))
    ->save();
  • 写回答

1条回答 默认 最新

  • 普通网友 2014-08-14 14:43
    关注

    You need to call the stock item instead of setting in the product. I use something like that to set the stock data :

        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
        $stockItemId = $stockItem->getId();
        $stock = array();
    
        if (!$stockItemId) {
            $stockItem->setData('product_id', $product->getId());
            $stockItem->setData('stock_id', 1);
        } else {
            $stock = $stockItem->getData();
        }
        $StockData['qty'] = 12;
        $StockData['is_in_stock'] = 1;
        $StockData['manage_stock'] = 1;
        $StockData['use_config_manage_stock'] = 0;
        $StockData['use_config_min_qty'] = 0;
    
        foreach($StockData as $field => $value) {
            $stockItem->setData($field, $value?$value:0);
            Mage::log($field.'=>'.$value);
        }
        $stockItem->save();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行