Magento - 无法刷新产品页面上的产品库存状态
我们的供应商之一拥有实时库存系统,我们希望将其实施到我们的网站中。当用户点击产品时,它应该检查库存并根据需要进行更新。这最多可以工作。问题是当产品切换到有货/缺货时。它在后端正确更新,但我无法添加/删除 addtocart 按钮。这是我用于更新库存的代码:
//$_stockQTY is the realtime inventory result
$stockData = Mage::getModel('cataloginventory/stock_item');
$stockData->loadByProduct($_product->getId());
$stockData->setData('qty', $_stockQTY);
$stockData->setData('is_in_stock',($_stockQTY > 0) ? 1 : 0);
if ($stockData->dataHasChangedFor('qty')) {
$stockData->save();
$_product = Mage::getModel('catalog/product')->load($_product->getId());
}
如您所见,当数量发生变化时,我会强制重新加载产品。这似乎适用于除“添加到购物车”按钮之外的所有内容。它显示了以前的结果(重新加载之前有库存或缺货。)
我有两个问题:
除了像我上面所做的那样重新分配产品之外,是否有更好的方法来重新加载产品:
$_product = Mage::getModel('catalog/product')->load($_product->getId());
以及为什么一切都正确更新,但是 addtocart 使用与我们的可用性等相同的
$_product->isSaleable()
调用。
比较:
<?php if($_product->isSaleable()): ?>
<p class="availability in-stock"><img src="<?php echo $this->getSkinUrl('images/stock.png') ?>"> <span><?php echo $this->__('In stock') ?></span>
...
?>
刷新
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php endif; ?>
<?php echo $this->getChildHtml('alert_urls') ?> //Only Shows up if addtocart does not.
页面将正确更新产品,但进行元刷新或任何类似的操作是不可能的。我很感谢您提供的任何建议,因为我希望解决此问题并继续执行下一个任务。
One of our Vendors has a real time inventory system and we would like to implement it into our site. When a person clicks on the product, it should check the inventory and update as necessary. This works ok at best. The problem is when the product switches to in/out of stock. It updates properly in the backend, but I am unable to get the addtocart button to be added/removed. This is my code for updating the stock:
//$_stockQTY is the realtime inventory result
$stockData = Mage::getModel('cataloginventory/stock_item');
$stockData->loadByProduct($_product->getId());
$stockData->setData('qty', $_stockQTY);
$stockData->setData('is_in_stock',($_stockQTY > 0) ? 1 : 0);
if ($stockData->dataHasChangedFor('qty')) {
$stockData->save();
$_product = Mage::getModel('catalog/product')->load($_product->getId());
}
As you can see, I am force reloading the product when qty is changed. This seems to work for everything but the addtocart button. It shows the previous result (In stock or out of stock before the reload.)
I have 2 questions:
Is there a better way to reload a product other than reassigning it as I am doing above:
$_product = Mage::getModel('catalog/product')->load($_product->getId());
And why is it that everything is updating properly, but the addtocart which uses the same
$_product->isSaleable()
call that our availability, etc uses.
Compare:
<?php if($_product->isSaleable()): ?>
<p class="availability in-stock"><img src="<?php echo $this->getSkinUrl('images/stock.png') ?>"> <span><?php echo $this->__('In stock') ?></span>
...
?>
To
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php endif; ?>
<?php echo $this->getChildHtml('alert_urls') ?> //Only Shows up if addtocart does not.
Refreshing the page will update the product properly, but doing a meta refresh or anything of the sorts is out of the question. I appreciate any advice that could be given as I would like to get this resolved and on to the next task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非我误解了你的问题,否则你的眼中钉似乎是股票状况指数。
试试这个:(
我还没有测试过,但看起来应该可行)
Unless I'm misunderstanding your question, it appears the thorn in your side is the stock status index.
Try this:
(I haven't tested this, but it looks like it ought to work)