Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Adding categories and products with code #382

Closed
arimk opened this issue Dec 6, 2018 · 6 comments
Closed

Adding categories and products with code #382

arimk opened this issue Dec 6, 2018 · 6 comments

Comments

@arimk
Copy link

arimk commented Dec 6, 2018

Hi,
First of all thank you for this great plugin!

I have a question about mass import. I have (in excel files) categories (a few dozen) and products (3000) to import in 3 langages.
What could be the best solution to do it?
Basically I would like to do it programaticaly

For the categories, the idea is to first add the default langage, then every other langage.
Same for the products.

@arimk
Copy link
Author

arimk commented Dec 7, 2018

Ok if someone is wondering, here is how I am doing it:

Categories

  1. create X terms with wp_insert_term ();
  2. for each term pll_set_term_language( term_id, lang );
  3. pll_save_term_translations(
    array(
    "en" => en_id,
    "fr" => fr_id
    )
    );

Posts

  1. create X posts, and add to the right category depending on the langage

  2. pll_set_post_language( $post_id, "en");

  3. pll_save_post_translations( $arr );

@arimk arimk closed this as completed Dec 7, 2018
@frisonl
Copy link

frisonl commented Dec 13, 2018

Ok if someone is wondering, here is how I am doing it:

Categories

  1. create X terms with wp_insert_term ();
  2. for each term pll_set_term_language( term_id, lang );
  3. pll_save_term_translations(
    array(
    "en" => en_id,
    "fr" => fr_id
    )
    );

Posts

  1. create X posts, and add to the right category depending on the langage
  2. pll_set_post_language( $post_id, "en");
  3. pll_save_post_translations( $arr );

Hi @arimk , this is really handy, thanks! Just so I understand better:

  • you loop through a csv, create a term for each cat in a certain lang (except dafault lang) and then link those back to the id-s of the default lang which you retrieve in the same loop?
    Do you perhaps have a more elaborate example file that you made for this purpose that i can develop for my case?

Hope my question is clear and a big thanks to the developers of this plugin!

@arimk
Copy link
Author

arimk commented Dec 13, 2018

Yes so for the categories let's say you have a csv like that for each line
"name EN","name FR","name ES"

You do a wp_insert_term for each
Example :

$en_id =
wp_insert_term(
$cats[$1], // the term name
'product_cat', // the taxonomy
array(
'parent' => $saveparentEn // if parent
)
);

Be carefull for double slugs, so I prefixed slugs with 'slug' => "fr-".$cats[$k]["fr"] for example
With each you do a pll_set_term_language( term_id, lang );
And at the end you do a

pll_save_term_translations(
array(
"en" => en_id,
"fr" => fr_id,
"es" => es_id
)
);

And this will create it!

Follow the same guidelines for posts

If it's not clear enough, tel me and I will try to elaborate a little more

@frisonl
Copy link

frisonl commented Dec 14, 2018

got it to work with those hints, thanks @arimk !

@frisonl
Copy link

frisonl commented Dec 14, 2018

Do only thing not syncing is the hierarchy.. how did you approach this? @arimk

@Jon007
Copy link
Contributor

Jon007 commented May 24, 2019

@frisonl @arimk I didn't notice this thread before - you are welcome to submit pull request for importer and comment on issue #208
There is an onImport function in Meta.php which was added in July 2017 and is an optional feature in Settings, WooPoly

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants