src/Document/Areabrick/PersonalizedProductTeaser.php line 85

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Document\Areabrick;
  15. use App\Ecommerce\IndexService\SegmentGetter;
  16. use CustomerManagementFrameworkBundle\SegmentManager\SegmentManagerInterface;
  17. use CustomerManagementFrameworkBundle\Targeting\SegmentTracker;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\Exception\InvalidConfigException;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  20. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\DefaultMysql;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ElasticSearch\AbstractElasticSearch;
  22. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  23. use Pimcore\Model\Document\Editable\Area\Info;
  24. use Pimcore\Targeting\VisitorInfoStorage;
  25. class PersonalizedProductTeaser extends AbstractAreabrick
  26. {
  27.     /**
  28.      * @var VisitorInfoStorage
  29.      */
  30.     protected $visitorInfoStorage;
  31.     /**
  32.      * @var SegmentTracker
  33.      */
  34.     protected $segmentTracker;
  35.     /**
  36.      * @var SegmentManagerInterface
  37.      */
  38.     protected $segmentManager;
  39.     /**
  40.      * @var Factory
  41.      */
  42.     protected $ecommerceFactory;
  43.     /**
  44.      * PersonalizedProductTeaser constructor.
  45.      *
  46.      * @param VisitorInfoStorage $visitorInfoStorage
  47.      * @param SegmentTracker $segmentTracker
  48.      * @param SegmentManagerInterface $segmentManager
  49.      * @param Factory $ecommerceFactory
  50.      */
  51.     public function __construct(VisitorInfoStorage $visitorInfoStorageSegmentTracker $segmentTrackerSegmentManagerInterface $segmentManagerFactory $ecommerceFactory)
  52.     {
  53.         $this->visitorInfoStorage $visitorInfoStorage;
  54.         $this->segmentTracker $segmentTracker;
  55.         $this->segmentManager $segmentManager;
  56.         $this->ecommerceFactory $ecommerceFactory;
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function getName()
  62.     {
  63.         return 'Personalized Product Teaser';
  64.     }
  65.     public function action(Info $info)
  66.     {
  67.         $info->setParam('usePersonalizedData'false);
  68.         if (! $this->visitorInfoStorage->hasVisitorInfo()) {
  69.             return;
  70.         }
  71.         //get relevant segments for filtering
  72.         $allowedSegmentGroups = [SegmentGetter::SEGMENT_GROUP_CAR_CLASSSegmentGetter::SEGMENT_GROUP_MANUFACTURER];
  73.         $segmentCollection $this->segmentTracker->getFilteredAssignments($this->visitorInfoStorage->getVisitorInfo(), $allowedSegmentGroups2);
  74.         if (empty($segmentCollection)) {
  75.             return;
  76.         }
  77.         //build filter list
  78.         $productList $this->ecommerceFactory->getIndexService()->getProductListForCurrentTenant();
  79.         foreach ($segmentCollection as $group => $groupCollection) {
  80.             if (!empty($groupCollection)) {
  81.                 $values = [];
  82.                 foreach ($groupCollection as $item) {
  83.                     $values[] = intval($item['segment']->getId());
  84.                 }
  85.                 $this->addRelationCondition($productList$values);
  86.             }
  87.         }
  88.         $this->setRandOrderKey($productList);
  89.         $productList->setLimit(3);
  90.         $productList->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY);
  91.         if ($productList->count() >= 3) {
  92.             $info->setParam('productList'$productList);
  93.             $info->setParam('usePersonalizedData'true);
  94.         }
  95.     }
  96.     protected function addRelationCondition(ProductListInterface $productList$values)
  97.     {
  98.         if ($productList instanceof DefaultMysql) {
  99.             $productList->addRelationCondition('segments''dest IN (' implode(','$values) . ')');
  100.         } elseif ($productList instanceof AbstractElasticSearch) {
  101.             $productList->addRelationCondition('segments', ['terms' => ['relations.' 'segments' => $values]]);
  102.         } else {
  103.             throw new InvalidConfigException('Product List Type not supported');
  104.         }
  105.     }
  106.     protected function setRandOrderKey(ProductListInterface $productList)
  107.     {
  108.         if ($productList instanceof DefaultMysql) {
  109.             $productList->setOrderKey('RAND()');
  110.         } elseif ($productList instanceof AbstractElasticSearch) {
  111.             //not possible
  112.         } else {
  113.             throw new InvalidConfigException('Product List Type not supported');
  114.         }
  115.     }
  116. }