<?php declare(strict_types=1);
namespace KmPitchPrint\Subscribers;
use KmPitchPrint\Events\MoveToWishlistEvent;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerWishlistProduct\CustomerWishlistProductEntity;
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractAddWishlistProductRoute;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use KmPitchPrint\Services\ProductPitchPrintService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Swp\ProductOptionsSix\Core\Content\ProductOptions\SalesChannel\ProductOptionsAddToCart;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use KmPitchPrint\Controller\ProductOptionsSix;
use Shopware\Core\Checkout\Customer\Event\WishlistProductAddedEvent;
use Shopware\Core\Checkout\Customer\Exception\DuplicateWishlistProductException;
class MoveToWishlistSubscriber implements EventSubscriberInterface
{
/**
* @var AbstractAddWishlistProductRoute
*/
private AbstractAddWishlistProductRoute $addWishlistRoute;
/**
* @var $pitchPrintRepository SalesChannelRepository
*/
private $pitchPrintRepository;
/**
* @var $productPitchPrintService ProductPitchPrintService
*/
private $productPitchPrintService;
/**
* @var EntityRepository
*/
private $productRepository;
/**
* @var CartService
*/
private $cartService;
private $customerWishlistRepository;
/**
* @var ProductOptionsSix
*/
private $productOptionsSix;
/**
* @var string
*/
private $wishlistProductId;
public function __construct(
EntityRepository $pitchPrintRepository,
ProductPitchPrintService $productPitchPrintService,
EntityRepository $productRepository,
ProductOptionsAddToCart $productOptionsAddToCart,
CartService $cartService,
EntityRepository $customerWishlistRepository,
ProductOptionsSix $productOptionsSix,
AbstractAddWishlistProductRoute $addWishlistRoute
)
{
$this->pitchPrintRepository = $pitchPrintRepository;
$this->productPitchPrintService = $productPitchPrintService;
$this->productRepository = $productRepository;
$this->productOptionsAddToCart = $productOptionsAddToCart;
$this->cartService = $cartService;
$this->customerWishlistRepository = $customerWishlistRepository;
$this->productOptionsSix =$productOptionsSix;
$this->addWishlistRoute = $addWishlistRoute;
}
public static function getSubscribedEvents(): array
{
return [
MoveToWishlistEvent::class => 'moveToWishlist',
WishlistProductAddedEvent::class => 'setWishlistProductId'
];
}
public function setWishlistProductId(WishlistProductAddedEvent $event){
$this->wishlistProductId = $event->getWishlistId();
}
public function moveToWishlist(MoveToWishlistEvent $event)
{
$productId = $event->getProductId();
$quantity = $event->getQuantity();
$context = $event->getContext();
$request = $event->getRequest();
$requestDataBag = $event->getRequestDataBag();
$payload = $event->getPayload();
#dump($payload);exit;
#if(!$request->cookies->get('wishlist-enabled')){
# return ;
#}
$cookieId = $request->cookies->get('pp_id');
$caltulatedPrice = $this->productOptionsSix->addOption($requestDataBag, $event->getSalesChannelContext());
$caltulatedPrice = json_decode($caltulatedPrice->getContent())->lineItem->price->totalPrice;
$payload['price'] = $caltulatedPrice;
$payload['qty'] = $quantity;
$criteria = new Criteria();
$criteria->addAssociation('productoptions');
$criteria->addAssociations([
'productoptions.group',
'productoptions.option',
]);
$criteria->getAssociation('productoptions')->addSorting(new FieldSorting('option.position', FieldSorting::ASCENDING));
$criteria->addFilter(new EqualsFilter('id', $productId));
$product = $this->productRepository->search($criteria, $context)->first();
$productOptions = $product->getExtension('productoptions');
$projectId = Uuid::randomHex();
#$lineItem = null;
$ppId = null;
if ($request->get('lineItemId')){
$cart = $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
$cartItems = $cart->getLineItems();
$lineItemId = $request->get('lineItemId');
$lineItem = $cartItems->filter(function(LineItem $lineItem) use ($lineItemId){
return $lineItem->getId() == $lineItemId;
})->first();
$projectId = $lineItem->getId();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('projectId', $projectId));
$result = $this->pitchPrintRepository->search($criteria, $context)->first();
$ppId = $result->getId();
if(isset($result->getPayload()['previews'])){
$payload['previews'] = $result->getPayload()['previews'];
}
$payload['price'] = $lineItem->getPrice()->getTotalPrice();
}
$payload['type'] = 'product';
if($productOptions->count() > 0){
$swOptions = $request->get('productOptions');
$payload['swoptions'] = $swOptions;
$payload['type'] = 'product-with-options';
}
$insert = [
'productId' => $productId,
'cookieId' => $cookieId,
'projectId' => $projectId,
'payload' => serialize($payload),
];
if($ppId){
$insert['id'] = $ppId;
}
$customer = $event->getSalesChannelContext()->getCustomer();
if (!$customer){
$insert['guestWishlist'] = 1;
}
if (isset($product->getCustomFields()['pp_design_id__'])) {
$insert['designId'] = $product->getCustomFields()['pp_design_id__'];
}
if ($customer !== null && $request->cookies->get('pp_id')) {
try {
$this->addWishlistRoute->add($productId, $event->getSalesChannelContext(), $customer);
}catch (DuplicateWishlistProductException $exception){
}
$customerWishlistRepository = $this->customerWishlistRepository;
$criteria = new Criteria();
$criteria->addAssociation('products');
$criteria->addFilter(new EqualsFilter('customerId', $customer->getId()));
$wp = $customerWishlistRepository->search($criteria, $context);
$wishlistProducts = $wp->first()->getProducts();
$wishlistProduct = $wishlistProducts->filter(function (CustomerWishlistProductEntity $customerWishlistProductEntity) use ($productId) {
return $customerWishlistProductEntity->getProductId() == $productId;
})->first();
$insert['wishlistProductId'] = $wishlistProduct->getId();
}
return $this->pitchPrintRepository->upsert([$insert], $context);
}
}