I am trying to follow the answer of this question Avoiding memory exhaustion when updating product stock? to rewrite a resource model. I created a custom module and is activated in the back-office. Here is my config.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Mymodule_Vendor>
<version>0.1.0</version>
</Mymodule_Vendor_Vendor>
</modules>
<models>
<vendor>
<class>Mymodule_Vendor_Model</class>
<resourceModel>vendor_cataloginventory_resource</resourceModel>
</vendor>
<cataloginventory_resource>
<rewrite>
<stock_item>Mymodule_Vendor_Model_CatalogInventory_Resource_Stock_Item</stock_item>
</rewrite>
</cataloginventory_resource>
</models>
</config>
When i do this :
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->setData('is_in_stock', $quantity > 0 ? 1 : 0)->setQty($quantity);
$stockItem->getResource()->saveAttribute($stockItem,array('is_in_stock','qty'));
Directories structure :
app/code/local/Mymodule/Vendor/:
-Model/CatalogInventory/Resource/Stock : item.php
-etc : config.xml
I get this error :
Fatal error: Call to undefined method Mage_CatalogInventory_Model_Resource_Stock_Item::saveAttribute()
I solved the error but i got another one:
Fatal error: Call to a member function loadByProduct() on a non-object
when i try this :
$quantity = 8;
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$productId = $product->getId();
$product = Mage::getModel('catalog/product')->load($productId);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->setData('is_in_stock', $quantity > 0 ? 1 : 0)->setQty($quantity);
$stockItem->getResource()->saveAttribute($stockItem,array('is_in_stock','qty');
I didn't one to create another question so i edited this one.