api add product

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
yesusagayaraj
Posts: 1
Joined: 03 Aug 2024, 10:55

api add product

Post by yesusagayaraj » 03 Aug 2024, 11:02

Hi Team
I have create product details through custom API but my details not added to the mshop_product_list table. Any changes needed

My code below
$manager = MShop::create( app( 'aimeos.context' )->get(), 'product' );

// Create new product item
$product = $manager->create()->setCode($request->code)->setLabel($request->name);
// Save the product
$manager->save( $product );

// Set product price
$priceManager = MShop::create(app('aimeos.context')->get(), 'price');
$price = $priceManager->create()
->setValue($request->price)
->setCurrencyId($request->currency_id)
->setType('default') // Set the type of the price
->setDomain('price'); // Set the domain for the price item
$price->getRefItems()->add(['product' => [$product->getId()]]);
$priceManager->save($price);

// Set product stock
$stockManager = MShop::create( app( 'aimeos.context' )->get(), 'stock' );
$stock = $stockManager->create()->setStockLevel($request->stock)->setProductId($product->getId());
$stockManager->save( $stock );

// Set product media
if($request->media_file) {
$mediaManager = MShop::create( app( 'aimeos.context' )->get(), 'media' );
$media = $mediaManager->create()->setFileName($request->media_file)->setMimeType('image/jpeg');
$media->getRefItems()->add( ['product' => [$product->getId()]] );
$mediaManager->save( $media );
}


return response()->json(['status' => 'Product created successfully']);
Last edited by yesusagayaraj on 03 Aug 2024, 11:04, edited 2 times in total.

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

Re: api add product

Post by aimeos » 08 Aug 2024, 08:12

You have to connect the price/media/text items to the product item via a list item like described here:
https://aimeos.org/docs/latest/models/m ... ated-items

Currently, you try to connect the product item to the price/media/text item which doesn't work (it must be the other way round).
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

yesusagayaraj
Posts: 1
Joined: 03 Aug 2024, 10:55

Add product details using API

Post by yesusagayaraj » 09 Aug 2024, 06:49

Hi Team,
I want to using create product through mobile app. Please help how will create product details using like name, price , stock , media API
Last edited by yesusagayaraj on 09 Aug 2024, 07:42, edited 1 time in total.

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

Re: api add product

Post by aimeos » 09 Aug 2024, 11:34

Example:

Code: Select all

$context = app( 'aimeos.context' )->get();
$manager = MShop::create( $context, 'product' );

// Create new product item
$product = $manager->create()->setCode($request->code)->setLabel($request->name);

// Create price
$priceManager = MShop::create($context, 'price');
$price = $priceManager->create()->setValue($request->price)->setCurrencyId($request->currency_id);

// Add price to product
$product->addListItem( 'price', $manager->createListItem(), $price );

// Save the product
$manager->save( $product );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply