How to update product's data using API?

  • Posts: 175
  • Thank you received: 0
9 years 3 months ago #212099

-- url of the page with the problem -- : localhost
-- HikaShop version -- : 2.4.0
-- Joomla version -- : -
-- PHP version -- : -
-- Browser(s) name and version -- : -
-- Error-message(debug-mod must be tuned on) -- : -


//EO Price
$product = null;
$product->product_name = $name;
$product->product_quantity = -1;
$product->product_code = $code;
$product->product_published = $prod_pub;
$product->product_tax_id = 11;
$product->product_weight = 1;
$product->product_weight_unit = "kg";
$product->product_min_per_order = $min_order;
$product->product_type = "main";
$product->product_access = "all";
$product->product_manufacturer_id = $id_merek;
$product->grup = $grup;
$product->tipe = $tipe;
$product->unit = $unit;

$product->product_quantity = $stock;
$product->stockstatus = $stockstatus;


$product->prices = array($price);
$product->categories = array($child); //where 1 and 2 are the ids of two categories [currenlty: string - "24"]
//EO product
$productClass = hikashop::get('class.product');
$productClass->save($product);

//load product id
$query = $db->getQuery(true);
$query->select('product_id')
->from('#__hikashop_product')
->where('product_code = "'.$code.'"');
$db->setQuery($query);
$results = $db->loadObjectList();

$product_id = $results[0]->product_id;

$productClass->save($product);


hi, i used this code to insert product into hikashop, however it's restrictly only for insert [new product], if product exists then it skipped the product


is there anyway to make it update the product if it's exists? [name, quantity, description, etc]

i have tried $productClass->refreshQuantities(); but it doesnt do anything

Last edit: 9 years 3 months ago by veeco.

Please Log in or Create an account to join the conversation.

  • Posts: 12953
  • Thank you received: 1778
9 years 3 months ago #212110

Hello,
To do what you want you'll just have to check through the "product" hikashop table <==> hikashop_table('product') that the products actually exists, and if it does, then you'll just have to update your product quantity for example.

Please Log in or Create an account to join the conversation.

  • Posts: 175
  • Thank you received: 0
9 years 3 months ago #212293

Mohamed Thelji wrote: Hello,
To do what you want you'll just have to check through the "product" hikashop table <==> hikashop_table('product') that the products actually exists, and if it does, then you'll just have to update your product quantity for example.


hi

Is it through manual SQL query with jdbo? or it could be done using hikashop API like this? could you provide sample code?

the
//EO product
$productClass = hikashop::get('class.product');
$productClass->save($product);

part only save if it's a new product, i need it to save if not exists and update the product's data if it does exists

is there any method to make it update instead of save?
$productClass->update($product); or something?

Last edit: 9 years 3 months ago by veeco.

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
9 years 3 months ago #212318

The system already handle automatically update/insert based on whether the product_id is provided.
With the code from your first message, you just need to add such line:

$product->product_id = $product_id;
before the last save.

The following user(s) said Thank You: veeco

Please Log in or Create an account to join the conversation.

Time to create page: 0.079 seconds
Powered by Kunena Forum