I want to add images to the product
Posted: 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()] );
}
}