ich habe eine Twig-Funktion erstellt. Wie kann ich hier z.B. die Produkte anzeigen?
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class CustomExtension extends AbstractExtension {
public function __construct() {
}
public function getFunctions() {
return [
new TwigFunction('getProductsByCategory', [$this, 'getProducts']),
];
}
public function getProducts($categoryID) {
/* WIE kann ich hier die Produkte auflisten? */
}
}
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class CustomExtension extends AbstractExtension {
private $productRepository;
private $categoryRepository;
public function __construct(EntityRepositoryInterface $productRepository, EntityRepositoryInterface $categoryRepository) {
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
}
public function getFunctions() {
return [
new TwigFunction('getProductsByCategory', [$this, 'getProducts']),
];
}
public function getProducts($categoryID) {
$criteria = new Criteria();
$criteria->addAssociation('children');
$criteria->addAssociation('children.products');
$criteria->addAssociation('children.products.seoUrls');
$criteria->addFilter(new EqualsFilter('id', $parentCategoryID));
$categories = $this->categoryRepository->search(
$criteria,
Context::createDefaultContext()
);
}
}