如何解决Magento-使用sql更新所有产品库存
| 我想更新所有My Magento产品的库存。 我可以只用sql请求吗? 如果可能的话,有什么要求? 非常感谢解决方法
在产品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();
,像这样吗
UPDATE cataloginventory_stock_item SET qty=\'<my_quantity>\';
该表中的其他有用字段可能是:
数量
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
,您可以使用sql更新库存,但是由于Magento的EAV模型,sql可能有点麻烦-而且您确实需要问自己为什么要这样做。在我们的案例中,我们有一家实体店,因此Magento并不是我们的主要库存存放位置。
我们首先从商店POS数据库中提取UPC,成本,价格和数量,然后将数据转换为sql insert语句,以将数据插入到Magento db中的临时表中:
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数据库中添加了一个\'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
,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
);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。