Create Product Attribute

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!
User avatar
marlonsamot
Posts: 9
Joined: 13 Apr 2021, 12:26

Create Product Attribute

Post by marlonsamot » 10 May 2021, 03:04

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

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

Re: Create Product Attribute

Post by aimeos » 12 May 2021, 06:47

Use the product and attribute manager:

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 );
This creates a new attribute item and attaches it to a new product item.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
marlonsamot
Posts: 9
Joined: 13 Apr 2021, 12:26

Re: Create Product Attribute

Post by marlonsamot » 13 May 2021, 13:54

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

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

Re: Create Product Attribute

Post by aimeos » 13 May 2021, 15:33

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, Image give us a star

User avatar
marlonsamot
Posts: 9
Joined: 13 Apr 2021, 12:26

Re: Create Product Attribute

Post by marlonsamot » 14 May 2021, 11:05

I use this code

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 );
But it appears

https://prnt.sc/12x5zye

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

Re: Create Product Attribute

Post by aimeos » 15 May 2021, 10:14

You have to use the product item you are fetching and not create another one afterwards. Use this code instead:

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 );
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.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
marlonsamot
Posts: 9
Joined: 13 Apr 2021, 12:26

Re: Create Product Attribute

Post by marlonsamot » 17 May 2021, 12:56

Thank you for this, it works :o :o :o .

Last question how about to validate if attribute exist ? just add it on a product only.

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

Re: Create Product Attribute

Post by aimeos » 19 May 2021, 07:47

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, Image give us a star

Post Reply