I want to add images to the product

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
WarisAli
Posts: 8
Joined: 08 Sep 2022, 05:54

I want to add images to the product

Post by WarisAli » 26 Sep 2022, 09:41

I'm using aimeos managers to add new product but not able to add image can you plz help me out.
public function productArray($key){
$product = [
"product" => [
"product.id" =>'',
"product.siteid" => "32.",
"product.ctime" => "2022-09-21 05:57:00",
"product.mtime" => "2022-09-22 11:39:49",
"product.editor" => "admin@test.com",
"score" => 2.0,
"product.url" => "test123",
"product.type" => "default",
"product.code" => "test123",
"product.label" => "test123",
"product.status" => 1,
"product.dataset" => "",
"product.datestart" => null,
"product.dateend" => null,
"product.config" => [],
"product.scale" => 1.0,
"product.target" => "",
"product.ratings" => 0,
"product.rating" => "0",
"product.instock" => 1,
],
"media" => [
[
"media.status" => "1",
"media.id" => null,
"media.type" => "default",
"media.label" => "testing",
"media.url" => "",
"media.languageid" => 'en',
"product.lists.type" => "default",
"product.lists.datestart" => null,
"product.lists.dateend" => null,
"media.domain" => "product",

]
]
];
}
public function context(){
$context = \App::make('\Aimeos\Shop\Base\Context')->get(true);
return $context;
}
public function listManager($key){
$listManager = \Aimeos\MShop::create( $this->context(),$key );
return $listManager;
}

$productManager = $this->listManager('product');
$productItem = $productManager->create($this->productArray('product'), true);
$getImageNameFromFile = "background.webp";
$localStoragePath = env('IMPORT_MAGENTO1_IMAGE_LOCAL_STORAGE');
$exists = storage_path($localStoragePath.$getImageNameFromFile);
if(file_exists($exists)){
$imageObject = getImageObject($localStoragePath, $getImageNameFromFile);
$this->createMedia($productItem,$imageObject);
}

public function createMedia($productItem,$mediaObject) {

$getImageObject['media'][0]['file'] = $mediaObject;
$mediaArray = $this->productArray('media');

$mediaManager = $this->listManager( 'media' );

$listManager = $this->listManager( 'product/lists' );
$cntl = \Aimeos\Controller\Common\Media\Factory::create( $this->context() );
$listItems = $productItem->getListItems( 'media', null, null, false );

$files = $getImageObject;
foreach( $mediaArray as $idx => $entry ) {
$id = $this->val( $entry, 'media.id', '' );
$type = $this->val( $entry, 'product.lists.type', 'default' );

$listItem = $productItem->getListItem( 'media', $type, $id, false ) ?: $listManager->create();
$refItem = $listItems->getRefItem() ?: $mediaManager->create();

$refItem->fromArray( $entry, true )->setDomain( 'product' );
$file = $this->val( $files, 'media/' . $idx . '/file' );
$preview = $this->val( $files, 'media/' . $idx . '/preview' );

if( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
$refItem = $cntl->copy( $refItem );
}

if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE ) {
//$refItem = $cntl->add( $productItem, $file );
if( $preview && $preview->getError() !== UPLOAD_ERR_NO_FILE ) {
$refItem = $cntl->addPreview( $refItem, $preview );
}
}

$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );

foreach( (array) $this->val( $entry, 'config', [] ) as $cfg ) {
if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
$listItem->setConfigValue( $key, trim( $cfg['val'] ?? '' ) );
}
}
$attrListItems = $productItem->getListItems( 'attribute', 'variant', null, false )->toArray();

$refItem = $this->addMediaAttributes( $refItem, $attrListItems );
$productItem->addListItem( 'media', $listItem, $refItem );

unset( $listItems[$listItem->getId()] );
}
}

User avatar
aimeos
Administrator
Posts: 6739
Joined: 01 Jan 1970, 00:00

Re: I want to add images to the product

Post by aimeos » 27 Sep 2022, 07:42

Here you can see how the admin backend adds/updates product images:
https://github.com/aimeos/ai-admin-jqad ... #L362-L401

The main lines are:

Code: Select all

$productManager = \Aimeos\MShop::create( $this->context(), 'product' );
$mediaManager = \Aimeos\MShop::create( $this->context(), 'media' );

$product = $productManager->get( '123' );
$listItem = $productManager->createListItem();
$mediaItem = $mediaManager->create();

$product->addListItem( 'media', $listItem, $mediaItem );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
WarisAli
Posts: 8
Joined: 08 Sep 2022, 05:54

Re: I want to add images to the product

Post by WarisAli » 27 Sep 2022, 12:44

I'm Getting this error when I'm trying to upload image. The Image is in storage/upload folder of my project
TypeError: Aimeos\Controller\Common\Media\Standard::add(): Argument #2 ($file) must be of type Psr\Http\Message\UploadedFileInterface, Symfony\Component\HttpFoundation\File\UploadedFile given, called in C:\xampp\htdocs\shopping\Aimeos-shop\app\Http\Controllers\Api\ProductImportController.php on line 332 in file C:\xampp\htdocs\shopping\Aimeos-shop\vendor\aimeos\aimeos-core\src\Controller\Common\Media\Standard.php on line 47
This how I'm creating the image object.
use Symfony\Component\HttpFoundation\File\UploadedFile;
$localStroagePath = ""/app/public/import_img/"";
$image = "background.webp";
$url = storage_path($localStoragePath . $image);
$path_parts = pathinfo($url);
$newPath = $path_parts['dirname'] . '/tmp-files/';
if (!is_dir($newPath))
{
mkdir($newPath, 0777);
}
$newUrl = $newPath . $path_parts['basename'];
copy($url, $newUrl);
$imgInfo = getimagesize($newUrl);
$file = new UploadedFile($newUrl, $path_parts['basename'], $imgInfo['mime'], filesize($url) , true, true);

User avatar
aimeos
Administrator
Posts: 6739
Joined: 01 Jan 1970, 00:00

Re: I want to add images to the product

Post by aimeos » 28 Sep 2022, 11:31

If your file is already at the file system, you don't have to upload it again. Instead, you can add the URL in the media item directly using setUrl():
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply