<?php
namespace Pixup\Wishlist\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Pixup\Wishlist\Core\Boot;
use Shopware\Core\Checkout\Cart\Event\LineItemRemovedEvent;
use Symfony\Component\HttpFoundation\RequestStack;
class CartSubscriber implements EventSubscriberInterface
{
private $boot;
public function __construct(Boot $boot,RequestStack $requestStack)
{
$this->boot = $boot;
//set sessionID because shopware does not set it if its an API call
$token = substr(bin2hex($requestStack->getCurrentRequest()->headers->get("sw-context-token")),0,32);
if(isset($token) && !isset($_SESSION['_sf2_attributes']['sessionId'])) {
$_SESSION['_sf2_attributes'] = array('sessionId' =>$token);
}
}
public static function getSubscribedEvents()
{
return [
LineItemRemovedEvent::class => "removeFromBirthdayist",
CheckoutOrderPlacedEvent::class => "removeProductFromWishlist",
CheckoutCartPageLoadedEvent::class => "addInfoToCheckoutCart",
OffcanvasCartPageLoadedEvent::class => "addInfoToOffcanvasCart"
];
}
/**
* @param CheckoutCartPageLoadedEvent $event
* @description adds information to lineItem
*/
private function addInfoToCart(PageLoadedEvent $event){
$salesChannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
if($this->boot->getConfig($salesChannelId)['fullAjaxMode'])
return;
$wishListCookieHandler = $this->boot->getFacade()->getCookieHandler();
$wishListEntityHandler = $this->boot->getFacade()->getWishlistEntityHandler();
$customer = $wishListEntityHandler->getWishlistCustomer(
($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
);
$customerId = $customer ? $customer->getId() : null;
if($customerId == null)
return;
if($this->boot->getConfig($salesChannelId)['showOnCart']) {
/**
* @var LineItem $lineItem
*/
foreach ($event->getPage()->getCart()->getLineItems() as $lineItem) {
$lineItem->setExtensions(array_merge($lineItem->getExtensions()??[],[
'isOnWishlist' => $this->boot->getFacade()->getWishlistEntityHandler()->checkIfProductExists($lineItem->getId(), $salesChannelId, $customerId)
]));
}
}
}
/**
* @param OffcanvasCartPageLoadedEvent $event
* @description adds configuration value to pagestruct
*/
public function addInfoToOffcanvasCart(OffcanvasCartPageLoadedEvent $event): void{
$salesChannelId = $event->getSalesChannelContext()->getSalesChannelId();
$event->getPage()->setExtensions(array_merge($event->getPage()->getExtensions()??[],[
"pixup"=>[
"showOnCart"=>$this->boot->getConfig($salesChannelId)['showOnOffcanvasCart'],
"showOnCartImage"=>$this->boot->getConfig($salesChannelId)['showOnOffcanvasCartImage'],
"antiCacheMode"=>$this->boot->getConfig($salesChannelId)['antiCacheMode'],
"fullAjaxMode"=>$this->boot->getConfig($salesChannelId)['fullAjaxMode']
]
]));
$this->addInfoToCart($event);
}
/**
* @param CheckoutCartPageLoadedEvent $event
* @description adds configuration value to pagestruct
*/
public function addInfoToCheckoutCart(CheckoutCartPageLoadedEvent $event): void{
$salesChannelId = $event->getSalesChannelContext()->getSalesChannelId();
$event->getPage()->setExtensions(array_merge($event->getPage()->getExtensions()??[],[
"pixup"=>[
"showOnCart"=>$this->boot->getConfig($salesChannelId)['showOnCart'],
"showOnCartImage"=>$this->boot->getConfig($salesChannelId)['showOnCartImage']
]
]));
$this->addInfoToCart($event);
}
/**
* @param LineItemRemovedEvent $event
* @description will check the birthday table and removes entrys from there if a lineItem is removed that whas added
* from a birthday wishlist
*/
public function removeFromBirthdayist(LineItemRemovedEvent $event) :void{
$wishListEntityHandler = $this->boot->getFacade()->getWishlistEntityHandler();
$wishListCookieHandler = $this->boot->getFacade()->getCookieHandler();
$productId = $event->getLineItem()->getId();
$customer = $wishListEntityHandler->getWishlistCustomer(
($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
);
$customerId = $customer ? $customer->getId() : null;
if($customerId == null)
return;
$wishListEntityHandler->removeUserFromBirthdayListByProductId($productId,$customerId);
}
/**
* @param CheckoutOrderPlacedEvent $event
* @description removes a product from the subscribed or owned birthday wishlist if customer completly orderd a item
*/
public function removeProductFromWishlist(CheckoutOrderPlacedEvent $event){
$wishListEntityHandler = $this->boot->getFacade()->getWishlistEntityHandler();
$wishListCookieHandler = $this->boot->getFacade()->getCookieHandler();
$salesChannelId = $event->getSalesChannelId();
if($event->getOrder()->getOrderCustomer()->getCustomer()->getGuest())
$customer = null;
else
$customer = $event->getOrder()->getOrderCustomer()->getCustomer();
$customer = $wishListEntityHandler->getWishlistCustomer(
($customer==null)?null:$customer->getId(),
($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
);
try {
$customerId = $customer ? $customer->getId() : null;
/**
* @var OrderLineItemEntity $orderLineItem
*/
foreach($event->getOrder()->getLineItems()->getElements() as $orderLineItem){
$wishlistId = $wishListEntityHandler->removeUserFromBirthdayListByProductId($orderLineItem->getProductId(),$customerId);
if(!empty($wishlistId))
$wishListEntityHandler->deleteProductFromBirthdayWishlist([$orderLineItem->getProductId()],$wishlistId,$customerId,$salesChannelId);
}
}catch(\Exception $e){
}
}
}