- Posts: 9
- Thank you received: 1
Creating products programmatically / miscellaneous questions
2 years 2 months ago - 2 years 2 months ago #362023
by DomB22
-- HikaShop version -- : 5.1.0
-- Joomla version -- : 4.4.5
I'm writing a plugin which creates products. I've managed to add categories, various prices levels, custom fields and at this time I still have two questions :
- is it possible to generate automatically a product_alias upon creation?
- how is it possible to add multilingual versions (in my case en-GB and fr-FR) of the half-dozen translatable product fields? By the way, is it possible to make a 'text' custom field multilingual?
-- Joomla version -- : 4.4.5
I'm writing a plugin which creates products. I've managed to add categories, various prices levels, custom fields and at this time I still have two questions :
- is it possible to generate automatically a product_alias upon creation?
- how is it possible to add multilingual versions (in my case en-GB and fr-FR) of the half-dozen translatable product fields? By the way, is it possible to make a 'text' custom field multilingual?
Last edit: 2 years 2 months ago by DomB22.
Please Log in or Create an account to join the conversation.
2 years 2 months ago #362029
by nicolas
Replied by nicolas on topic Creating products programmatically / miscellaneous questions
Hi,
1. Yes. You can use such code to generate the alias from the product name automatically:
2. If you want to programmatically add translations to products, you can do like this:
This assumes that the id of the en-GB language in your Joomla is 1 and the id of the fr-FR language in your Joomla is 2.
You want to look at the lang_id column of the languages table of Joomla in the database in order to know these ids. The loadLanguage method of $translationHelper will return an array of objects from this table with the id and the code for each language.
3. If you want a custom product field to be multilingual, you need to edit the settings of the custom field in the menu Display>Custom fields and turn on the "Translatable" setting. The custom field will then be automatically added to the translation popup of each product.
1. Yes. You can use such code to generate the alias from the product name automatically:
Code:
// supposing you have your product data in $product
$productClass = hikashop_get('class.product');
$productClass->addAlias($product);
$product->product_alias = $product->alias;
unset($product->alias);
$productClass->save($product);
2. If you want to programmatically add translations to products, you can do like this:
Code:
$translations = array(
'product_name' => array(
1 => 'product name in english',
2 => 'nom du produit en français',
),
'product_description' => array(
1 => 'product description in english',
2 => 'description du produit en français',
),
);
$translationHelper = hikashop_get('helper.translation');
$translationHelper->handleTranslations('product', $product->product_id, $product, 'hikashop_', $translations);
You want to look at the lang_id column of the languages table of Joomla in the database in order to know these ids. The loadLanguage method of $translationHelper will return an array of objects from this table with the id and the code for each language.
3. If you want a custom product field to be multilingual, you need to edit the settings of the custom field in the menu Display>Custom fields and turn on the "Translatable" setting. The custom field will then be automatically added to the translation popup of each product.
The following user(s) said Thank You: DomB22
Please Log in or Create an account to join the conversation.
2 years 2 months ago #362171
by DomB22
Replied by DomB22 on topic Creating products programmatically / miscellaneous questions
Following your answers it works fine, thank you for the great support Nicolas.
The following user(s) said Thank You: Philip
Please Log in or Create an account to join the conversation.
Time to create page: 0.367 seconds