Create Product Attribute
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!
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
- marlonsamot
- Posts: 9
- Joined: 13 Apr 2021, 12:26
Create Product Attribute
Hi devs, please help me with this.
I want to create a attribute of a product in controller. Still, i don't get it how it works
I want to create a attribute of a product in controller. Still, i don't get it how it works
Re: Create Product Attribute
Use the product and attribute manager:
This creates a new attribute item and attaches it to a new product item.
Code: Select all
$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
$manager = \Aimeos\MShop::create( $context, 'product' );
$attrItem = $attrManager->create()->setCode( 'value' )->setType( 'test' );
$prodItem = $manager->create()->setCode( 'test' );
$listItem = $manager->createListItem();
$prodItem->addListItem( 'attribute', $listItem, $attrItem );
$manager->save( $prodItem );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
- marlonsamot
- Posts: 9
- Joined: 13 Apr 2021, 12:26
Re: Create Product Attribute
Which part of the code to identify on what product do i add a attribute
for example,
$product_id = 15;
this key of product id i need to add a attribute
for example,
$product_id = 15;
this key of product id i need to add a attribute
Re: Create Product Attribute
Use e.g.
Code: Select all
$manager = \Aimeos\MShop::create( $context, 'product' );
$manager->get( $product_id, ['attribute', 'media', 'price', 'text'] );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
- marlonsamot
- Posts: 9
- Joined: 13 Apr 2021, 12:26
Re: Create Product Attribute
I use this code
But it appears
https://prnt.sc/12x5zye
Code: Select all
$manager = \Aimeos\MShop::create( $context, 'product' );
$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
$manager->get( $product_id, ['attribute', 'media', 'price', 'text'] );
$attrItem = $attrManager->create()->setCode( 'value' )->setType( 'test' );
$prodItem = $manager->create()->setCode( 'test' );
$listItem = $manager->createListItem();
$prodItem->addListItem( 'attribute', $listItem, $attrItem );
$manager->save( $prodItem );
https://prnt.sc/12x5zye
Re: Create Product Attribute
You have to use the product item you are fetching and not create another one afterwards. Use this code instead:
Note: This code will fail when running twice because the attribute and listitem will be created again and you will receive a duplicate error from the database.
Code: Select all
$manager = \Aimeos\MShop::create( $context, 'product' );
$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
$prodItem = $manager->get( $product_id, ['attribute', 'media', 'price', 'text'] );
$attrItem = $attrManager->create()->setCode( 'value' )->setType( 'test' );
$listItem = $manager->createListItem();
$prodItem->addListItem( 'attribute', $listItem, $attrItem );
$manager->save( $prodItem );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
- marlonsamot
- Posts: 9
- Joined: 13 Apr 2021, 12:26
Re: Create Product Attribute
Thank you for this, it works .
Last question how about to validate if attribute exist ? just add it on a product only.
Last question how about to validate if attribute exist ? just add it on a product only.
Re: Create Product Attribute
Use something like this:
Code: Select all
$attrMap = [];
foreach( $prodItem->getListItems( 'attribute', null, null, false ) as $listItem ) {
if( $refItem = $listItem->getRefItem() ) {
$attrMap[$refItem->getType()][$refItem->getCode()] = $listItem;
}
}
if( !isset( $attrMap['test']['value'] ) ) {
$attrItem = $attrManager->create()->setCode( 'value' )->setType( 'test' );
$listItem = $manager->createListItem();
$prodItem->addListItem( 'attribute', $listItem, $attrItem );
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star