Search code examples
phptypo3typo3-extensions

TYPO3 EXT:cart_product: extend class product with own extension


I try to extend the existing class Product from the Typo3 (Version 11) cart_products extension with my own extension. Therefor I have already extended the backend and tables to store a new value called productpackagetype.

Next step for me was to implement the new class Product in myext/Classes/Domain/Model/Product/Product.php which looks like this :

<?php

namespace Vendor\myext\Domain\Model\Product;

class Product extends \Extcode\CartProducts\Domain\Model\Product\Product
{

   
    protected $productpackagetype = '';

    public function getProductpackagetype()
    {
        return $this->productpackagetype;
    }

    public function setProductpackagetype($productpackagetype)
    {
        $this->productpackagetype = $productpackagetype;
    }
    
   
}

To tell typo3 to use the new class definition I tried the following code in the myext/ext_localconf.php

$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Extcode\CartProducts\Domain\Model\Product\Product::class] = [
    'className' => \Vendor\myext\Domain\Model\Product\Product::class
];



\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class)
    ->registerImplementation(
        \Extcode\CartProducts\Domain\Model\Product\Product::class,
        \Vendor\myext\Domain\Model\Product\Product::class
    );

As long as I write nothing to myext/ext_localconf.php nothing in the frontend changes. But as soon as I add the above code typo3 comes up with a Error 503 Return value of Extcode\CartProducts\Domain\Model\Product\Product::getBeVariants() must be an instance of TYPO3\CMS\Extbase\Persistence\ObjectStorage, null returned

So what would be the right way to bring up my extended class. And maybe someone can tell me how I tell typo3 to use my extensions private template show.html instead of using cart_products template.

Best regards Johannes


Solution

  • You forgot to map your model to the products table of the extension cart_products. To do so, create a file myext/Configuration/Extbase/Persistence/Classes.php and paste these lines:

    <?php
    declare(strict_types = 1);
    
    return [
        \Vendor\myext\Domain\Model\Product\Product::class => [
            'tableName' => 'tx_cartproducts_domain_model_product_product',
        ],
    ];
    

    You need to flush all the caches to apply this change.

    You can read more about persistence handling in TYPO3 here.

    Concerning your second question: You have to configure the paths to your templates in TypoScript:

    plugin.tx_cartproducts {
        view {
            templateRootPaths.10 = EXT:myext/Resources/Private/Templates/
            partialRootPaths.10 = EXT:myext/Resources/Private/Partials/
            layoutRootPaths.10 = EXT:myext/Resources/Private/Layouts/
        }
    }
    

    Please note the index 10 here. You can configure multiple paths. TYPO3 will start looking for the template in the folder configured with the hightest numbered index. If the file does not exist at that location, it continues with the next lower index.

    When you inspect the file cart_products/Configuration/TypoScript/setup.typoscript you will see that the extension uses the index 0. So if you don't provide any templates, the default ones will be used.

    Make sure to use the same names for folders and files as in the original extension.

    You can read more about template paths in TYPO3 here