How can I update a list item like text content
Forum rules
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
How can I update a list item like text content
Hi,
How can I please update a list item like text, price or categories via a php controller?
My goal is to create, modify and delete events by (TYPO3) frontendusers. Creating and Deleting is actually working.
I managed to create functions in the controller class to create and delete products and a function to update simple attributes like the label but I'm not achieving to update a (list?) item like 'text'. In summery, I want to update a text item, a price item and a category item.
Unfortunately I didn' find a solution to update the items directly in the documentation. Do I have to delete the items and settting them afterwards with the new value?
Here is my surely faulty code:
TYPO3: 12.4.22
Aimeos: 24.4.2
PHP: 8.3
Thank you very much for any tip!
How can I please update a list item like text, price or categories via a php controller?
My goal is to create, modify and delete events by (TYPO3) frontendusers. Creating and Deleting is actually working.
I managed to create functions in the controller class to create and delete products and a function to update simple attributes like the label but I'm not achieving to update a (list?) item like 'text'. In summery, I want to update a text item, a price item and a category item.
Unfortunately I didn' find a solution to update the items directly in the documentation. Do I have to delete the items and settting them afterwards with the new value?
Here is my surely faulty code:
Code: Select all
/**
* Action zum Updaten eines Aimeos-Produkts
* @param \Unterleitner\Sitepackage\Domain\Model\Event $event
*
* @return void
*/
public function updateProductAction(\Unterleitner\Sitepackage\Domain\Model\Event $event = NULL, $categoryUrl = NULL ): void {
$config = Base::config((array) $this->settings);
$context = \Aimeos\Aimeos\Base::context($config);
// Produkt-Manager instanziieren
$productManager = MShop::create($context, 'product');
$product = $productManager->find($event->getUid());
$product->setLabel($event->getTitle());
$startdate = $event->getKursbeginn();
$newTimezone = new \DateTimeZone('Europe/Berlin');
$startdate->setTimezone($newTimezone);
$enddate = $event->getKursende();
$enddate->setTimezone($newTimezone);
$product->setDatestart($startdate->format('Y-m-d H:i'));
$product->setDateend($enddate->format('Y-m-d H:i'));
$product->setModified();
$productManager->save($product);
// until here it is working
// the following is not working:
$contentManager = MShop::create( $context, 'text' );
$content = $contentManager->create() -> deleteListItem( 'text', $product->getId() );
// Create content
$text = $contentManager->create()->setContent($event->getDescription())->setType('name')->setLanguageId('de')->setLabel('name/de');
// Add Text to product
$product->addListItem( 'text', $contentManager->createListItem()->setRefId( $product->getId()), $text );
$content->setModified();
$contentManager->save($content);
}
Aimeos: 24.4.2
PHP: 8.3
Thank you very much for any tip!
Re: How can I update a list item like text content
You can see in the CSV import how texts (and prices) are updated:
https://github.com/aimeos/ai-controller ... #L119-L164
https://github.com/aimeos/ai-controller ... #L119-L164
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
Re: How can I update a list item like text content
Thank you!
Unfortunately I didn't take a step forward.
$listItems or $product->getListItems (the item which should be loaded from the product which is saved in the database) is always empty.
Could it be because $product is in my class of type Aimeos\MShop\Product\Item\Standard but the function expects $product of type \Aimeos\MShop\Product\Item\Iface ?
If yes, how to I get a $product of type \Aimeos\MShop\Product\Item\Iface? Could it be due to a missing included class?
Unfortunately I didn't take a step forward.
$listItems or $product->getListItems (the item which should be loaded from the product which is saved in the database) is always empty.
Could it be because $product is in my class of type Aimeos\MShop\Product\Item\Standard but the function expects $product of type \Aimeos\MShop\Product\Item\Iface ?
If yes, how to I get a $product of type \Aimeos\MShop\Product\Item\Iface? Could it be due to a missing included class?
Re: How can I update a list item like text content
PS: I managed to overwrite (update) a modified text item of a product:
But how do I get to know the refId of a text (or price or category)?
Code: Select all
$contentManager = MShop::create($context, 'text');
$refId = 744; // example Id
$text = $contentManager->create()->setId($refId)->setContent($event->getDescription())->setType('name')->setLanguageId('de')->setLabel('name/de');
$contentManager->save($text);
Re: How can I update a list item like text content
When you load the product, you need to specify with related items you want to load too, e.g.:
The second parameter of the find() method takes care about that.
Code: Select all
$product = $productManager->find($event->getUid(), ['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
-
- Posts: 8
- Joined: 07 Mar 2018, 11:33
Re: How can I update a list item like text content
For updating list items like text, price, or categories, it looks like you're mostly there but missing a few steps for attaching the text item to the product.
Re: How can I update a list item like text content
Thank you very much @aimeos! That was the right clue. Now I was able to complete the update action.
For anyone who needs an example, here is the source code of the function:
For anyone who needs an example, here is the source code of the function:
Code: Select all
/**
* Action for updating an aimeos product
* @param \Unterleitner\Sitepackage\Domain\Model\Event $event
*
* @return void
*/
public function updateProductAction(\Unterleitner\Sitepackage\Domain\Model\Event $event = NULL, $categoryUrl = NULL ): void {
$config = Base::config((array) $this->settings);
$context = \Aimeos\Aimeos\Base::context($config);
// Produkt-Manager instanziieren
$productManager = MShop::create($context, 'product');
$product = $productManager->find($event->getUid(), ['price', 'text', 'catalog','supplier']);
$product->setLabel($event->getTitle());
$startdate = $event->getKursbeginn();
$newTimezone = new \DateTimeZone('Europe/Berlin');
$startdate->setTimezone($newTimezone);
$enddate = $event->getKursende();
$enddate->setTimezone($newTimezone);
$product->setDatestart($startdate->format('Y-m-d H:i'));
$product->setDateend($enddate->format('Y-m-d H:i'));
$listItems = $product->getListItems();
// delete price, text, categories
$product->deleteListItems( $listItems->toArray());
$priceManager = MShop::create($context, 'price');
$price = $priceManager->create()->setValue($event->getKosten())->setCurrencyId('EUR')->setTaxrate(0);
// Add price to product
$product->addListItem( 'price', $productManager->createListItem()->setRefId( $product->getId() ), $price );
// Create content
$contentManager = MShop::create($context, 'text');
$text = $contentManager->create()->setContent($event->getDescription())->setType('name')->setLanguageId('de')->setLabel('name/de');
// Add Text to product
$product->addListItem( 'text', $contentManager->createListItem()->setRefId( $product->getId()), $text );
// save category
$refManager = \Aimeos\MShop::create( $context, 'catalog' );
if ($categoryUrl) {
$catId = $refManager->find($categoryUrl);
$manager = \Aimeos\MShop::create($context, 'product');
$listItem = $manager->createListItem()->setRefId($catId);
$product->addListItem('catalog', $listItem);
$manager->save($product);
}
// Produkt speichern
$product->setModified();
$productManager->save($product);
// Create stock
$stockManager = MShop::create($context, 'stock');
$stockItem = $stockManager->search( $stockManager->filter()->add( ['stock.productid' => $product->getId()] ) );
$stock = $stockManager->delete( $stockItem )->create()->setProductId($product->getId())->setType( 'default' )->setStockLevel($event->getTeilnehmerzahl());
$stockManager->save( $stock );
}