Cosmo Phobia
Cosmo Phobia

Reputation: 41

Get Akeneo PIM product as standard format in own bundle

I have a custom symfony bundle used in Akeneo 1.6 (Upgraded from 1.3 some time ago), which I need to port to Akeneo 2.2.

What I need is get all product data by product ID in a controller action. This was done by $repository->getFullProduct($productId). This Method was removed in the current Akeneo version.

While researching I found that the "standard format" seemed helpful as it contains all product data as array.

How can I recieve this data? It doesn't have to be a clean solution, Quick&Dirty (Like the whole Bundle ;) ) is just fine. It's only for internal use.

I tried some stuff like $productStandard = $this->container->get('pim_api.normalizer.product')->normalize($product); whith different services, but based on the useless error messages I recieved I think it just doesn't make sense.

Upvotes: 0

Views: 292

Answers (1)

grena
grena

Reputation: 1020

When searching products in Akeneo PIM, you should use the Product Query Builder. You can read more about it on the official documentation on querying products. A quite similar question has been asked, you can see my answer here: Query products with Doctrine ind Akeneo.

To get the standard format of a product, you can normalize your Product instance with the normalizer.

So this would look like this:

<?php
// Get a new instance of the PQB
$pqbFactory = $this->getContainer()->get('pim_catalog.query.product_query_builder_factory');
$pqb = $pqbFactory->create([
    'default_locale' => 'en_US',
    'default_scope' => 'ecommerce'
]);

// Now you can search for products with your ids
$pqb->addFilter(
    'id',
    'IN',
    ['234', '22', '90']
);

// Retrieve your products
$productsCursor = $pqb->execute();
$normalizedProducts = [];
foreach ($productsCursor as $product) {
    // normalize them to the standard format
    $normalizedProducts[] = $this->getContainer()->get('pim_standard_format_serializer')->normalize($product, 'standard');
}

Upvotes: 0

Related Questions