I am wondering, how can I return calculated price of one product for product slider (Goal is to render a product slider with calculated prices for different customers, taxes, and so on). So far I've tried to use Sales Channel Product Repository
, but didn't work (returns null)
Function where I use
public function onPageLoaded(PageLoadedEvent $event): void
{
$context = $event->getContext();
$salesChannelContext = $event->getSalesChannelContext();
$page = $event->getPage();
if (str_contains($event->getRequest()->getRequestUri(), '/link')) {
$parts = explode("/link/", $event->getRequest()->getRequestUri());
$toolId = $this->getToolIdBySlug($parts[1], $context);
if ($toolId) {
$toolsProducts = $this->getToolsProductByToolId($toolId, $context);
$tools = [];
$foo = [];
foreach ($toolsProduct as $tool) {
$tools[] = $tool->getProduct();
$foo[] = $this->getSalesChannelProductById($tool->getProduct()->getId(), $salesChannelContext);
}
dd($foo); //NULL
}
}
}
protected function getToolsProductByToolId(string $toolId, Context $context): array
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('toolId', $recipeId));
$criteria->addAssociation('product');
$criteria->addAssociation('product.cover');
return $this->productToolRepository->search($criteria, $context)->getElements();
}
SalesChannelProductRepository
protected function getSalesChannelProductById(string $productId, SalesChannelContext $salesChannelContext): ?SalesChannelProductEntity
{
/** @var SalesChannelProductEntity|null $salesChannelProduct */
$salesChannelProduct = $this->salesChannelProductRepository
->search(new Criteria([$productId]), $salesChannelContext)
->first();
return $salesChannelProduct;
}
This looks ok to me. If the product with the id passed to getSalesChannelProductById
is available within the sales channel of the context, then it should return an instance of SalesChannelProductEntity
. Check that all the conditions of the responsible ProductAvailableFilter
are met for the product in question.