News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Add multiple categories simply with php?

Started by panoss, August 21, 2017, 23:00:42 PM

Previous topic - Next topic

panoss

I 'm trying to find a way to add categories in Virtuemart in a simple way using php.
Can I use the controller 'Category' (VirtuemartControllerCategory) in another component?
In order to use it's save function?
Something like the following pseudo-code:

import VirtuemartControllerCategory as Controller;
$cats=[];
$cats[]='cat1';
$cats[]='cat2';
Controller->save($cats)
Virtuemart 3.2.4 on Joomla! 3.8.0

panoss

I ended up using the model and not the controller (I suppose this is the correct way?):


           // Set the extension
            $extension = 'com_virtuemart';

            // Set the title for the category
            $title     = 'My first VM Category';

            // Type the description, this is also the 'body'. HTML allowed here.
            $desc      = 'A category for Viruemart created with php code';

            // Set the parent category. 1 is the root item.
            $parent_id = 1;

            if (version_compare(JVERSION, '3.0', 'lt'))
            {
               JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
            }           

            if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR.'/'.'components'.'/'.'com_virtuemart'.'/'.'helpers'.'/'.'config.php');
            $config = VmConfig::loadConfig();
           
            // Initialize a new category
            $category = VmModel::getModel('Category');

            $category->extension = $extension;
            $category->title = $title;
            $category->description = $desc;
            $category->published = 1;
            $category->access = 1;
            $category->params = '{"target":"","image":""}';
            $category->metadata = '{"page_title":"","author":"","robots":""}';
            $category->language = '*';


But how do I store a new category? I tried:

            // Now store the category
            $data = [];
            if (!$category->store($data))
            {
               JError::raiseNotice(500, $category->getError());
               return false;
            }

But I get a token error:
"Message
Invalid Token"

What am I doing wrong?
Virtuemart 3.2.4 on Joomla! 3.8.0

Ghost

It means you're not logged in or your account does not have a permission to create categories.

panoss

But I am logged in and with this account I can create categories using the VM GUI.
Virtuemart 3.2.4 on Joomla! 3.8.0

panoss

I added:
echo JHtml::_('form.token');

before this code:

// Now store the category
$data = [];


but I still get the same error with the token.
Virtuemart 3.2.4 on Joomla! 3.8.0