1

I'm creating a new product like this:

$item = array(...);    // associative array has attribute values
$product = Mage::getModel('catalog/product');
$product->setTypeId('simple');
$product->setSku($item['sku']);
$product->setName($item['name']);
$product->setPrice($item['price']);
$product->setStatus($item['status']);
etc...
$this->updateStock(product->getId(), $item['quantity']);
$product->save();

....

public function updateStock($id, $qty)
{
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);

    $stockItem->setData('is_in_stock', 1);
    $stockItem->setData('manage_stock', 1);   // tried a couple of variants like 'inventory_manage_stock'
    $stockItem->setData('qty', $qty);
    $stockItem->save();
}

I'm having some problems:

  1. [SOLVED] Price doesn't get inserted the first time save() is called. If I run this code again (with $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $item['sku'])), price get set around the second time. (SOLVED: I didn't set the product type. Code updated)

  2. [SOLVED]Regarding stock.. I can't get the Use Config Settings checkboxes to be checked (by default it is when item is created in admin) in the Inventory tab in the admin when a new product is created.

Upon investigating the database, when I save the product initially, the corresponding row in cataloginventory_stock_item is not created. So, it looked like I needed to initialize it somehow before the initial product save. I have solves this issue, and I will update the answer later.

  1. [SOLVED]I tried to remedy problem 2 by setting the manage_stock to true, but I can't even set "Manage Stock" to "Yes". updateStock() works for products that are already existing.

How can I solve these issues?

2

Creating a new product will not generate product->id that you are passing in updateStock Method so that is why updateStock is working only for existing products.

I would suggest to check

$product = Mage::getModel('catalog/product');
    $product->load(18); //18 is my existing product id
    echo '<pre>';
    print_r($product->getData());
    exit;

This will give you idea about data associated to product.

Recently, I have created a method that is used to create product. Please review it.

public function createProduct($categories = array(3),$productName,$sku,$price=0,$productDescription,$productShortDescription,$status,$visibility,$attributeSet,$taxClass,$qty,$stock,$websites = array(1),$productImage='',$productMetaTitle,$productMetaKeyword,$productMetaDescription) {             

    $product = new Mage_Catalog_Model_Product();        
    // Build the product
    $prod = array(); 
    $prod['sku'] = $sku;
    $prod['attribute_set_id'] = $attributeSet;
    $prod['type_id'] = 'simple';
    $prod['category_ids'] = $categories;
    $prod['website_ids'] = $websites;
    $prod['name'] = $productName;
    $prod['description'] = $productDescription;
    $prod['short_description'] = $productShortDescription;
    $prod['weight'] = '1';
    $prod['status'] = $status;
    $prod['url_key'] = Mage::getModel('catalog/product_url')->formatUrlKey($productName);
    $prod['url_path'] = Mage::getModel('catalog/product_url')->formatUrlKey($productName); //TO-DO :: find correct method
    $prod['visibility'] = $visibility;
    $prod['price'] = $price;
    $prod['tax_class_id'] = $taxClass;
    $prod['meta_title'] = $productMetaTitle;
    $prod['meta_keyword'] = $productMetaKeyword;
    $prod['meta_description'] = $productMetaDescription;
    $product->setData($prod);
    $product->setStockData(array( 
        'is_in_stock' => $stock, 
        'qty' => $qty,
                'manage_stock' => 1
    ));
    if($productImage) {
        Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
        $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
        $product->addImageToMediaGallery($productImage,array('image','small_image','thumbnail'),false,false);
    }
    try {
        $product->save();           
        return $product->getId();
    }
    catch (Exception $e){
        echo $e->getMessage();
    }               
}

Hope above method helps you.

Also if you set manage stock from System->Configuration->Catalog->Inventory->Product Stock Options to Yes, then you don't need to pass manage_stock in above method. Product will be automatically created with manage stock set to yes.

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.