How to add product characteristics using Dev Code?

  • Posts: 175
  • Thank you received: 0
9 years 5 months ago #233914

-- HikaShop version -- : -
-- Joomla version -- : -
-- PHP version -- : -
-- Browser(s) name and version -- : -
-- Error-message(debug-mod must be tuned on) -- : -

Hello

Taking sample from this :
www.hikashop.com/support/support/documen...umentation.html#code

How do you add a product's characteristics [including a variant name and price for each variant] through that code/php?
Could you please provide a sample code to add it?

Thank You

Last edit: 9 years 5 months ago by veeco.

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

  • Posts: 84307
  • Thank you received: 13701
  • MODERATOR
9 years 5 months ago #233934

Hi,

That's not easy to do with the PHP as the code we have isn't meant to do that.
It will require quite a bit of code and queries to generate objects and array in a specific way to be able to call the save and updatecharacteristics functions of the product class.

The simplest will be to generate a CSV of the data of the product/variants and use the import system to generate the product out of it. Like that:

$csv = "product_code,product_parent_id,price_value,categories,color\r\n".
"main_product_code,,,category name,blue\r\n".
"variant1_code,main_product_code,10,,blue\r\n".
"variant2_code,main_product_code,12,,green\r\n";
$importHelper = hikashop_get('helper.import');
$importHelper->handleContent($csv);
This requires that you already have a characteristic called "color" with the values blue and green.

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

  • Posts: 175
  • Thank you received: 0
9 years 5 months ago #234007

Hi, Thank you for your reply

could give me an example [with real data] how to use the $csv method like you provided?
also, for example i have 2 characterstics [solution and weight]. How do you use 2 characteristics in your method?

like for example :
Product Name : paint
product code : p001
ID : 146
price = 1$

characteristics :
red & 1 kg = 1$
product code = p001r1
red & 5 kg = 5$
product code = p001r5
red & 25 kg = 25$
product code = p001r5

blue& 1 kg = 2$
product code = p001b1
blue& 5 kg = 10$
product code = p001b5
blue& 25 kg = 50$
product code = p001b25

I still dont get how to use the csv method and how to determine what characteristics the product have.
Cant we just use the characteristics's code rather than the value?

Thank You

Last edit: 9 years 5 months ago by veeco.

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

  • Posts: 84307
  • Thank you received: 13701
  • MODERATOR
9 years 5 months ago #234009

Well, it's like a normal HikaShop CSV. If you create such product on your backend and you click on the export button of the products listing, you'll get a CSV with the example you want.

$csv = "product_name,product_code,product_parent_id,price_value,categories,color,weight\r\n".
"paint,p001,,1,category name,red,1kg\r\n".
",p001r1,p001,1,,red,1kg\r\n".
",p001r5,p001,5,,red,5kg\r\n".
",p001r25,p001,25,,red,25kg\r\n".
",p001b1,p001,1,,blue,1kg\r\n".
",p001b5,p001,5,,blue,5kg\r\n".
",p001b25,p001,25,,blue,25kg\r\n";
$importHelper = hikashop_get('helper.import');
$importHelper->handleContent($csv);
And I don't understand your last questions. Characteristics don't have "codes". They have values, aliases and aliases for their values, no codes. If you're talking about the aliases, then yes, you can use the alias of the values instead of the values.

The following user(s) said Thank You: veeco

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

  • Posts: 175
  • Thank you received: 0
9 years 5 months ago #234121

Hello, Thank you

I have tried your code



It works in characteristics


but it doesnt add to variant


also, if i run the code again / update the data, one the characteristics is gone


any suggestion where i do it wrong? Thank You

Last edit: 9 years 5 months ago by veeco.

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

  • Posts: 175
  • Thank you received: 0
9 years 5 months ago #234127

Hello, this is an example of the csv generated by the code

Attachments:

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

  • Posts: 175
  • Thank you received: 0
9 years 5 months ago #234158

hi, Turns out there are some problem in my formatting and i finally figured it out to fix it

However, i want to ask
When i imported the csv, i set the solution field first then amount, however after imported the ordering is changed



i tried place it in reverse but it seems that it doesnt affect it, it's till amount first then solution.

I need it to be solution first then amount

any suggestion?

and

does product and product variant aliases stored somewhere other than hikashop_product?

i noticed that i cant use the same alias otherwise it's errors out or using the old format.
I also noticed that deleting the item through backend menu doesnt clean the item aliases

Thank You

Last edit: 9 years 5 months ago by veeco.

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

  • Posts: 84307
  • Thank you received: 13701
  • MODERATOR
9 years 5 months ago #234159

Hi,

The ordering depends on the ordering that you configured for the characteristics on the characteristics listing in your backend. The order of the columns in a CSV has not influence on what it does.

The characteristics data is stored in hikashop_characteristic. That way, you can have several products using the same characteristics and you don't have to create the characteristics each time.
So the alias of the characteristics and their values is also stored there and the characteristics data is not deleted when you delete a product since you can have other products using them.
The characteristics and the products/variants are linked together by the hikashop_variant table.
The variants data is stored like main products as rows of the hikashop_product table.
When you delete a product, the rows for the main product and its variants are deleted both in the hikashop_product and the hikashop_variant tables.

Last edit: 9 years 5 months ago by nicolas.
The following user(s) said Thank You: veeco

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

Time to create page: 0.069 seconds
Powered by Kunena Forum