<?php declare(strict_types=1);
namespace Vitec\CustomContentPdp\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Checkout\Cart\LineItemFactoryRegistry;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
use Shopware\Core\Checkout\Cart\Event\CartSavedEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Defaults;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Content\Category\CategoryEntity;
use DreiscSeoPro\Core\Content\Category\CategoryRepository;
use Shopware\Core\Content\Category\CategoryDefinition;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\Context;
//~ use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class MySubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $productRepository;
private $categoryRepository;
private RequestStack $requestStack;
private LineItemFactoryRegistry $factory;
private CartService $cartService;
private $connection;
public function __construct(
EntityRepositoryInterface $productRepository,
RequestStack $requestStack,
LineItemFactoryRegistry $factory,
CartService $cartService,
Connection $connection,
EntityRepositoryInterface $categoryRepository
)
{
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->requestStack = $requestStack;
$this->factory = $factory;
$this->cartService = $cartService;
$this->connection = $connection;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded',
];
}
// public function getCategoryById(string $categoryId, Context $context): ?CategoryEntity
// {
// // Fetch the category by its ID
// $criteria = new Criteria([$categoryId]);
// $criteria->addAssociations(['media', 'translations','customFields']);
// return $this->categoryRepository->search($criteria, $context)->first();
// }
public function getCategoryById(string $categoryId, Context $context , $languageId = string): ?CategoryEntity
{
// Define the criteria with custom fields associations
$criteria = (new Criteria([$categoryId]))->addAssociations([
//~ 'media',
//~ 'translations',
'customFields', // Replace 'customFields' with the actual name of your custom field association
]);
//~ $criteria->addFilter(new EqualsFilter('category_translation.language_id', $languageId));
// Fetch the category by its ID along with custom fields
return $this->categoryRepository->search($criteria, $context)->first();
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$_SESSION['product_id_insurance_add'] = 0;
$Context = $event->getContext();
$page = $event->getPage();
$category_value = [];
$product = $page->getProduct();
$categories = $product->getCategoryTree();
$arrCustomFields = $product->getCustomFields();
$context1 = Context::createDefaultContext();
$context = Context::createDefaultContext();
$languageId = $event->getSalesChannelContext()->getSalesChannel()->getLanguageId();
foreach ($categories as $category) {
$categorydetail = $this->getCategoryById($category, $context1,$languageId);
// dd($categorydetail instanceof CategoryEntity);
if ($categorydetail instanceof CategoryEntity) {
}
if($categorydetail->customFields) {
$category_description[] = $categorydetail->customFields;
foreach ($category_description as $transaction) {
if(!empty($transaction['custom_category_description'])) {
$category_value[] = $transaction['custom_category_description'];
}
}
}
}
if(!empty($category_value)) {
$catCustomConetentArray = array("mactradePDP" => [$category_value[0]],'totalcount' => count($category_value));
$event->getPage()->addExtension('mactradePDPPageValue', new ArrayEntity($catCustomConetentArray));
}
$criteria = (new Criteria($event->getPage()->getProduct()->getCategoryIds()));
$categories = $this->categoryRepository->search($criteria, $context1);
$catNamearray=[];
$catNamearrayb=[];
foreach($categories as $k => $cat) {
$catNamearray[] = $cat->getName();
$catNamearrayb[] = $cat->getTranslated()['breadcrumb'];
}
$catArray =
['Mac & MacBook' => '4'
,'MacBook Air' => '13'
,'MacBook Pro' => '14'
,'MacBook' => '5'
,'Mac mini' => '15'
,'Mac Studio' => '380'
,'iMac' => '2'
,'iMac Pro' => '11'
,'PowerMac' => '12'
,'Mac Pro' => '16'
,'Apple' => '128'
,'iPad & Tablet' => '150'
,'iPad Pro' => '316'
,'iPad Air' => '318'
,'iPad' => '317'
,'iPad mini' => '319'
,'TV & Musik' => '13'
,'Watch' => '73'
,'Zubehör' => '74'
,'iPhone & Handy' => '18'
,'Apple Tv' => '321'
,'Apple Watch' => '102'
];
$catArraytext =
[
'für Ihr altes MacBook Air' => '13'
,'für Ihr altes MacBook Pro' => '14'
,'für Ihren alten Mac mini' => '15'
,'für Ihren alten Mac Studio' => '380'
,'für Ihren alten iMac' => '2'
,'für Ihren alten Mac Pro' => '16'
,'für Ihr altes iPad Pro' => '316'
,'für Ihr altes iPad Air' => '318'
,'für Ihr altes iPad' => '317'
,'für Ihr altes iPad mini' => '319'
,'für Ihr altes iPhone' => '18'
,'für Ihren alten Apple TV' => '321'
,'für Ihre alte Apple Watch ' => '102'
];
$foundKey = null;
foreach($catNamearray as $catName) {
//~ $catName = $categories->getTranslated()['name'];
$catName = strtolower($catName);
foreach ($catArray as $key => $value) {
if($catName == strtolower('iPhone')) {
$foundKey = '18';
break;
}
if($catName == strtolower('MacBook Pro') || $catName == strtolower('14“ MacBook Pro') || $catName == strtolower('16“ MacBook Pro')) {
$foundKey = '14';
break;
}
if($catName == strtolower('iPad Air')) {
$foundKey = '318';
break;
}
if($catName == strtolower('iPad mini')) {
$foundKey = '319';
break;
}
if($catName == strtolower('iPad')) {
$foundKey = '317';
break;
}
if ($catName == $key) {
$foundKey = $catArray[$key];
break;
}
}
}
if($foundKey == null) {
$breads = array_values($catNamearrayb);
foreach($breads as $k => $bread) {
//~ $remove1 = array_shift($bread);
foreach($bread as $k => $catgory) {
$catgory = strtolower($catgory);
foreach ($catArray as $key => $value) {
$pattern = "/$catgory/i";
if($catgory == strtolower('iPhone')) {
$foundKey = '18';
break;
}else if($catgory == strtolower('watch')) {
$foundKey = '102';
break;
}else if($catgory == strtolower('iPad Air')) {
$foundKey = '318';
break;
}
//~ else if ($catgory == 'mac') {
//~ $foundKey = '4';
//~ break;
//~ }
else if ($catgory == strtolower($key)) {
$foundKey = $catArray[$key];
break;
}
else if (strpos($pattern, strtolower($key)) !== false) {
}
}
}
}
}
$catArraytext = array_flip($catArraytext);
$newArray = [];
if ($foundKey !== null) {
$newArray['keyApi'] = $foundKey;
$newArray['keyApiText'] = $catArraytext[$foundKey];
} else {
//~ $newArray['keyApi'] = '18';
//~ $newArray['keyApiText'] = $catArraytext[18];
//~ return;
}
if($newArray) {
$event->getPage()->addExtension('amMactradeap1', new ArrayEntity($newArray));
}
}
public function onProductsLoaded(EntityLoadedEvent $event)
{
dd($event);
}
}