Magento:在数量变化时自动将“库存可用性"从“缺货"更改为“库存"(反之亦然) [英] Magento: Auto-changing the “Stock Availability” from “Out of Stock” to “In Stock” (& vice-versa) on Quantity Change

查看:12
本文介绍了Magento:在数量变化时自动将“库存可用性"从“缺货"更改为“库存"(反之亦然)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在寻找一种方法,当数量字段大于 0 时,将库存可用性更改回有库存.当您将数量设置为 0 时,系统已经自动将库存可用性更改为缺货,并且保存产品.当您将数量设置为大于 0 并保存产品时,我想要一种将其设置回 In Stock 的方法.

So I’ve been looking for a way to change the Stock Availability back to In Stock when the quantity field is greater than 0. The system already automatically changes the Stock Availability to Out of Stock when you set the quantity to 0 and save the product. I would like a way to set it back to In Stock when you set the quantity greater than 0 and save the product.

好吧,我想我找到了一个简单的方法,这本身就让我很紧张.所以我想向各位大师发帖,看看这是否安全、正确和可行.

Well, I think I found a simple way, which in itself makes me nervous. So I wanted to post to you gurus to see if this is safe, correct, and ok to do.

app/design/adminhtml/default/default/template/catalog/product/tab/inventory.phtml

我已经改变了这个:

<?php foreach ($this->getStockOption() as $option): ?>
        <?php $_selected = ($option['value'] == $this->getFieldValue('is_in_stock')) ? 'selected="selected"' : '' ?>
        <option value="<?php echo $option['value'] ?>" <?php echo $_selected ?>><?php echo $option['label'] ?></option>
<?php endforeach; ?>

为此:

<?php if( ($this->getFieldValue('qty')*1) > 0): ?>
        <option selected="selected" value="1">In Stock</option>
<?php else: ?>
    <option selected="selected" value="0">Out of Stock</option>
<?php endif; ?>

此时我要做的只是一个实时站点,所以你可以理解我的担忧......

All I have to work on at this point is a live site, so you can understand my concern…

请告诉我这是否会产生预期的效果(看起来如此,但似乎很简单......)

Please let me know if this will have the intended effect (it appears so but it seems to simplistic....)

推荐答案

我相信你可以使用 Magento 事件 catalog_product_save_after.创建一个观察者方法,对事件 catalog_product_save_after 执行以下操作.

I believe you can use Magento event catalog_product_save_after. Create an observer method that does the following on event catalog_product_save_after.

public function catalog_product_save_after($observer) {
    $product = $observer->getProduct();
    $stockData = $product->getStockData();

    if ( $product && $stockData['qty'] ) {
        $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getEntityId()); // Load the stock for this product
        $stock->setData('is_in_stock', 1); // Set the Product to InStock                               
        $stock->save(); // Save
    }
}

这篇关于Magento:在数量变化时自动将“库存可用性"从“缺货"更改为“库存"(反之亦然)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆