custom/plugins/fourtwosixPreOrderProduct/src/Subscriber/ProductDetails.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace fourtwosix\PreOrder\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEvents;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Shopware\Storefront\Page\Product;
  8. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  9. class ProductDetails implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var SystemConfigService
  13.      */
  14.     private $systemConfigService;
  15.     public function __construct(SystemConfigService $systemConfigService)
  16.     {
  17.         $this->systemConfigService $systemConfigService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return[
  22.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductDetails',
  23.             CheckoutCartPageLoadedEvent::class => 'onCartPage'
  24.         ];
  25.     }
  26.     public function onProductDetails(EntityLoadedEvent $event)
  27.     {
  28.         $buttonColor $this->systemConfigService->get('fourtwosixPreOrderProduct.config.preOrderButtonColor');
  29.         if ($buttonColor == null$buttonColor "";
  30.         $matches = array();
  31.         preg_match('/#([a-f0-9]{3}){1,2}\b/i'$buttonColor$matches);
  32.         if(count($matches) > 0){
  33.             $buttonColor $matches[0];
  34.             $entities $event->getEntities();
  35.             foreach( $entities as $entity ){
  36.                 $extensions $entity->getExtensions();
  37.                 $extensions['preorder_button_color'] = $buttonColor;
  38.                 $entity->setExtensions($extensions);
  39.             }
  40.         }
  41.     }
  42.     public function onCartPage(CheckoutCartPageLoadedEvent $event) {
  43.         /* add to lineItems extensions the release date indicator */
  44.         $cart_release_dates = [];
  45.         $lineItems $event->getPage()->getCart()->getLineItems();
  46.         foreach ($lineItems as $lineItem) {
  47.             foreach ($event->getPage()->getCart()->getData() as $originalProduct) {
  48.                 if(!$originalProduct instanceof \Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity) {
  49.                     continue;
  50.                 }
  51.                 if (!is_array($originalProduct) && get_class($originalProduct) == "Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity"
  52.                     && $lineItem->getId() == $originalProduct->getId()) {
  53.                     if (isset($originalProduct->getCustomFields()["preorder_enabled"]) && $originalProduct->getCustomFields()["preorder_enabled"])
  54.                         if ($originalProduct->getReleaseDate())
  55.                             $cart_release_dates[$lineItem->getId()] = $originalProduct->getReleaseDate()->format("Y-m-d H:i:s");
  56.                     break;
  57.                 }
  58.             }
  59.         }
  60.         $event->getPage()->setExtensions([
  61.             "fourtwosixPreorderCartReleaseDates" => $cart_release_dates
  62.         ]);
  63.     }
  64. }