adding stocklevel by PHP controller to new product

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

adding stocklevel by PHP controller to new product

Post by claude27 » 01 Nov 2024, 10:35

Hi,

I've created a secured area where frontend users can add events (for which later places will be sold with aimeos). Now I want to synchronize it automatically with the Ameos shop, starting now with creating new aimeos product items by a controller. I managed it already partially (title, type and price are created), but now I'm struggling on how to create the stock. Creating the price is working, I tried the same principle for the stocklevel but this seems not to work. What am I making wrong?

Code: Select all

       $config = Base::config((array) $this->settings);
        //   $context = \Aimeos\TYPO3\Utility\Helper::getContext();
        $context = \Aimeos\Aimeos\Base::context($config);

        $productManager = MShop::create($context, 'product');

        $product = $productManager->create();

        $product->setCode($event->getUid()); // Eindeutiger Produktcode
        $product->setLabel($event->getTitle()); // Produktname
        $product->setType('event'); // Produkttyp

        // Create price (is working)
        $priceManager = MShop::create($context, 'price');
        $price = $priceManager->create()->setValue($event->getKosten())->setCurrencyId('EUR');

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

        // Create stock
        $stockManager = MShop::create($context, 'stock');
        $stock = $stockManager->create()->setProductId($event->getUid())->setType( 'default' )->setStockLevel($event->getTeilnehmerzahl());

        $stockManager->save( $stock );

        // Produkt speichern
        $product->setModified();
        $productManager->save($product);
TYPO3: 12.4.22
Aimeos: 24.4.2
PHP: 8.3

Thank you!!!

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

Re: adding stocklevel by PHP controller to new product

Post by aimeos » 01 Nov 2024, 15:59

Saving stock needs the product ID, not your event ID and you need to save the product before saving the stock item.
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: adding stocklevel by PHP controller to new product

Post by claude27 » 04 Nov 2024, 18:47

Thank you very much! That was the solution.

Post Reply