Hey,
I wrote a little Twig Extension, which I pass a product ID and the context as a parameter and it will return me a SalesChannelProductEntity to use it later in the Template, which is done in another class called GetProductsByID and the method getProduct.
public function getFunctions()
{
return [
new TwigFunction('getProductByID', [$this, 'TwigGetProductByID']),
];
}
public function TwigGetProductByID($productID, SalesChannelContext $salesChannelContext):SalesChannelProductEntity
{
if ($this->salesChannelProductRepository instanceof SalesChannelRepositoryInterface)
{
if ($salesChannelContext instanceof SalesChannelContext)
{
$productByID = new GetProductsByID($this->salesChannelProductRepository, $salesChannelContext); //Instance of my Class to get the rroduct
$product = $productByID->getProduct($productID); // Call the function to return the product
print_r($product); //this prints the whole SalesChannelProductEntity object fine
return $product;
}
}
}
The call in the template with the product ID and the actual SalesChannelContext as parameter:
{% set product1 %}
{{ getProductByID('2a88d9b59d474c7e869d8071649be43c', context) }}
{% endset %}
{{ dump(product1) }}
The print_r() before the return outputs me the whole filled SalesChannelProductEntity object.
But when I set my variable product1 in my template and dump it, it will display just the product name and the charset in a Twig/Markup.
Twig\Markup {#41665 ▼
-content: "Main product"
-charset: "UTF-8"
}
Any idea what I’m doing wrong? This is my first plugin attempt and I’m racking my brain for days.
Best regards
Alex