Creating products programmatically / miscellaneous questions

  • Posts: 9
  • Thank you received: 1
5 months 1 day ago #362023

-- 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?

Last edit: 5 months 1 day ago by DomB22.

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

  • Posts: 82967
  • Thank you received: 13393
  • MODERATOR
5 months 11 hours ago #362029

Hi,

1. Yes. You can use such code to generate the alias from the product name automatically:

// 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:
$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);
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.

The following user(s) said Thank You: DomB22

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

  • Posts: 9
  • Thank you received: 1
4 months 3 weeks ago #362171

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.056 seconds
Powered by Kunena Forum