Creating categories during import

Phoca Cart - complex e-commerce extension
machack
Phoca Member
Phoca Member
Posts: 13
Joined: 27 Mar 2017, 12:59

Creating categories during import

Post 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

Tags:
machack
Phoca Member
Phoca Member
Posts: 13
Joined: 27 Mar 2017, 12:59

Re: Creating categories during import

Post 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;
                }
            }
        }
    }
    
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 47883
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Creating categories during import

Post 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
If you find Phoca extensions useful, please support the project
Post Reply