<?php declare(strict_types=1);
/**
* Shopware
* Copyright © 2020
*
* @category Shopware
* @package SwpProductOptionsSix
* @subpackage ProductPageCriteriaSubscriber.php
*
* @copyright 2020 Iguana-Labs GmbH
* @author Module Factory <info at module-factory.com>
* @license https://www.module-factory.com/eula
*/
namespace Swp\CrosssellingInCartSix\Storefront\Subscriber;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Frontend implements EventSubscriberInterface
{
/** @var SystemConfigService */
private $systemConfigService;
/** @var CachedProductCrossSellingRoute */
private $crossSellingLoader;
/** @var CachedProductDetailRoute */
private $productLoader;
public function __construct(
SystemConfigService $systemConfigService,
AbstractProductCrossSellingRoute $crossSellingLoader,
AbstractProductDetailRoute $productLoader
) {
$this->systemConfigService = $systemConfigService;
$this->crossSellingLoader = $crossSellingLoader;
$this->productLoader = $productLoader;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'cartLoaded',
OffcanvasCartPageLoadedEvent::class => 'canvasCartLoaded'
];
}
public function canvasCartLoaded(OffcanvasCartPageLoadedEvent $event):void{
$this->generateCrossSelling($event);
}
public function cartLoaded(CheckoutCartPageLoadedEvent $event): void
{
$this->generateCrossSelling($event);
}
/**
* @param OffcanvasCartPageLoadedEvent|CheckoutCartPageLoadedEvent $event
* @return bool
*/
private function generateCrossSelling($event):bool {
/** @var array $pluginConfig */
$pluginConfig = $this->systemConfigService->get(
'SwpCrosssellingInCartSix.config',
$event->getSalesChannelContext()->getSalesChannel()->getId());
if ($pluginConfig['channelActive']!=1){
return false;
}
$cart = $event->getPage()->getCart()->getLineItems()->getElements();
$crossSellingProducts = array();
$limit =intval($pluginConfig['numberOfItems']);
$limitCount = 0;
$array_keys =array();
$randomCrossSell = array();
$criteria = new Criteria();
foreach ($cart as $key => $val) {
// check auf valid id
if (strlen($val->getId()) != 32) {
continue;
}
$crossSelling = $this->crossSellingLoader->load($val->getId(), $event->getRequest(), $event->getSalesChannelContext(), $criteria);
$crossSellingResult = $crossSelling->getResult();
if(!$crossSellingResult->count()) {
continue;
}
foreach ($crossSellingResult as $ikey => $ival) {
if ($ival->getCrossSelling()->getAssignedProducts() != null) {
foreach ($ival->getCrossSelling()->getAssignedProducts()->getElements() as $akey => $aval) {
/*
if ($limitCount < $limit) {
$product = $this->productLoader->load($aval->getProductId(), $event->getRequest(), $event->getSalesChannelContext(), $criteria);
$productData = $product->getProduct();
$crossSellingProducts[$productData->getId()] = $productData;
$limitCount++;
}
$array_keys[$aval->getProductId()]=$aval->getProductId();
*/
}
}
if ($ival->getProducts() != null) {
foreach ($ival->getProducts()->getElements() as $akey => $aval) {
if ($limitCount < $limit) {
$crossSellingProducts[$aval->getId()] = $aval;
$limitCount++;
}
$array_keys[$aval->getId()]=$aval->getId();
}
}
}
}
if(count($array_keys)>1 && $pluginConfig['RandomProducts'] == 1) {
if ($limit > count($array_keys)){
$limit = count($array_keys);
}
/** @var array $array_keys */
$array_keys = array_rand($array_keys, $limit);
foreach ($array_keys as $key => $val) {
try {
$product = $this->productLoader->load($val, $event->getRequest(), $event->getSalesChannelContext(), $criteria);
$productData = $product->getProduct();
$randomCrossSell[] = $productData;
} catch (\Exception $exception) {
}
}
$crossSellingProducts = $randomCrossSell;
}
if (count($crossSellingProducts) > 0) {
$event->getSalesChannelContext()->assign(['CrossSellingInCart' => $crossSellingProducts]);
}
return true;
}
// /**
// * @param SalesChannelContext $event
// * @return array|mixed|null
// */
// private function isCannelActive(SalesChannelContext $event)
// {
// return $this->systemConfigService->get(
// 'SwpCrosssellingInCartSix.config.channelActive',
// $event->getSalesChannel()->getId()
// );
// }
}