John Davidson

php - How to fetch Media Assets of a Product from Akeneo 5

0 comments
Message:


I try to fetch the media assets from products in


I am using https://github.com/akeneo/api-php-client-ee (v6) and know about https://api.akeneo.com/api-reference-50.html


This is what I came up so far:


$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder();
$searchBuilder->addFilter('enabled', '=', true);
$searchFilters = $searchBuilder->getFilters();

$products = $client->getProductApi()->all(100, ['search' => $searchFilters]);

foreach ($products as $product) {
foreach($product['values']['assets'] as $assetData) {
foreach($assetData['data'] as $code) {
echo $code;
$asset = $client->getProductMediaFileApi()->all();
var_dump($asset);
}
}

What I have tried / Questions:



  • I get a code like 1234_00 (if 1234 is the product number), but I do not know how to fetch the specific file from the product media file api. Do I have to filter here? How?



  • I tried to $client->getAssetMediaFileApi()->download($code) but the $code I have does not seem to be the full asset code (I get a 404 not found error)




How can I find out which assets are related to a specific product to download them or get the download URL?



My Answer:
To fetch media assets of a product from Akeneo 5 using the API PHP client, you can follow these steps:

1. Get the product data including the media assets:
php
$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder();
$searchBuilder->addFilter('enabled', '=', true);
$searchFilters = $searchBuilder->getFilters();

$products = $client->getProductApi()->all(100, ['search' => $searchFilters]);

foreach ($products as $product) {
$productId = $product['identifier'];
$mediaAssets = $product['values']['media_attribute_code']; // Replace 'media_attribute_code' with the actual attribute code for media assets

foreach ($mediaAssets as $mediaAsset) {
$assetCode = $mediaAsset['data']['_link']['code'];
$asset = $client->getAssetMediaFileApi()->download($assetCode);
// Handle the downloaded asset as needed
}
}


2. Replace 'media_attribute_code' with the actual attribute code for media assets in your product data. This will give you the media assets related to each product.

3. Use the asset code obtained from the product data to download the specific media asset using the `download` method of `AssetMediaFileApi`.

4. Handle the downloaded asset as needed in your application.

By following these steps, you should be able to fetch and download the media assets of a product from Akeneo 5 using the API PHP client.

Rate this post

3 of 5 based on 5499 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us