Magento - 使用sql更新所有产品库存

时间:2011-05-24 07:49:51

标签: magento

我想更新所有My Magento产品的库存。

我可以使用只有sql请求吗?

如果可能,请求是什么?

非常感谢

4 个答案:

答案 0 :(得分:7)

这样的东西?

UPDATE cataloginventory_stock_item SET qty='<my_quantity>';

此表中的其他有用字段可能是:

  • qty
  • min_qty
  • use_config_min_qty
  • is_qty_decimal
  • 延期交货
  • use_config_backorders
  • min_sale_qty
  • use_config_min_sale_qty
  • max_sale_qty
  • use_config_max_sale_qty
  • is_in_stock
  • low_stock_date
  • notify_stock_qty
  • use_config_notify_stock_qty
  • manage_stock
  • use_config_manage_stock
  • stock_status_changed_automatically
  • use_config_qty_increments
  • qty_increments
  • use_config_enable_qty_increments
  • enable_qty_increments

答案 1 :(得分:2)

在产品foreach循环中使用,您可以修改以下代码:

$newstocklevel = 100;

$product_id = Mage::getModel('catalog/product')->getIdBySku(321);
$product = Mage::getModel('catalog/product');
$product ->load($product_id);
$stockData = $product->getStockData();
$stockData['qty'] = $newstocklevel;
$stockData['is_in_stock'] = 1;

$product->setStockData($stockData);

$product->save();

答案 2 :(得分:2)

你可以使用sql更新你的库存,但由于Magento的EAV模型,sql可能有点麻烦 - 你真的需要问自己为什么要这样做。在我们的案例中,我们有一个实体店,因此Magento不是我们的主要库存存储位置。

我们首先从商店POS数据库中提取UPC,成本,价格和数量,然后将数据转换为sql insert语句,将数据插入Magento数据库的临时表中:

CREATE TABLE Temp_Inventory (
`UPC` varchar(40) NOT NULL,
`ItemName` varchar(60) NOT NULL,
`Cost` float NOT NULL,
`Price` float NOT NULL,
`In_Stock` float NOT NULL,
`Helper_ItemNum` varchar(40) DEFAULT NULL,
 UNIQUE KEY `Temp_Inventory_IN` (`UPC`),
 KEY `Temp_Inventory_H` (`Helper_ItemNum`));

delete from Temp_Inventory;
insert into Temp_Inventory (UPC, ItemName, Cost, Price, In_Stock) values ("132456789123", "Item Description", 9.6667, 14.9900, 14);

我们使用UPC作为主键,但Magento使用自己的密钥,因此我们在Magento DB中添加了一个'UPC'属性(编码'upc'),以便我们可以匹配系统之间的项目。首先,我们需要使用Magento项目ID填充临时表。

 update Temp_Inventory set Helper_ItemNum =
    (select catalog_product_entity.entity_id
    from catalog_product_entity, catalog_product_entity_varchar, eav_attribute
    where eav_attribute.attribute_code = "upc"
    and catalog_product_entity_varchar.entity_type_id = eav_attribute.entity_type_id
    and catalog_product_entity_varchar.attribute_id = eav_attribute.attribute_id
    and catalog_product_entity_varchar.entity_id = catalog_product_entity.entity_id
    and catalog_product_entity_varchar.value = UPC);

然后我们需要更新库存值,确保我们只更新具有以下值的项目:

update cataloginventory_stock_item set qty =
    (select In_Stock
    from Temp_Inventory
    where Helper_ItemNum = cataloginventory_stock_item.item_id)
 where cataloginventory_stock_item.item_id in (select Helper_ItemNum from Temp_Inventory);

使用表catalog_product_entity_decimal可以以类似的方式更新成本和价格。

然后我们更新库存状态库存与缺货:

update cataloginventory_stock_item set is_in_stock = 1
where cataloginventory_stock_item.item_id in (select Helper_ItemNum from Temp_Inventory)
and cataloginventory_stock_item.qty > 0;

update cataloginventory_stock_item set is_in_stock = 0
where cataloginventory_stock_item.item_id in (select Helper_ItemNum from Temp_Inventory)
and cataloginventory_stock_item.qty <= 0;

最后,我们需要重新索引库存状态,以便我们的网站正确显示:

php -f /path/to/magento/shell/indexer.php -- --reindex cataloginventory_stock

答案 3 :(得分:2)

function updateProductStock($productId, $qty) { 
    $resource = Mage::getSingleton('core/resource');
    $write = $resource->getConnection('core_write');
    $write->update(
      "cataloginventory_stock_item"
    , array("qty" => $qty, 'is_in_stock' => ($qty > 0 ? 1 : 0))
    , "product_id = " . $productId
    );
}