vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/PricingManager/Condition/CatalogProduct.php line 70

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 Commercial License (PCL)
  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 PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Condition;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractProduct;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\ConditionInterface;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\EnvironmentInterface;
  19. use Pimcore\Model\DataObject\Concrete;
  20. class CatalogProduct extends AbstractObjectListCondition implements CatalogProductInterface
  21. {
  22.     /**
  23.      * @var AbstractProduct[]
  24.      */
  25.     protected $products = [];
  26.     /**
  27.      * Serialized product IDs
  28.      *
  29.      * @var array
  30.      */
  31.     protected $productIds = [];
  32.     /**
  33.      * @param EnvironmentInterface $environment
  34.      *
  35.      * @return bool
  36.      */
  37.     public function check(EnvironmentInterface $environment)
  38.     {
  39.         // init
  40.         $productsPool = [];
  41.         // get current product if we have one
  42.         if ($environment->getProduct()) {
  43.             $productsPool[] = $environment->getProduct();
  44.         }
  45.         // products from cart
  46.         if ($environment->getExecutionMode() === EnvironmentInterface::EXECUTION_MODE_CART && $environment->getCart()) {
  47.             foreach ($environment->getCart()->getItems() as $item) {
  48.                 $productsPool[] = $item->getProduct();
  49.             }
  50.         }
  51.         // test
  52.         foreach ($productsPool as $currentProduct) {
  53.             // check all valid products
  54.             foreach ($this->getProducts() as $product) {
  55.                 /** @var Concrete $currentProductCheck */
  56.                 $currentProductCheck $currentProduct;
  57.                 while ($currentProductCheck instanceof CheckoutableInterface) {
  58.                     if ($currentProductCheck->getId() === $product->getId()) {
  59.                         return true;
  60.                     }
  61.                     $currentProductCheck $currentProductCheck->getParent();
  62.                 }
  63.             }
  64.         }
  65.         return false;
  66.     }
  67.     /**
  68.      * @return string
  69.      */
  70.     public function toJSON()
  71.     {
  72.         // basic
  73.         $json = [
  74.             'type' => 'CatalogProduct',
  75.             'products' => [],
  76.         ];
  77.         // add categories
  78.         foreach ($this->getProducts() as $product) {
  79.             $json['products'][] = [
  80.                 $product->getId(),
  81.                 $product->getFullPath(),
  82.             ];
  83.         }
  84.         return json_encode($json);
  85.     }
  86.     /**
  87.      * @param string $string
  88.      *
  89.      * @return ConditionInterface
  90.      */
  91.     public function fromJSON($string)
  92.     {
  93.         $json json_decode($string);
  94.         $products = [];
  95.         foreach ($json->products as $cat) {
  96.             $product $this->loadObject($cat->id);
  97.             if ($product) {
  98.                 $products[] = $product;
  99.             }
  100.         }
  101.         $this->setProducts($products);
  102.         return $this;
  103.     }
  104.     /**
  105.      * Don't cache the entire product object
  106.      *
  107.      * @return array
  108.      *
  109.      * @internal
  110.      */
  111.     public function __sleep()
  112.     {
  113.         return $this->handleSleep('products''productIds');
  114.     }
  115.     /**
  116.      * Restore products from serialized ID list
  117.      */
  118.     public function __wakeup()
  119.     {
  120.         $this->handleWakeup('products''productIds');
  121.     }
  122.     /**
  123.      * @param AbstractProduct[] $products
  124.      *
  125.      * @return CatalogProductInterface
  126.      */
  127.     public function setProducts(array $products)
  128.     {
  129.         $this->products $products;
  130.         return $this;
  131.     }
  132.     /** @inheritDoc */
  133.     public function getProducts()
  134.     {
  135.         return $this->products;
  136.     }
  137. }