0

I'm trying to do this for a while, but there's something isn't right about this, I've tried to put it in stock and enable it but nothing is working for me, can someone give me a hint or pinpoint me to the issue?

<?xml version="1.0"?>
<config>
  <modules>
    <Mr_StockEnhancements>
      <version>1.0.0</version>
    </Mr_StockEnhancements>
  </modules>
  <global>
    <models>
      <mr_stockenhancements>
        <class>Mr_StockEnhancements_Model</class>
      </mr_stockenhancements>
    </models>
    <events>
      <catalog_product_prepare_save>
        <observers>
          <Mr_StockEnhancements_Model_Observer>
            <type>singleton</type>
            <class>Mr_StockEnhancements_Model_Observer</class>
            <method>stockEnhancements</method>
          </Mr_StockEnhancements_Model_Observer>
        </observers>
      </catalog_product_prepare_save>
    </events>
  </global>

</config>

And here's the observer:

class Mr_StockEnhancements_Model_Observer {

public function stockEnhancements(Varien_Event_Observer $observer) {

        $product   = $observer->getProduct();
        $stockData = $product->getStockData();

        //make sure stock qty is greater than zero.
        if (isset($stockData['qty']) && $stockData['qty'] >= 1) {

            $product->setStockData['is_in_stock'] = 1; //update to instock
            Mage::getSingleton('catalog/product_action')
            ->updateAttributes(array($product->getId()), array('status' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED), $storeId);

            print_r($stockData['qty'], $product->StockData['is_in_stock']);
            exit;

        } else {
            Mage::getModel('catalog/product_status')->updateProductStatus($product->getProductId(), $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
        }
    }
 }
  • Is your observer working? – Vivek Kumar Aug 30 '18 at 19:15
  • Yes, it's working, but it isn't doing what it should be doing. – James Smith Aug 30 '18 at 21:14
  • Do you get desired data while printing? – Vivek Kumar Aug 30 '18 at 22:29
  • Yes, but it isn't updating as it should be, that's the only problem. – James Smith Aug 30 '18 at 23:52
  • Have you updated the event? – Vivek Kumar Aug 31 '18 at 0:17
0

You should use this observer on catalog_product_save_after event , replace your <catalog_product_prepare_save> tags in your xml to <catalog_product_save_after>

EDIT :

I think you've forgot to call $product->save() , so you should add it in the end of the code before print_r

EDIT 2

Also your stock update code doesn't seem right .. Use following code ;

$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('qty', 1);
$product->setStockItem($stockItem);
  • I tried this, but still, it isn't working, the observer is working, but it seems like the function isn't working properly! what could I be missing here? – James Smith Aug 30 '18 at 22:24
0

Try this code.

    $productData = $observer->getProduct();
    $product = Mage::getModel('catalog/product')->load($productData->getId());
    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
    $stockQty = $stock->getQty();
    if($stockQty>1){
    $stock->setManageStock(1)
          ->setUseConfigManageStock(1)
          ->setIsInStock(1);
     Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
     $stock->save();
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.