Forum

Thread tagged as: Api

Categories in import API

Hi

I am importing items into a collection via the API.

Each item may or not have a category associated with it.

If that category string is null or empty then I get an exception error thrown: "Category not found:".

Is there any way to escape this exception so that items can have categories or not?

Jonathan Elliman

Jonathan Elliman 27 points

  • 2 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Jonathan,

Are you always setting a category regardless whether you actually have a category to add?

$item_data = [
  'title' => 'Some Title',
  'slug' => '',
  'category' => $category, // may be empty?
];

$Importer->add_item($item_data);

Or are you only adding a category when you actually have one to add?

$item_data = [
  'title' => 'Some Title',
  'slug' => '',
];

if($category) {
  $item_data['category'] = $category;
}

$Importer->add_item($item_data);

Hi Hussein.

Thanks so much for replying. This is my first app so I am taking baby steps at the moment.

I am following the first method but did think about the second one and my brain caught fire.

Your second way of working is exactly what my feeble mind was telling me to try out.

So basically I can create the import array separately, add to it dynamically with if statements and then pass that off to the add_item function? That way I guess I can add unlisted categories and check for images in the csv too.