vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/PricingManager/Action/Gift.php line 121

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\Action;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractProduct;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\ActionInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\EnvironmentInterface;
  18. class Gift implements GiftInterface
  19. {
  20.     /**
  21.      * @var AbstractProduct|null
  22.      */
  23.     protected ?AbstractProduct $product null;
  24.     /**
  25.      * @var string
  26.      */
  27.     protected string $productPath '';
  28.     /**
  29.      * @param EnvironmentInterface $environment
  30.      *
  31.      * @return GiftInterface
  32.      */
  33.     public function executeOnCart(EnvironmentInterface $environment)
  34.     {
  35.         $comment $environment->getRule()->getDescription();
  36.         $environment->getCart()->addGiftItem($this->getProduct(), 1nulltrue, [], [], $comment);
  37.         return $this;
  38.     }
  39.     /**
  40.      * set gift product
  41.      *
  42.      * @param AbstractProduct $product
  43.      *
  44.      * @return GiftInterface
  45.      */
  46.     public function setProduct(AbstractProduct $product)
  47.     {
  48.         $this->product $product;
  49.         return $this;
  50.     }
  51.     /**
  52.      * @return AbstractProduct|null
  53.      */
  54.     public function getProduct()
  55.     {
  56.         return $this->product;
  57.     }
  58.     /**
  59.      * @return string
  60.      */
  61.     public function toJSON()
  62.     {
  63.         return json_encode([
  64.             'type' => 'Gift',
  65.             'product' => $this->getProduct() ? $this->getProduct()->getFullPath() : null,
  66.         ]);
  67.     }
  68.     /**
  69.      * @param string $string
  70.      *
  71.      * @return ActionInterface
  72.      */
  73.     public function fromJSON($string)
  74.     {
  75.         $json json_decode($string);
  76.         $product AbstractProduct::getByPath($json->product);
  77.         if ($product) {
  78.             $this->setProduct($product);
  79.         }
  80.         return $this;
  81.     }
  82.     /**
  83.      * dont cache the entire product object
  84.      *
  85.      * @return array
  86.      *
  87.      * @internal
  88.      */
  89.     public function __sleep()
  90.     {
  91.         if (is_object($this->product)) {
  92.             $this->productPath $this->product->getFullPath();
  93.         }
  94.         return ['productPath'];
  95.     }
  96.     /**
  97.      * restore product
  98.      *
  99.      * @internal
  100.      */
  101.     public function __wakeup()
  102.     {
  103.         if ($this->productPath !== '') {
  104.             $this->product AbstractProduct::getByPath($this->productPath);
  105.         }
  106.     }
  107. }