1

Hi I'm trying to create products programmatically following the guide at http://www.vodoosolutions.com/importing-products-magento-programmatically-php.html. The products do show up in the categories but when I view the product page the 'Add to Cart' button is not showing up. Looking at the code, the product(s) are obviously not saleable. Any ideas on this? Below is the code I'm using when creating the products.

$product = Mage::getModel('catalog/product');
  /*** Product insertions ***/
  $part_row = array_map('trim', $part_row);
  $product->setSku($part_row['partnum']);
  $product->setName($part_row['shortdesc']);
  $product->setDescription($part_row['longdesc']);
  $product->setShortDescription($part_row['shortdesc']);
  $product->setManufacturer('');
  $product->setPrice($part_row['price']);
  $product->setTypeId('simple');
  $product->setCategoryIds(GetCategory($part_row['category'])); // need to look these up
  $product->setWeight(0);
  $product->setTaxClassId(2); // taxable goods
  $product->setVisibility(4); // catalog, search
  $product->setStatus(1);
  $product->setAttributeSetId(4); // need to look this up
  $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
  /*** Stock Data Array ***/ 
  $stockData = $product->getStockItem();      
  $stockQty = 10000;
  $stockItem = Mage::getModel('cataloginventory/stock_item');
  $stockItem->assignProduct($product);
  $stockItem['is_in_stock'] = 1;
  $stockItem['stock_id'] = 1;
  $stockItem['store_id'] = 1;
  $stockItem['manage_stock'] = 0;
  $stockItem['use_config_manage_stock'] = 0;
  $stockItem['min_sale_qty'] = 0;
  $stockItem['use_config_min_sale_qty'] = 0;
  $stockItem['max_sale_qty'] = 1000;
  $stockItem['use_config_max_sale_qty'] = 0;
  $stockItem['is_salable'] = 1;
  $stockItem['qty'] = $stockQty;
  $product->setStockData($stockData);
  $product->save();  

EDIT Thanks Marius for catching that, after importing products I always reindex/clear the cache. The frontend is still not rendering properly after changing the code to actually set the product stock. While the categories (list.phtml) view does show the 'Add to Cart ' button the product view (view.phtml) does not. The new code is posted below for more information on how I'm importing things.

/*** Stock Data Array ***/ 
  $stockData = $product->getStockItem();      
  $stockQty = 10000;
  $stockData = Mage::getModel('cataloginventory/stock_item');
  $stockData->assignProduct($product);
  $stockData['is_in_stock'] = 1;
  $stockData['stock_id'] = 1;
  $stockData['store_id'] = 1;
  $stockData['manage_stock'] = 0;
  $stockData['use_config_manage_stock'] = 1;
  $stockData['min_sale_qty'] = 0;
  $stockData['use_config_min_sale_qty'] = 0;
  $stockData['max_sale_qty'] = 1000;
  $stockData['use_config_max_sale_qty'] = 0;
  $stockData['is_saleable'] = 1;
  $stockData['qty'] = $stockQty;
  $product->setStockData($stockData);
  $product->save();   
  • What do you see in the admin? Did you tried reindexing and flushing cache? – mbalparda Feb 18 '15 at 16:45
  • 2
    What does the frontend look like? If you change to the default theme, do you see a add to cart button? Have you reindexed? We need more information. – kab8609 Feb 18 '15 at 18:43
1

I think this is your issue:

/*** Stock Data Array ***/ 
  $stockData = $product->getStockItem();      
  $stockQty = 10000;
  $stockItem = Mage::getModel('cataloginventory/stock_item');
  $stockItem->assignProduct($product);
  $stockItem['is_in_stock'] = 1;
  $stockItem['stock_id'] = 1;
  $stockItem['store_id'] = 1;
  $stockItem['manage_stock'] = 0;
  $stockItem['use_config_manage_stock'] = 0;
  $stockItem['min_sale_qty'] = 0;
  $stockItem['use_config_min_sale_qty'] = 0;
  $stockItem['max_sale_qty'] = 1000;
  $stockItem['use_config_max_sale_qty'] = 0;
  $stockItem['is_salable'] = 1;
  $stockItem['qty'] = $stockQty;
  $product->setStockData($stockData);

On the first line, you get the stock item and store it in the $stockData variable: $stockData = $product->getStockItem();

But then you instantiate a stock item model and you assign values to it: $stockItem=...

But you never use this $stockItem variable. You just set the $stockData (that is empty) back to the product. Try with

  $product->setStockData($stockItem);
1

Answered My own question. Here was the problem, I had setup the store to manage the stock not the actual product. It was conflicting with what the import script was trying to do. As soon as I cleared the store settings and re-imported everything was working as it should be. Thanks for all of the input!

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.