custom/plugins/MasterFFLCheckout/src/Subscriber/CheckoutConfirmPageSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MasterFFL\Checkout\Subscriber;
  3. use MasterFFL\Checkout\Service\FFLConfigService;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
  12. {
  13.     private FFLConfigService $configService;
  14.     private EntityRepositoryInterface $productRepository;
  15.     public function __construct(
  16.         FFLConfigService $configService,
  17.         EntityRepositoryInterface $productRepository
  18.     ) {
  19.         $this->configService $configService;
  20.         $this->productRepository $productRepository;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded'
  26.         ];
  27.     }
  28.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  29.     {
  30.         $page $event->getPage();
  31.         $salesChannelContext $event->getSalesChannelContext();
  32.         
  33.         // Get FFL configuration
  34.         $fflConfig $this->configService->getFFLConfig($salesChannelContext->getSalesChannel()->getId());
  35.         
  36.         // Add FFL config to page extensions
  37.         $page->addExtension('fflConfig', new ArrayStruct($fflConfig));
  38.         
  39.         // Check if cart has FFL products
  40.         $hasFFLProducts $this->checkForFFLProducts($event->getPage()->getCart(), $event->getContext());
  41.         $page->addExtension('hasFFLProducts', new ArrayStruct(['value' => $hasFFLProducts]));
  42.     }
  43.     
  44.     private function checkForFFLProducts($cart$context): bool
  45.     {
  46.         if ($cart->getLineItems()->count() === 0) {
  47.             return false;
  48.         }
  49.         // Get all product IDs from cart
  50.         $productIds = [];
  51.         foreach ($cart->getLineItems() as $lineItem) {
  52.             if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  53.                 $productIds[] = $lineItem->getReferencedId();
  54.             }
  55.         }
  56.         if (empty($productIds)) {
  57.             return false;
  58.         }
  59.         try {
  60.             // Fetch products with custom fields
  61.             $criteria = new Criteria();
  62.             $criteria->addFilter(new EqualsAnyFilter('id'$productIds));
  63.             
  64.             $products $this->productRepository->search($criteria$context);
  65.             // Check each product for FFL requirement
  66.             foreach ($products as $product) {
  67.                 $customFields $product->getCustomFields();
  68.                 
  69.                 if ($this->isFFLProduct($customFields)) {
  70.                     error_log('FFL Product found: ' $product->getName() . ' - Custom Fields: ' json_encode($customFields));
  71.                     return true;
  72.                 }
  73.             }
  74.         } catch (\Exception $e) {
  75.             error_log('Error checking FFL products: ' $e->getMessage());
  76.         }
  77.         return false;
  78.     }
  79.     private function isFFLProduct(?array $customFields): bool
  80.     {
  81.         if (!$customFields) {
  82.             return false;
  83.         }
  84.         // Check for FFL attribute set to "yes" or true
  85.         $fflFields = [
  86.             'ffl_required',
  87.             'is_ffl_product',
  88.             'requires_ffl',
  89.             'ffl_product',
  90.             'ffl_attribute',
  91.             'ffl'
  92.         ];
  93.         foreach ($fflFields as $fieldName) {
  94.             if (isset($customFields[$fieldName])) {
  95.                 $value $customFields[$fieldName];
  96.                 
  97.                 // Check if value indicates "yes" for FFL requirement
  98.                 if ($value === 'yes' || $value === 'Yes' || $value === 'YES') {
  99.                     return true;
  100.                 }
  101.             }
  102.         }
  103.         return false;
  104.     }
  105. }