src/Entity/Product/Product.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Product;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Entity\Trait\ReferenceableTrait;
  6. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableInterface;
  7. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetProductSubjectInterface;
  8. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetProductSubjectTrait;
  9. use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetSubjectInterface;
  10. use Dedi\SyliusSEOPlugin\Entity\SEOContent;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Loevgaard\SyliusBrandPlugin\Model\BrandAwareInterface;
  15. use Loevgaard\SyliusBrandPlugin\Model\ProductTrait as LoevgaardSyliusBrandPluginProductTrait;
  16. use Mezcalito\SyliusFileUploadPlugin\Model\FileInterface;
  17. use Mezcalito\SyliusFileUploadPlugin\Model\FilesAwareInterface;
  18. use Mezcalito\SyliusFileUploadPlugin\Model\FilesAwareTrait;
  19. use Setono\SyliusCalloutPlugin\Model\CalloutsAwareInterface;
  20. use Setono\SyliusCalloutPlugin\Model\CalloutsAwareTrait as SetonoSyliusCalloutCalloutsAwareTrait;
  21. use Sylius\Component\Core\Model\ChannelInterface;
  22. use Sylius\Component\Core\Model\CustomerInterface;
  23. use Sylius\Component\Core\Model\Product as BaseProduct;
  24. use Sylius\Component\Product\Model\ProductTranslationInterface;
  25. use Sylius\Component\Product\Model\ProductVariantInterface;
  26. use Tavy315\SyliusLabelsPlugin\Model\LabelsAwareInterface;
  27. use Tavy315\SyliusLabelsPlugin\Model\LabelsAwareTrait;
  28. /**
  29.  * @ORM\Entity
  30.  * @ORM\Table(name="sylius_product")
  31.  * @ApiResource()
  32.  */
  33. class Product extends BaseProduct implements
  34.     ProductInterface,
  35.     ReferenceableInterface,
  36.     RichSnippetProductSubjectInterface,
  37.     BrandAwareInterface,
  38.     CalloutsAwareInterface,
  39.     LabelsAwareInterface,
  40.     FilesAwareInterface
  41. {
  42.     use ReferenceableTrait {
  43.         getMetadataTitle as getBaseMetadataTitle;
  44.         getMetadataDescription as getBaseMetadataDescription;
  45.         getOpenGraphMetadataImage as getBaseOpenGraphMetadataImage;
  46.     }
  47.     use RichSnippetProductSubjectTrait;
  48.     use LoevgaardSyliusBrandPluginProductTrait;
  49.     use LabelsAwareTrait {
  50.         LabelsAwareTrait::__construct as private __labelsTraitConstruct;
  51.     }
  52.     use SetonoSyliusCalloutCalloutsAwareTrait {
  53.         SetonoSyliusCalloutCalloutsAwareTrait::__construct as private __calloutsTraitConstruct;
  54.     }
  55.     use FilesAwareTrait {
  56.         FilesAwareTrait::__construct as private initializeFilesCollection;
  57.     }
  58.     /**
  59.      * @ORM\Column(type="string", length=50, unique=true, nullable=true)
  60.      */
  61.     private $ean;
  62.     /**
  63.      * @ORM\Column(type="integer", nullable=true)
  64.      */
  65.     private $minQuantity 0;
  66.     /**
  67.      * @ORM\Column(type="integer", nullable=true)
  68.      */
  69.     private $minPrice 0;
  70.     /**
  71.      * @ORM\Column(type="integer", nullable=true)
  72.      */
  73.     private $maxPrice 0;
  74.     /**
  75.      * @ORM\Column(type="integer", nullable=true)
  76.      */
  77.     private $markup 0;
  78.     /**
  79.      * @ORM\Column(type="boolean", nullable=false)
  80.      */
  81.     private $onlyBreakpoints false;
  82.     /**
  83.      * @ORM\OneToMany(targetEntity="App\Entity\Product\ProductFileInterface", mappedBy="owner", orphanRemoval=true, cascade={"all"})
  84.      * @var Collection|FileInterface[]
  85.      */
  86.     protected $files;
  87.     /**
  88.      * @ORM\OneToMany(targetEntity="ProductVariant", mappedBy="product", orphanRemoval=true, cascade={"persist", "remove"})
  89.      * @var Collection|ProductVariantInterface[]
  90.      */
  91.     protected $variants;
  92.     public function __construct()
  93.     {
  94.         $this->__labelsTraitConstruct();
  95.         $this->__calloutsTraitConstruct();
  96.         $this->initializeFilesCollection();
  97.         parent::__construct();
  98.         $this->files = new ArrayCollection();
  99.     }
  100.     public function getMetadataTitle(): ?string
  101.     {
  102.         if (null === $this->getReferenceableContent()->getMetadataTitle()) {
  103.             $this->setCurrentLocale($this->getReferenceableContent()->getTranslation()->getLocale());
  104.             return null === $this->getMainTaxon() ? $this->getName() :
  105.                 $this->getMainTaxon()->getName() . ' | ' $this->getName();
  106.         }
  107.         return $this->getBaseMetadataTitle();
  108.     }
  109.     public function getMetadataDescription(): ?string
  110.     {
  111.         if (null === $this->getReferenceableContent()->getMetadataDescription()) {
  112.             $this->setCurrentLocale($this->getReferenceableContent()->getTranslation()->getLocale());
  113.             return $this->getShortDescription();
  114.         }
  115.         return $this->getBaseMetadataDescription();
  116.     }
  117.     public function getOpenGraphMetadataImage(): ?string
  118.     {
  119.         if (
  120.             null === $this->getReferenceableContent()->getOpenGraphMetadataImage() && $this->getImages() && $this->getImages()->first()
  121.         ) {
  122.             return $this->getImages()->first()->getPath();
  123.         }
  124.         return $this->getBaseOpenGraphMetadataImage();
  125.     }
  126.     protected function createReferenceableContent(): ReferenceableInterface
  127.     {
  128.         return new SEOContent();
  129.     }
  130.     public function getRichSnippetSubjectParent(): ?RichSnippetSubjectInterface
  131.     {
  132.         return $this->getMainTaxon();
  133.     }
  134.     public function getRichSnippetSubjectType(): string
  135.     {
  136.         return 'product';
  137.     }
  138.     public function getEan(): ?string
  139.     {
  140.         return $this->ean;
  141.     }
  142.     public function setEan(string $ean null): void
  143.     {
  144.         $this->ean $ean;
  145.     }
  146.     public function getMinQuantity(): ?int
  147.     {
  148.         return $this->minQuantity;
  149.     }
  150.     public function setMinQuantity(int $minQuantity null): void
  151.     {
  152.         $this->minQuantity $minQuantity;
  153.     }
  154.     public function getMinPrice(): ?int
  155.     {
  156.         return $this->minPrice;
  157.     }
  158.     public function setMinPrice(int $minPrice null): void
  159.     {
  160.         $this->minPrice $minPrice;
  161.     }
  162.     public function getMaxPrice(): ?int
  163.     {
  164.         return $this->maxPrice;
  165.     }
  166.     public function setMaxPrice(int $maxPrice null): void
  167.     {
  168.         $this->maxPrice $maxPrice;
  169.     }
  170.     public function getMarkup(): ?int
  171.     {
  172.         return $this->markup;
  173.     }
  174.     public function setMarkup(int $markup null): void
  175.     {
  176.         $this->markup $markup;
  177.     }
  178.     public function isOnlyBreakpoints(): bool
  179.     {
  180.         return $this->onlyBreakpoints;
  181.     }
  182.     public function setOnlyBreakpoints(bool $onlyBreakpoints): void
  183.     {
  184.         $this->onlyBreakpoints $onlyBreakpoints;
  185.     }
  186.     protected function createTranslation(): ProductTranslationInterface
  187.     {
  188.         return new ProductTranslation();
  189.     }
  190.     public function getFirstVariant()
  191.     {
  192.         return $this->isSimple() ? $this->variants[0] : null;
  193.     }
  194.     public function getMainImage()
  195.     {
  196.         foreach ($this->getImages() as $key => $value) {
  197.             if ($value->getType() === 'main') {
  198.                 return $value;
  199.             }
  200.         }
  201.         return null;
  202.     }
  203.     public function getVariantsBreakpoints(ChannelInterface $channel, ?CustomerInterface $customer null): array
  204.     {
  205.         $out = [];
  206.         foreach ($this->getEnabledVariants() as $productVariant) {
  207.             $tierPrices $productVariant->getTierPricesForChannel($channel$customer null);
  208.             $code $productVariant->getCode();
  209.             $ovs $productVariant->getOptionValues();
  210.             if (count($ovs) === 1) {
  211.                 foreach ($tierPrices as $tierPrice) {
  212.                     $qty $tierPrice->getQty();
  213.                     // vyhodime nulove(N/A) breakpointy
  214.                     if ($tierPrice->getPrice()) {
  215.                         $out[$ovs[0]->getCode()][$qty] = $qty;
  216.                         $out['all'][$qty] = $qty;
  217.                     }
  218.                 }
  219.             }
  220.         }
  221.         if (!empty($out['all']) && is_array($out['all'])) {
  222.             ksort($out['all']);
  223.         }
  224.         return $out;
  225.     }
  226.     public function variantsBreakpointsPricesForJs(ChannelInterface $channel, ?CustomerInterface $customer null): array
  227.     {
  228.         $out = [];
  229.         foreach ($this->getEnabledVariants() as $productVariant) {
  230.             $tierPrices $productVariant->getTierPricesForChannel($channel$customer null);
  231.             foreach ($tierPrices as $tierPrice) {
  232.                 $qty $tierPrice->getQty();
  233.                 $code $productVariant->getCode();
  234.                 $options = [];
  235.                 foreach ($productVariant->getOptionValues() as $ov) {
  236.                     // vsetky product options pre variants; ak zvolime vsetky x kombinacie options, potom je to tento variant
  237.                     $options[$ov->getOption()->getCode()] = [
  238.                         'id' => $ov->getId(),
  239.                         'code' => $ov->getCode()
  240.                     ];
  241.                 }
  242.                 $out[$code][$qty] = [
  243.                     'variant' => $code,
  244.                     'qty' => $qty,
  245.                     'price' => $tierPrice->getPrice(),
  246.                     'options' => $options
  247.                 ];
  248.                 if (is_array($out[$code])) {
  249.                     ksort($out[$code]);
  250.                 }
  251.             }
  252.         }
  253.         return $out;
  254.     }
  255.     public function getEnabledVariantsWith1Option(): Collection
  256.     {
  257.         return $this->variants->filter(
  258.             function (ProductVariantInterface $productVariant) {
  259.                 return $productVariant->isEnabled() && count($productVariant->getOptionValues()) === && $productVariant->getOptionValues()->first()->getOption()->getCode() === 'type';
  260.             }
  261.         );
  262.     }
  263.     public function calculateMinQty(): ?int
  264.     {
  265.         $min null;
  266.         foreach ($this->getEnabledVariants() as $productVariant) {
  267.             $tierPrices $productVariant->getTierPrices();
  268.             foreach ($tierPrices as $tierPrice) {
  269.                 if ($tierPrice->getQty() < $min || $min === null) {
  270.                     $min $tierPrice->getQty();
  271.                 }
  272.             }
  273.         }
  274.         return $min ?: 0;
  275.     }
  276.     public function getEnabledVariants(): Collection
  277.     {
  278.         $variants $this->variants->filter(
  279.             function (ProductVariantInterface $productVariant) {
  280.                 return $productVariant->isEnabled();
  281.             },
  282.         );
  283.         $variants = [...$variants];
  284.         foreach($variants as $key => $variant) {
  285.             if (count($variant->getOptionValues()) === && $variant->getOptionValues()[0]->getCode() === 'stock-plain') {
  286.                 unset($variants[$key]);
  287.                 $variants array_merge([$variant], $variants);
  288.                 break;
  289.             }
  290.         }
  291.         return new ArrayCollection($variants);
  292.     }
  293. }