How can I update a list item like text content

Questions around the TYPO3 integration and plugins
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!
claude27
Posts: 12
Joined: 25 Jul 2024, 11:24

How can I update a list item like text content

Post by claude27 » 04 Nov 2024, 22:15

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:

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);
    }
TYPO3: 12.4.22
Aimeos: 24.4.2
PHP: 8.3

Thank you very much for any tip!

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

Re: How can I update a list item like text content

Post by aimeos » 05 Nov 2024, 15:23

You can see in the CSV import how texts (and prices) are updated:
https://github.com/aimeos/ai-controller ... #L119-L164
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

claude27
Posts: 12
Joined: 25 Jul 2024, 11:24

Re: How can I update a list item like text content

Post by claude27 » 06 Nov 2024, 10:42

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?

claude27
Posts: 12
Joined: 25 Jul 2024, 11:24

Re: How can I update a list item like text content

Post by claude27 » 07 Nov 2024, 11:45

PS: I managed to overwrite (update) a modified text item of a product:

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);
But how do I get to know the refId of a text (or price or category)?

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

Re: How can I update a list item like text content

Post by aimeos » 07 Nov 2024, 18:19

When you load the product, you need to specify with related items you want to load too, e.g.:

Code: Select all

$product = $productManager->find($event->getUid(), ['price', 'text']);
The second parameter of the find() method takes care about that.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

technoxprt
Posts: 8
Joined: 07 Mar 2018, 11:33

Re: How can I update a list item like text content

Post by technoxprt » 08 Nov 2024, 06:20

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.

claude27
Posts: 12
Joined: 25 Jul 2024, 11:24

Re: How can I update a list item like text content

Post by claude27 » 08 Nov 2024, 11:53

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:

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 );
    }

Post Reply