Soumi Basu
Soumi Basu

Reputation: 127

Getting list of categories in Akeneo

The following code has been given in Akeneo documentation: Use REST API . On execution of the code, it gives result like

RESULT:{"resource":"http:\/\/akeneo-pim.local\/api\/rest\/products\/OROMUG_DBO","family":"mugs","groups":OMUG_OB","OROMUG_ODB"]}}.....

I want to get the categories present in Akeneo in similar way. The code above uses the ProductController from WebserviceBundle. How should I proceed in order to get the categories in a similar way.

Upvotes: 4

Views: 1505

Answers (2)

Here is some sample code for a controller displaying a form allowing to choose among all existing categories from the 'print' channel.

public function indexAction()
{

    $channels = $this->channelRepository->getFullChannels(); 
    $selected_channel = null;

    /*
    * default channels are: 'print', 'mobile' 'ecommerce'
    */
    foreach($channels as $channel) {
        if('print' == $channel->getCode() ) {
            $selected_channel = $channel;
            break;
        }
    }
    $categories = [];

    /*
    * fill-in the array with the values we're interested in
    */
    if($selected_channel) {
        $category = $selected_channel->getCategory();
        $categories_ids = array_merge([$category->getId()], $this->categoryRepository->getAllChildrenIds($category));

        foreach($categories_ids as $category_id) {
            $category = $this->categoryRepository->find($category_id);
            $categories[] = array('id' =>$category->getId(), 'label' => $category->getLabel());
        }
    }

    return $this->templating->renderResponse('CfXmlBundle:Form:index.html.twig', array('categories' => $categories, 'locale' => 'en_US', 'scope' => null));
}

And the related Twig template:

<form>
    <div style="clear: both; width: 100%;">
        <label>Choose a catalog:</label>
        <select name="category_id" style="width: 100%;">
        {% for category in categories %}
        <option value="{{ category.id }}">{{ category.label }}</option>
        {% endfor %}
        </select>
    </div>
    <div style="clear: both; width: 100%">
        <label>Catalog title</label>
        <input type="text" name="title" value="" style="width: 100%;" placeholder="default is choosen catalog name" />
    </div>
    <div style="clear: both; width: 100%;">
        <label>Catalog description</label>
        <textarea name="description" style="width: 100%;"></textarea>
    </div>
    <div style="clear: both;">
        <input style="float: left;" type="checkbox" name="prices" value="0" />
        <label style="float: left;">&nbsp;Show prices ?</label>
    </div>
    <div style="clear: both; text-align:right;">
        <input type="submit" value="Generate" />
    </div>    
</form>

Upvotes: 0

Julien Sanchez
Julien Sanchez

Reputation: 841

Indeed Akeneo PIM only provide a product REST controller for external purposes for now.

Your only solution is to create your own category controller to extract category data from the PIM.

The product controller is a good template to start

You can also take a look at our internal API category controller to see how to properly normalize categories

Upvotes: 3

Related Questions