custom/plugins/MasterFFLCheckout/src/Storefront/Page/Checkout/CheckoutPageLoadedSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MasterFFL\Checkout\Storefront\Page\Checkout;
  3. use MasterFFL\Checkout\Service\FFLAttributeService;
  4. use MasterFFL\Checkout\Service\FFLProductService;
  5. use MasterFFL\Checkout\Service\FFLConfigService;
  6. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CheckoutPageLoadedSubscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         private readonly FFLAttributeService $fflAttributeService,
  17.         private readonly EntityRepository $productRepository,
  18.         private readonly FFLProductService $fflProductService,
  19.         private readonly SystemConfigService $systemConfigService,
  20.         private readonly FFLConfigService $fflConfigService // ADD THIS
  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.         $firearmTypeValue '';
  31.         $cart $event->getPage()->getCart();
  32.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  33.         $hasFFLProducts false;
  34.         $fflProducts = [];
  35.         // Get FFL category mappings
  36.         $fflCategoryIds $this->getFFLCategoryIds($salesChannelId);
  37.         
  38.         foreach ($cart->getLineItems() as $lineItem) {
  39.             if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  40.                 $productId $lineItem->getReferencedId();
  41.                 
  42.                 $criteria = new Criteria([$productId]);
  43.                 $criteria->addAssociation('categories');
  44.                 $product $this->productRepository->search($criteria$event->getContext())->first();
  45.                 
  46.                 if ($product instanceof ProductEntity) {
  47.                     $isFFLProduct false;
  48.                     $firearmType '';
  49.                     
  50.                     // Check custom fields first
  51.                     $customFields $product->getCustomFields();
  52.                     if (isset($customFields[$this->fflAttributeService->getFFLAttributeName()])) {
  53.                         $fflValue $customFields[$this->fflAttributeService->getFFLAttributeName()];
  54.                         
  55.                         if ($fflValue === 'yes') {
  56.                             $isFFLProduct true;
  57.                             
  58.                             if (isset($customFields[$this->fflAttributeService->getFirearmTypeAttributeName()])) {
  59.                                 $firearmTypeValue $customFields[$this->fflAttributeService->getFirearmTypeAttributeName()];
  60.                                 $firearmType $this->getFirearmTypeLabel($firearmTypeValue);
  61.                             }
  62.                         }
  63.                     }
  64.                     
  65.                     // Check if product is in FFL category
  66.                     if ($this->isProductInFFLCategory($product$fflCategoryIds)) {
  67.                         $fflValue $customFields[$this->fflAttributeService->getFFLAttributeName()] ?? "yes";
  68.                         // print_r($fflValue); die;
  69.                         if ($fflValue === 'no') {
  70.                             $isFFLProduct false;
  71.                         } else {
  72.                             $isFFLProduct true;
  73.                             $firearmType $this->getFirearmTypeFromCategory($product$salesChannelId);
  74.                         }
  75.                     }
  76.                     
  77.                     if ($isFFLProduct) {
  78.                         $hasFFLProducts true;
  79.                         
  80.                         $fflProducts[] = [
  81.                             'id' => $lineItem->getId(),
  82.                             'label' => $lineItem->getLabel(),
  83.                             'fflType' => $firearmType,
  84.                             'quantity' => $lineItem->getQuantity()
  85.                         ];
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.         // FIXED: Get FFL configuration and assign to page
  91.         $fflConfig $this->fflConfigService->getFFLConfig($salesChannelId);
  92.         $fflConfig['headerColor'] = $this->fflConfigService->getHeaderColor($salesChannelId);
  93.         $fflConfig['buttonColor'] = $this->fflConfigService->getButtonColor($salesChannelId);
  94.         $fflConfig['firearmType'] = $firearmTypeValue;
  95.         $event->getPage()->assign([
  96.             'hasFFLProducts' => $hasFFLProducts,
  97.             'fflProducts' => $fflProducts,
  98.             'fflProductCount' => count($fflProducts),
  99.             'fflConfig' => $fflConfig // ADD THIS
  100.         ]);
  101.         // FIXED: Also set as extensions for backward compatibility
  102.         $event->getPage()->addExtension('hasFFLProducts', new \Shopware\Core\Framework\Struct\ArrayStruct(['value' => $hasFFLProducts]));
  103.         $event->getPage()->addExtension('fflConfig', new \Shopware\Core\Framework\Struct\ArrayStruct($fflConfig));
  104.     }
  105.     
  106.     private function getFFLCategoryIds(string $salesChannelId): array
  107.     {
  108.         $configKey 'MasterFFLCheckout.config.categoryMappingTable';
  109.         $categoryMappings $this->systemConfigService->get($configKey$salesChannelId);
  110.         
  111.         if (!is_array($categoryMappings)) {
  112.             return [];
  113.         }
  114.         $fflCategoryIds = [];
  115.         foreach ($categoryMappings as $mapping) {
  116.             if (isset($mapping['storeCategory']) && !empty($mapping['storeCategory'])) {
  117.                 $fflCategoryIds[] = $mapping['storeCategory'];
  118.             }
  119.         }
  120.         return $fflCategoryIds;
  121.     }
  122.     
  123.     // private function isProductInFFLCategory(ProductEntity $product, array $fflCategoryIds): bool
  124.     // {
  125.     //     if (empty($fflCategoryIds)) {
  126.     //         return false;
  127.     //     }
  128.     //     $productCategories = $product->getCategories();
  129.     //     if (!$productCategories) {
  130.     //         return false;
  131.     //     }
  132.     //     echo"<pre>"; print_r($productCategories); die;
  133.     //     foreach ($productCategories as $category) {
  134.     //         if (in_array($category->getId(), $fflCategoryIds)) {
  135.     //             die("Category found: " . $category->getId());
  136.     //             return true;
  137.     //         }
  138.     //     }
  139.     //     return false;
  140.     // }
  141.     private function isProductInFFLCategory(ProductEntity $product, array $fflCategoryIds): bool
  142.     {
  143.         if (empty($fflCategoryIds)) {
  144.             return false;
  145.         }
  146.         $productCategories $product->getCategories();
  147.         if (!$productCategories) {
  148.             return false;
  149.         }
  150.         // Extract category IDs from the CategoryCollection
  151.         $productCategoryIds = [];
  152.         foreach ($productCategories as $category) {
  153.             $productCategoryIds[] = $category->getId();
  154.         }
  155.         // Check if any product category matches FFL categories
  156.         foreach ($productCategoryIds as $categoryId) {
  157.             if (in_array($categoryId$fflCategoryIds)) {
  158.                 error_log("FFL Category match found: " $categoryId);
  159.                 return true;
  160.             }
  161.         }
  162.         return false;
  163.     }
  164.     
  165.     private function getFirearmTypeFromCategory(ProductEntity $productstring $salesChannelId): string
  166.     {
  167.         $configKey 'MasterFFLCheckout.config.categoryMappingTable';
  168.         $categoryMappings $this->systemConfigService->get($configKey$salesChannelId);
  169.         
  170.         if (!is_array($categoryMappings)) {
  171.             return '';
  172.         }
  173.         $productCategories $product->getCategories();
  174.         if (!$productCategories) {
  175.             return '';
  176.         }
  177.         foreach ($productCategories as $category) {
  178.             foreach ($categoryMappings as $mapping) {
  179.                 if (isset($mapping['storeCategory']) && 
  180.                     $mapping['storeCategory'] === $category->getId() &&
  181.                     isset($mapping['fflMapping'])) {
  182.                     return $this->getFirearmTypeLabel($mapping['fflMapping']);
  183.                 }
  184.             }
  185.         }
  186.         return '';
  187.     }
  188.     
  189.     private function getFirearmTypeLabel(string $value): string
  190.     {
  191.         return match($value) {
  192.             'hand_gun' => 'Handgun',
  193.             'long_gun' => 'Long Gun',
  194.             'other_firearm' => 'Other Firearm',
  195.             'nfa' => 'NFA Item',
  196.             default => ''
  197.         };
  198.     }
  199. }