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

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

问题描述

因此,我一直在寻找一种方法,当数量字段大于0时将库存可用性"更改为库存".当您将数量设置为0时,系统已经自动将库存可用性"更改为无库存".保存产品.当您将数量设置为大于0并保存产品时,我想将其重新设置为库存".

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天全站免登陆