Magento 取消订单后增加库存

<?php
$curr_date = date('Y-m-d H:i:s');
$order = $observer->getEvent()->getOrder();
 
foreach ($order->getItemsCollection() as $item) 
{ 
    $productId  = $item->getProductId();
    $qty = (int)$item->getQtyOrdered();
    $product = Mage::getModel('catalog/product')->load($productId);
    $stock_obj = Mage::getModel('cataloginventory/stock_item')
                   ->loadByProduct($productId);
    $stockData = $stock_obj->getData();
    $product_qty_before = (int)$stock_obj->getQty();
    $product_qty_after = (int)($product_qty_before + $qty); 
    $stockData['qty'] = $product_qty_after;
    /*
     * it may be case that admin has enabled product add in stock, 
     * after product sold, he set is_in_stock = 0 and
     * if order cancelled then we need to update only qty 
     * not is_in_stock status.
     * make a note of it
     */
    if($product_qty_after != 0) {
        $stockData['is_in_stock'] = 1;
    }else{
        $stockData['is_in_stock'] = 0;
    }
 
    $productInfoData = $product->getData();
    $productInfoData['updated_at'] = $curr_date;
    $product->setData($productInfoData);
    $product->setStockData($stockData);
    $product->save();
}

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论