In our last Post in the Zoo series, we looked at deleting records programmatically. Copying zoo item is not as easy as deleting records. You should have all the details including the zoo category of the zoo item which you want to copy. First, you should have all data of a zoo record. We have used the item API which gives zoo item's data & getRelatedCategoryIds which tells the relation of a zoo item to the categories.

Let's get the zoo records data first

$user  = JFactory::getUser(); // Get the current user info.
$app  = App::getInstance('zoo'); // Define Zoo Instance.
$now   = $app->date->create()->toMySQL(); // Get Current date
$item  = $app->table->item->get($zoo_id); // Get Zoo Item info
$categories = $item->getRelatedCategoryIds(); // Get the Zoo category info

Now We have all the information of Zoo Item that we wanna copy.

$item->id = 0; // set id to 0, to force new item
$item->state = 0; // Set the state of Zoo Item. 1 - publish 0 - Unpublish 
$item->alias = $app->alias->item->getUniqueAlias($zoo_id, 'copy-'.$item->alias); 
// Alias should be unique for each Zoo Item.
$item->name  .= ' ('.JText::_('Copy').')'; // set copied name
$item->created   = $item->modified = $now; // Created & Modified date
$item->created_by  = $item->modified_by = $user->id;
$item->hits   = 0;
$item->setTags($app->table->tag->getItemTags($zoo_id)); // copy tags
 
$app->table->item->save($item); // Save $item in zoo_item table
 
$app->category->saveCategoryItemRelations($item->id, $categories);
// Save the category relation in zoo_category table.