src/Entity/Product/ProductVariant.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Product;
  4. use App\Entity\Customer\CustomerInterface;
  5. use App\Entity\ProductOptionValue;
  6. use App\Entity\Trait\TierPriceableTrait;
  7. use Brille24\SyliusTierPricePlugin\Entity\ProductVariantInterface;
  8. use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;
  9. use Brille24\SyliusTierPricePlugin\Entity\TierPriceInterface;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Sylius\Component\Core\Model\ChannelInterface;
  12. use Sylius\Component\Product\Model\ProductVariantTranslationInterface;
  13. /**
  14.  * @ORM\Entity
  15.  * @ORM\Table(name="sylius_product_variant")
  16.  */
  17. class ProductVariant extends BaseProductVariant implements ProductVarinatInterfaceProductVariantInterface
  18. {
  19.     use TierPriceableTrait;
  20.     public function __construct()
  21.     {
  22.         parent::__construct();
  23.         $this->initTierPriceableTrait();
  24.     }
  25.     protected function createTranslation(): ProductVariantTranslationInterface
  26.     {
  27.         return new ProductVariantTranslation();
  28.     }
  29.     public function getMainImage()
  30.     {
  31.         foreach ($this->images as $key => $value) {
  32.             if ($value->getType === 'main') {
  33.                 return $value;
  34.             }
  35.         }
  36.         return null;
  37.     }
  38.     public function getTierPriceByQty(int $qty): ?TierPriceInterface
  39.     {
  40.         foreach ($this->getTierPrices() as $item) {
  41.             if ($item->getQty() === $qty) {
  42.                 return $item;
  43.             }
  44.         }
  45.         return null;
  46.     }
  47.     public function getMinQtyFromTierprice($channel): ?int
  48.     {
  49.         $min null;
  50.         foreach ($this->getTierPricesForChannel($channel) as $tierprice) {
  51.             if ($tierprice->getQty() < $min || $min === null) {
  52.                 $min $tierprice->getQty();
  53.             }
  54.         }
  55.         return $min;
  56.     }
  57.     public function getBreakpoints(ChannelInterface $channel, ?CustomerInterface $customer null): array
  58.     {
  59.         $out = [];
  60.         $tierPrices $this->getTierPricesForChannel($channel$customer null);
  61.         $ovs $this->getOptionValues();
  62. //        if (count($ovs) === 1) {
  63.             foreach ($tierPrices as $tierPrice) {
  64.                 $qty $tierPrice->getQty();
  65.                 $out[$qty] = $qty;
  66.             }
  67. //        }
  68.         if (is_array($out)) {
  69.             ksort($out);
  70.         }
  71.         return $out;
  72.     }
  73.     public function getOptionValueByCode(string $code)
  74.     {
  75.         foreach ($this->getOptionValues() as $optionValue) {
  76.             if ($optionValue->getCode() === $code) {
  77.                 return $optionValue;
  78.             }
  79.         }
  80.         return null;
  81.     }
  82.     public function getOptionValueByOptionCode(string $code)
  83.     {
  84.         foreach ($this->getOptionValues() as $optionValue) {
  85.             if ($optionValue->getOption()->getCode() === $code) {
  86.                 return $optionValue;
  87.             }
  88.         }
  89.         return null;
  90.     }
  91.     public function __toString(): string
  92.     {
  93.         return $this->name ?? 'Variant #' $this->getId(); // Provide a fallback
  94.     }
  95. }