Page 1 of 1

Creating categories during import

Posted: 25 Feb 2022, 18:04
by machack
What would be the easiest way to create product categories on import (writing my own import routine...)
I have tested Export/Import and categories are not recreated if deleted before the import.
I have not found a function in the API for creating categories.
By the way: Great product anyway
All the best
Martin

Re: Creating categories during import

Posted: 25 Feb 2022, 21:35
by machack
Answering my own question again...
this is my code for setting or adding new productcategories
$productarticle is Joomla content containing data (title, alias, images) for the new product to be added
$data is stored with storeProduct in a new product.
Hope that helps others who try to import data to phocacart.

Code: Select all

   function handleproductcategory($productarticle,&$data) { //Kategorie nachschlagen und checken ob schon in Produkten existent
        $filter_catid = $productarticle->catid;
        $categoryTable = JTable::getInstance('Category', 'JTable');
        $categoryTable->load($filter_catid);
        if ($categoryTable->id  == $filter_catid) { //Kategorie existiert schon
            $db = JFactory::getDBO();
            $query  = "SELECT id,alias FROM #__phocacart_categories ";
            $query .= "WHERE ".$db->quoteName('title')." LIKE '".$categoryTable->title."'";
            $db->setQuery($query);
            $prodcats = $db->loadObjectlist();
            if (!empty($prodcats)) {
                $o=1;
                foreach ($prodcats as $kC=>$vC) {
                    $idC = (int)PhocacartUtils::getIntFromString($vC->id);
                    $data['catid_multiple'][] 				= $idC;
                    $data['catid_multiple_ordering'][$idC]	= $o;
                    $o=$o+1;
                }
            } else { //neue Kategorie anlegen
                $prodcatTable = JTable::getInstance('PhocaCartCategory', 'Table', array());
                $cdata=array();
                $cdata['parent_id']=1; //ungelöst... müsste als Parameter kommen
                $cdata['title'] = $categoryTable->title;
                $cdata['alias'] = $categoryTable->alias;
                $cdata['published'] = 1;
                $cdata['access']= 1;
                try {
                    $prodcatTable->bind($cdata);
                }
                catch (Exception $e) {
                    JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
                    return false;
                }
                if (intval($prodcatTable->date) == 0) {
                    $prodcatTable->date = JFactory::getDate()->toSql();
                }
                try {
                    $prodcatTable->check();
                }
                catch (Exception $e) {
                    JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
                    return false;
                }
                try {
                    $prodcatTable->store();                }
                catch (Exception $e) {
                    JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
                    return false;
                }
            }
        }
    }
    

Re: Creating categories during import

Posted: 26 Feb 2022, 18:26
by Jan
Hi, thank you very much for the guide. Yes, the standard CSV/XML import/export is only about products - for more complicated export/imports it is better to use standard SQL or such type of script you wrote. Thank you.

Jan