<?php declare(strict_types=1);
namespace MasterFFL\Checkout\Subscriber;
use MasterFFL\Checkout\Service\FFLConfigService;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
{
private FFLConfigService $configService;
private EntityRepositoryInterface $productRepository;
public function __construct(
FFLConfigService $configService,
EntityRepositoryInterface $productRepository
) {
$this->configService = $configService;
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded'
];
}
public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
{
$page = $event->getPage();
$salesChannelContext = $event->getSalesChannelContext();
// Get FFL configuration
$fflConfig = $this->configService->getFFLConfig($salesChannelContext->getSalesChannel()->getId());
// Add FFL config to page extensions
$page->addExtension('fflConfig', new ArrayStruct($fflConfig));
// Check if cart has FFL products
$hasFFLProducts = $this->checkForFFLProducts($event->getPage()->getCart(), $event->getContext());
$page->addExtension('hasFFLProducts', new ArrayStruct(['value' => $hasFFLProducts]));
}
private function checkForFFLProducts($cart, $context): bool
{
if ($cart->getLineItems()->count() === 0) {
return false;
}
// Get all product IDs from cart
$productIds = [];
foreach ($cart->getLineItems() as $lineItem) {
if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
$productIds[] = $lineItem->getReferencedId();
}
}
if (empty($productIds)) {
return false;
}
try {
// Fetch products with custom fields
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('id', $productIds));
$products = $this->productRepository->search($criteria, $context);
// Check each product for FFL requirement
foreach ($products as $product) {
$customFields = $product->getCustomFields();
if ($this->isFFLProduct($customFields)) {
error_log('FFL Product found: ' . $product->getName() . ' - Custom Fields: ' . json_encode($customFields));
return true;
}
}
} catch (\Exception $e) {
error_log('Error checking FFL products: ' . $e->getMessage());
}
return false;
}
private function isFFLProduct(?array $customFields): bool
{
if (!$customFields) {
return false;
}
// Check for FFL attribute set to "yes" or true
$fflFields = [
'ffl_required',
'is_ffl_product',
'requires_ffl',
'ffl_product',
'ffl_attribute',
'ffl'
];
foreach ($fflFields as $fieldName) {
if (isset($customFields[$fieldName])) {
$value = $customFields[$fieldName];
// Check if value indicates "yes" for FFL requirement
if ($value === 'yes' || $value === 'Yes' || $value === 'YES') {
return true;
}
}
}
return false;
}
}