<?php declare(strict_types=1);
namespace Itptheme\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
class ItpthemeSubscriber implements EventSubscriberInterface
{
protected EntityRepositoryInterface $shippingMethodRepository;
public function __construct(EntityRepositoryInterface $shippingMethodRepository)
{
$this->shippingMethodRepository = $shippingMethodRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductsLoaded',
'product.loaded' => 'onEntityLoadedEvent',
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartLoaded',
CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
GenericPageLoadedEvent::class => 'onGenericPageLoaded'
];
}
public function onEntityLoadedEvent(EntityLoadedEvent $event)
{
$products = $event->getEntities();
foreach ($products as $product) {
$p = $product->{"customFields"};
$tmp = array();
$tmp = $this->checkZeiss($p, $tmp);
$tmpStr = join(', ', array_unique($tmp));
$tmpStr = trim($tmpStr);
$tmpStr = trim($tmpStr, ',');
$tmpStr = trim($tmpStr);
if (count($tmp) > 0) {
$p['show_extended_itp_skus'] = '1';
$p['extended_itp_skus'] = $tmpStr;
} else {
$p['show_extended_itp_skus'] = '0';
}
$product->{"customFields"} = $p;
}
}
public function onProductsLoaded(ProductPageLoadedEvent $event)
{
$productData = $event->getPage()->getProduct()->getCustomFields();
$tmp = array();
$tmp = $this->checkZeiss($productData, $tmp);
if (count($tmp) > 0) {
$tmpStr = join(', ', array_unique($tmp));
$tmpStr = trim($tmpStr);
$tmpStr = trim($tmpStr, ',');
$tmpStr = trim($tmpStr);
$event->getPage()->assign(['show_extended_itp_skus' => '1']);
$event->getPage()->assign(['extended_itp_skus' => $tmpStr]);
} else {
$event->getPage()->assign(['show_extended_itp_skus' => '0']);
}
}
public function checkZeiss($element, $tmp)
{
if (@$element['itp_product_attributes_zeiss_no'] != '') {
$tmp[] = $element['itp_product_attributes_zeiss_no'];
}
if (@$element['itp_product_attributes_zeiss_no_2'] != '') {
$tmp[] = $element['itp_product_attributes_zeiss_no_2'];
}
return $tmp;
}
public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event)
{
$this->selectAndSortShippingMethods($event);
}
public function onOffcanvasCartLoaded(OffcanvasCartPageLoadedEvent $event)
{
$this->selectAndSortShippingMethods($event);
}
public function onCheckoutCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event)
{
$this->selectAndSortShippingMethods($event);
}
private function selectAndSortShippingMethods($event)
{
$shippingMethodIds = $event->getPage()->getShippingMethods()->getIds();
$context = $event->getContext();
$context->setConsiderInheritance(true);
$orFilters = [];
foreach ($shippingMethodIds as $shippingMethodId) {
$orFilters[] = new EqualsFilter('id', $shippingMethodId);
}
$criteria = new Criteria();
$criteria->addAssociation('prices');
$criteria->addAssociation('deliveryTime');
$criteria->addAssociation('tax');
$criteria->addFilter(new OrFilter($orFilters));
$shippingMethods = $this->shippingMethodRepository->search($criteria, $context);
$sortArray = $this->sortArray($shippingMethods->getElements(), $this->getShippingNetPrices($shippingMethods));
$sortedShippingMethods = new \Shopware\Core\Checkout\Shipping\ShippingMethodCollection($sortArray);
$event->getPage()->setShippingMethods($sortedShippingMethods);
}
private function getShippingNetPrices($shippingMethods)
{
$shippingMethodsWithNetPrices = [];
foreach ($shippingMethods as $shippingMethod) {
$netPrice = null;
$priceCollection = $shippingMethod->getPrices()->getElements();
if (is_array($priceCollection) && !empty($priceCollection)) {
$firstPrice = reset($priceCollection);
if ($firstPrice instanceof \Shopware\Core\Checkout\Shipping\Aggregate\ShippingMethodPrice\ShippingMethodPriceEntity) {
$netPrice = $firstPrice->getCurrencyPrice()->first()->getNet();
}
}
$shippingMethodsWithNetPrices[$shippingMethod->getId()] = $netPrice;
}
return $shippingMethodsWithNetPrices;
}
private function sortArray($sortArray, $shippingMethodsWithNetPrices)
{
uasort($sortArray, function ($a, $b) use ($shippingMethodsWithNetPrices) {
$netPriceA = $shippingMethodsWithNetPrices[$a->getId()] ?? 0;
$netPriceB = $shippingMethodsWithNetPrices[$b->getId()] ?? 0;
if ($netPriceA === $netPriceB) {
return 0;
}
return ($netPriceA < $netPriceB) ? -1 : 1;
});
return $sortArray;
}
public function onGenericPageLoaded(GenericPageLoadedEvent $event)
{
$customerLoggedIn = false;
if ($event->getSalesChannelContext()->getCustomer()) {
$customerLoggedIn = true;
}
$event->getPage()->setExtensions(['customer_logged_in' => $customerLoggedIn]);
}
}