<?php declare(strict_types=1);
namespace KmPitchPrint\Subscribers;
#use App\Product;
use KmPitchPrint\Services\ProductPitchPrintService;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
#use Shopware\Core\Checkout\Cart\LineItem\LineItem;
#use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Customer\Event\CustomerWishlistLoaderCriteriaEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use KmPitchPrint\Events\AfterWishlistDeleteEvent;
use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
use Shopware\Storefront\Page\Wishlist\GuestWishlistPageLoadedEvent;
use Symfony\Contracts\EventDispatcher\Event;
class PitchPrintSubscriber implements EventSubscriberInterface
{
/**
* @var SalesChannelRepositoryInterface
*/
private $productRepository;
private $orderLineItemRepository;
/**
* @var CartService
*/
private $cartService;
/**
* @var RequestStack
*/
private $requestStrack;
/**
* @var ProductPitchPrintService
*/
private $productPitchPrintService;
/**
* @var EntityRepositoryInterface
*/
private $pitchPrintProductRepository;
//public function __construct(SalesChannelRepositoryInterface $productRepository, CartService $cartService)
public function __construct(
SalesChannelRepositoryInterface $productRepository,
EntityRepositoryInterface $orderLineItemRepository,
EntityRepositoryInterface $pitchPrintProductRepository,
CartService $cartService,
ProductPitchPrintService $productPitchPrintService,
RequestStack $requestStack
)
{
$this->orderLineItemRepository = $orderLineItemRepository;
$this->pitchPrintProductRepository = $pitchPrintProductRepository;
$this->productRepository = $productRepository;
$this->cartService = $cartService;
$this->productPitchPrintService = $productPitchPrintService;
$this->requestStrack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class =>'afterCartConverted',
#BeforeLineItemAddedEvent::class => 'onBeforeLineItemAdded',
#GuestWishlistPageletLoadedEvent::class => 'guestWishlistPageletLoaded',
CheckoutOrderPlacedEvent::class => 'checkoutOrderPlacedEvent',
CustomerWishlistLoaderCriteriaEvent::class =>'customerWishlistLoaderCriteriaEvent',
AfterWishlistDeleteEvent::class => 'deletePitchPrintProject',
WishListPageProductCriteriaEvent::class => 'wishListPageProductCriteriaEvent',
GuestWishlistPageLoadedEvent::class=> 'wishListPageProductCriteriaEvent',
];
}
/**
* @param WishListPageProductCriteriaEvent $event
* @return void
*/
public function wishListPageProductCriteriaEvent( WishListPageProductCriteriaEvent $event){
$event->getCriteria()->addAssociation('media');
$event->getCriteria()->getAssociation('media')->addSorting(new FieldSorting('position'));
}
/**
* @param $criteria CustomerWishlistLoaderCriteriaEvent
* @return void
*/
public function customerWishlistLoaderCriteriaEvent(CustomerWishlistLoaderCriteriaEvent $criteria){
$criteria->getCriteria()->addAssociation('productoptions');
$criteria->getCriteria()->addAssociation('pitchPrintProduct');
$criteria->getCriteria()->addFilter(new EqualsFilter('pitchPrintProduct.cookieId',
$this->requestStrack->getCurrentRequest()->cookies->get('pp_id')));
$criteria->getCriteria()->addAssociations([
'productoptions.group',
'productoptions.option',
'productoptions.group.productoptionsgroup',
'productoptions.group.productoptionsgroup.parent',
'productoptions.option.productoptionsoption',
//'productoptions.option.productoptionsoption.media'
]);
}
public function checkoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event):void{
/*
$order = $event->getOrder();
$i = 1;
foreach ($order->getLineItems() as $lineItem){
if ($lineItem->getType() == 'product' || $lineItem->getType() == 'product-with-options'){
$this->orderLineItemRepository->upsert([
[
'id'=>$lineItem->getId(),
'humanReadableId'=>[
'lineItemId' => $lineItem->getId(),
'readableId' => $i,
'orderNumber' => $order->getOrderNumber()
]
]
],$event->getContext());
}
$i++;
}*/
$this->requestStrack->getCurrentRequest()->cookies->remove('pp_id');
}
public function guestWishlistPageletLoaded(GuestWishlistPageletLoadedEvent $event): void{
$products = $event->getPagelet()->getSearchResult()->getProducts();
if($event->getRequest()->cookies->get('pp_id')){
$cart = $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
$lineItems = $cart->getLineItems();
$productMatches = $products->filter(function(SalesChannelProductEntity $product) use ($lineItems){
foreach($lineItems as $lineItem ){
return $lineItem->getReferencedId() == $product->getId();
}
});
foreach($productMatches as $productMatch){
dump($productMatch->getCover());
}
#dump($cart->getLineItems()->getReferenceIds());exit;
}
}
public function onBeforeLineItemAdded(BeforeLineItemAddedEvent $event):void
{
return;
$lineItem = $event->getLineItem();
$previews = $lineItem->getPayload()['features']['pitch_print']['previews'];
$criteria = New Criteria([$event->getLineItem()->getId()]);
/**
* @var SalesChannelProductEntity
*/
$product = $this->productRepository->search($criteria, $event->getSalesChannelContext())->first();
#dump($product->getCover()->getMedia());exit;
$i=0;
foreach ($product->getCover()->getMedia() as $mediaEntity) {
//replace only previews, if the mediaItems are more, skip the rest
if (isset($previews[$i])) {
$samplePreview = $previews[$i];
} else {
break;
}
foreach ($mediaEntity->getMedia()->getThumbnails() as $thumbnail) {
$thumbnail->setUrl($samplePreview);
}
$mediaEntity->getMedia()->setUrl($samplePreview);
$i++;
}
$lineItem->setCover($product->getCover()->getMedia());
}
public function deletePitchPrintProject(AfterWishlistDeleteEvent $event): void{
$ppProjectId = $event->getPpProject()->getProjectId();
$swProjectId = $event->getPpProject()->getId();
}
/**
* adds productId to product-type: 'product-with-options' (by default it's null, product_id is required for KM_PP (python))
* @param CartConvertedEvent $event
* @return void
*/
public function afterCartConverted(CartConvertedEvent $event){
$data = $event->getConvertedCart();
$lineItems = [];
foreach($data['lineItems'] as $lineItem){
if ($lineItem['type']=='product-with-options'){
foreach($data['lineItems'] as $pLineItem){
if(isset($pLineItem['parentId'])){
if($pLineItem['type'] == 'product' && $pLineItem['parentId'] == $lineItem['id'] ){
$lineItem['productId'] = $pLineItem['productId'];
}
}
}
}
$lineItems[] = $lineItem;
}
$data['lineItems'] = $lineItems;
$event->setConvertedCart($data);
}
/**
* @param ProductPageLoadedEvent $event
* @return void
*/
public function productPageLoaded(ProductPageLoadedEvent $event){
$product = $event->getPage()->getProduct();
foreach($product->getPrice() as $price){
/**
* @var \Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price
*/
$price->setGross($price->getGross()*$product->getMinPurchase());
$price->setNet($price->getNet()*$product->getMinPurchase());
}
#dump($product->getPrice());exit;
}
}