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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?