<?php declare(strict_types=1);
namespace Crehler\MojeBambinoEngine\Subscriber;
use Crehler\MojeBambinoEngine\Service\PageLinksService;
use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductSubscriber implements EventSubscriberInterface
{
private SalesChannelRepository $productRepository;
private EntityRepository $categoryRepository;
protected ProductStatusStorefrontService $productStatus;
protected PageLinksService $pageLinks;
public function __construct(
SalesChannelRepository $productRepository,
EntityRepository $categoryRepository,
ProductStatusStorefrontService $productStatus,
PageLinksService $pageLinks
)
{
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->productStatus = $productStatus;
$this->pageLinks = $pageLinks;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
// ProductLoaderCriteriaEvent::class => 'onProductLoaderCriteria',
ProductPageCriteriaEvent::class => 'onProductLoaderCriteria',
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
/**
* @param ProductPageCriteriaEvent $event
*/
//public function onProductLoaderCriteria(ProductLoaderCriteriaEvent $event): void
public function onProductLoaderCriteria(ProductPageCriteriaEvent $event): void
{
try {
$event
->getCriteria()
->addAssociation('pageLinks')
->addAssociation('alternativeProducts')
->addAssociation('checkoutCrossSellingStream')
->addAssociation('crProductStatus')
->addAssociation('crProductStatusId')
->getAssociation('visibilities')
->addFilter(new EqualsFilter('salesChannelId', $event->getSalesChannelContext()->getSalesChannel()->getId()));
} catch (\Throwable $exception) {
dd($exception);
}
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$product = $event->getPage()->getProduct();
if ($event->getRequest()->get("c")) {
$criteria = (new Criteria())->addFilter(new EqualsFilter('autoIncrement', $event->getRequest()->get("c")));
/** @var CategoryEntity $category */
$category = $this->categoryRepository->search($criteria, Context::createDefaultContext())->first();
$event->getPage()->getProduct()->addExtension('setBreadcrumb', $category);
}
$alternativeProducts = $product->getExtension('alternativeProducts');
if ($alternativeProducts != null) { /* ------------- dodaĆam warunek ------ */
$alternativeProductsIds = array_keys($alternativeProducts->getElements());
}
if (!empty($alternativeProductsIds)) {
$product->addExtension('alternativeProductReal', $this->getProductAlternative(
$alternativeProductsIds,
$event->getSalesChannelContext()
));
}
$product->addExtension('productStatus', $this->productStatus->getProductStatus($product, $event->getContext()));
$product->addExtension('pageLinks', $this->pageLinks->getProductPageLinks($product, $event->getContext()));
$categoryTree = $product->getCategoryTree();
$cat_mb_id = $cat_ze_sciezki = null;
$referer = $event->getRequest()->headers->get('referer');
if ($referer != "") {
$ref_tab = explode("/", $referer);
/* echo "<pre>";
print_r($ref_tab);
print_r($product->getCategoryTree());
echo "</pre>"; */
if (count($ref_tab) == 5) {
$cat_mb_id = (int)$ref_tab[3];
//$a = $this->getCategoryByAutoIncr($cat_mb_id, $event->getContext());
//$criteria = new Criteria(['f92569b5dd4247bb94c6d412da11a6e9', '840dcda202934addb055b091937afa20']);
$criteria = new Criteria($product->getCategoryTree());
$wynik = $this->categoryRepository->search($criteria, $event->getContext());
foreach ($wynik as $wyn) {
//echo "<pre>"; print_r($wyn->getAutoIncrement()); echo "</pre>";
if ($wyn->getAutoIncrement() == $cat_mb_id) {
$cat_ze_sciezki = $wyn->getId();
break;
}
}
}
}
$catId = null;
if ($product->getMainCategories() != null) {
$mainCategories = $product->getMainCategories()->getElements();
}
//echo "<pre>"; print_r($mainCategories); echo "</pre>";
if (!empty($mainCategories)) {
foreach ($mainCategories as $cat) {
//echo "<pre>"; print_r($cat->getCategoryId()); echo "</pre>";
$catId = $cat->getCategoryId();
}
}
$treeCategoryId = null;
if (is_array($categoryTree)) {
$treeCategoryId = end($categoryTree);
}
if ($cat_ze_sciezki) {
$product->addExtension('categoryBreadcrumb', $this->getCategory($cat_ze_sciezki, $event->getContext()));
} elseif ($catId) {
$product->addExtension('categoryBreadcrumb', $this->getCategory($catId, $event->getContext()));
} else {
if ($treeCategoryId) {
$product->addExtension('categoryBreadcrumb', $this->getCategory($treeCategoryId, $event->getContext()));
} else {
$product->addExtension('categoryBreadcrumb', null);
}
}
if ($product->getVisibilities() != null) {
if ($product->getVisibilities()->first()
&& $product->getVisibilities()->first()->getVisibility() === ProductVisibilityDefinition::VISIBILITY_LINK) {
$product->setAvailable(false);
}
}
}
private function getProductAlternative(array $productIds, SalesChannelContext $context): EntityCollection
{
$criteria = new Criteria($productIds);
$criteria->addFilter(new ProductAvailableFilter($context->getSalesChannel()->getId()));
return $this->productRepository->search($criteria, $context)->getEntities();
}
private function getCategory(string $categoryId, Context $context): ?CategoryEntity
{
$criteria = new Criteria([$categoryId]);
return $this->categoryRepository->search($criteria, $context)->get($categoryId);
}
}