1

I have product SKU.

using that i have to change product quantity As stock so how to do this.

I am getting a SKU and quantity like this

Array
(
    [minimalVS] => 1
    [seaglassin] => 0
    [half_moon_knuckle_ring] => 90
    [unique_Walnut_and_Cherry] => 8
    [half_eternity_ring_Sapphire_ring] => 11
    [ear_jacket] => 2
    [short_Thin_Line] => 44
    [totally_Awesome] => 1
    [handcrafted_Leather_Briefcase] => 5
    [militaryexpedition] => 5
)

Key as SKU and Value as Quantity means [sku] => quantity so how to change quantity

2
  • Where do you want to change the quantity? In the cart? As stock? via an import? Apr 16, 2015 at 10:30
  • As stock i want to change quantity
    – ND17
    Apr 16, 2015 at 10:31

1 Answer 1

1

The load method accepts the field to load on as a second argument. In this case that would be sku

$productId = Mage::getModel('catalog/product')->getIdBySku([the SKU], 'sku');
$product = Mage::getModel('catalog/product')->load($productId);

$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
// some default stock settings that you might need
$stockItem->setData('manage_stock', 1);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('use_config_notify_stock_qty', 0);

$stockItem->setData('qty', [the quantity]);

$stockItem->save();
8
  • Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (example.mg_cataloginventory_stock_item, CONSTRAINT FK_MG_CATINV_STOCK_ITEM_PRD_ID_MG_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY (product_id) REFERENCES mg_catalog_product_entity (entity_i), query was: INSERT INTO mg_cataloginventory_stock_item` (qty, is_in_stock, use_config_notify_stock_qty, manage_stock) VALUES (?, ?, ?, ?)";i:1;s:1923:"#0 F:\wamp\www\example\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Getting this error
    – ND17
    Apr 16, 2015 at 10:53
  • Please make sure the product is indeed loaded. Vardump for example the product objects ID to see if it is Apr 16, 2015 at 10:57
  • product is not loaded
    – ND17
    Apr 16, 2015 at 11:08
  • Is the SKU correct? Apr 16, 2015 at 11:28
  • its correct i am change this$product = Mage::getModel('catalog/product')->load([the SKU], 'sku'); use this $product = Mage::getModel('catalog/product')->getIdBySku([the SKU]); using this only 3 product is load please help me how to solve this
    – ND17
    Apr 16, 2015 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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