LemonStand Forum: Use API Shop_Product::create() to assign category!? - LemonStand Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Use API Shop_Product::create() to assign category!? Problems creating a product with API and assigning the category,.

#1 User is offline   manic 

  • Member
  • Group: Members
  • Posts: 27
  • Joined: 22-October 11

Posted 29 January 2012 - 09:28 PM

Hi there,

Ok I have some code that loops data in a CSV and attempts to create or update a product in lemonstand.

It works great apart from the Category assignment which is not sticking. I'm trying to put all new products into a 'hidden from site' category so that the shop administrator can later decide which category they want to place them on the site.

Heres the simplified code:

<?php

 		//For Access to Lemonstand API
		$Phpr_InitOnly = true; 
		include '../../index.php'; 
		
                //The Holding Category for Newly added PRoducts
		$default_category = 
		Shop_Category::create()->find_by_code('product-manage');

		
	//process csv
	if (($handle = fopen($csv, "r")) !== FALSE) {
		
		
		while (($data = fgetcsv($handle, 1000, ',','"')) 
		       !== FALSE) {
		 
		  if($row > 0){
			
			//attempt load product
			$product = Shop_Product::create()->find_by_sku($data[0]);
					
					  
			  if($product){
				/* UPDATE A FEW THINGS */
				$productdata = array('name'=>$data[1],'price'=>$data[3]);	  
									 
				
			  } else {
				/* CREATE PRODUCT 
				   AND PLACE IT IN A DEFAULT HOLDING CATEGORY*/
				
				$product = Shop_Product::create(); 
				
				$productdata = array('sku'=>$data[0],
									 'name'=>$data[1], 
									 'short_description'=>$data[2],
									 'price'=>$data[3],
									 'weight' => $data[5],
									 'url_name' => $data[0],
									 'category_list' => $default_category); 
			  }
			  
			  
			//enable product on site
			$productdata['enabled'] = 1;
			 
					  
			//save data
			$product->save($productdata);
		  }
		}
	fclose($handle);
	echo '[SUCCESS]';
	exit;
	}


The documentation says that Shop_Product->category_list should be an object of Db_DataCollection with each element in the collection an object of Shop_Category class.

Ok so thats probably where I am going wrong. But how do I put
Shop_Category::create()->find_by_code('product-manage');

into a Db_DataCollection object?

If that is infact the problem here.

Help much appreciated as always
0

#2 User is online   Aleksey 

  • Co-Founder
  • Group: +Administrators
  • Posts: 3,633
  • Joined: 31-October 09

Posted 31 January 2012 - 03:35 PM

Hi,

Please try this:

$product->categories->add(Shop_Category::create()->find_by_code('product-manage'));
$product->save();


Alternatively you can add references to the shop_products_categories table with direct SQL queries. Direct queries are preferable if you need to process multiple records at a time. Documentation: http://lemonstandapp...h_the_database/

Thank you

#3 User is offline   manic 

  • Member
  • Group: Members
  • Posts: 27
  • Joined: 22-October 11

Posted 17 February 2012 - 02:45 PM

that sorted it... thanks
0

Share this topic:


Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users