src/Entity/Product/ProductVariant.php line 20

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\Trait\TierPriceableTrait;
  6. use Brille24\SyliusTierPricePlugin\Entity\ProductVariantInterface;
  7. use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;
  8. use Brille24\SyliusTierPricePlugin\Entity\TierPriceInterface;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Sylius\Component\Core\Model\ChannelInterface;
  11. use Sylius\Component\Product\Model\ProductVariantTranslationInterface;
  12. /**
  13.  * @ORM\Entity
  14.  * @ORM\Table(name="sylius_product_variant")
  15.  */
  16. class ProductVariant extends BaseProductVariant implements ProductVarinatInterfaceProductVariantInterface
  17. {
  18.     use TierPriceableTrait;
  19.     /**
  20.      * @ORM\Column(name="pack_units", type="integer", nullable=true)
  21.      */
  22.     private ?int $packUnits null;
  23.     /**
  24.      * @ORM\Column(name="pack_length", type="decimal", precision=10, scale=3, nullable=true)
  25.      */
  26.     private ?string $packLength null;
  27.     /**
  28.      * @ORM\Column(name="pack_width", type="decimal", precision=10, scale=3, nullable=true)
  29.      */
  30.     private ?string $packWidth null;
  31.     /**
  32.      * @ORM\Column(name="pack_height", type="decimal", precision=10, scale=3, nullable=true)
  33.      */
  34.     private ?string $packHeight null;
  35.     /**
  36.      * @ORM\Column(name="pack_weight", type="decimal", precision=10, scale=3, nullable=true)
  37.      */
  38.     private ?string $packWeight null;
  39.     public function __construct()
  40.     {
  41.         parent::__construct();
  42.         $this->initTierPriceableTrait();
  43.     }
  44.     protected function createTranslation(): ProductVariantTranslationInterface
  45.     {
  46.         return new ProductVariantTranslation();
  47.     }
  48.     public function getMainImage()
  49.     {
  50.         foreach ($this->images as $key => $value) {
  51.             if ($value->getType === 'main') {
  52.                 return $value;
  53.             }
  54.         }
  55.         return null;
  56.     }
  57.     public function getTierPriceByQty(int $qty): ?TierPriceInterface
  58.     {
  59.         foreach ($this->getTierPrices() as $item) {
  60.             if ($item->getQty() === $qty) {
  61.                 return $item;
  62.             }
  63.         }
  64.         return null;
  65.     }
  66.     public function getMinQtyFromTierprice($channel): ?int
  67.     {
  68.         $min null;
  69.         foreach ($this->getTierPricesForChannel($channel) as $tierprice) {
  70.             if ($tierprice->getQty() < $min || $min === null) {
  71.                 $min $tierprice->getQty();
  72.             }
  73.         }
  74.         return $min;
  75.     }
  76.     public function getBreakpoints(ChannelInterface $channel, ?CustomerInterface $customer null): array
  77.     {
  78.         $out = [];
  79.         $tierPrices $this->getTierPricesForChannel($channel$customer null);
  80.         $ovs $this->getOptionValues();
  81. //        if (count($ovs) === 1) {
  82.             foreach ($tierPrices as $tierPrice) {
  83.                 $qty $tierPrice->getQty();
  84.                 $out[$qty] = $qty;
  85.             }
  86. //        }
  87.         if (is_array($out)) {
  88.             ksort($out);
  89.         }
  90.         return $out;
  91.     }
  92.     public function getOptionValueByCode(string $code)
  93.     {
  94.         foreach ($this->getOptionValues() as $optionValue) {
  95.             if ($optionValue->getCode() === $code) {
  96.                 return $optionValue;
  97.             }
  98.         }
  99.         return null;
  100.     }
  101.     public function getOptionValueByOptionCode(string $code)
  102.     {
  103.         foreach ($this->getOptionValues() as $optionValue) {
  104.             if ($optionValue->getOption()->getCode() === $code) {
  105.                 return $optionValue;
  106.             }
  107.         }
  108.         return null;
  109.     }
  110.     public function __toString(): string
  111.     {
  112.         return $this->name ?? 'Variant #' $this->getId(); // Provide a fallback
  113.     }
  114.     public function getPackUnits(): ?int
  115.     {
  116.         return $this->packUnits;
  117.     }
  118.     public function setPackUnits(?int $packUnits): void
  119.     {
  120.         $this->packUnits $packUnits;
  121.     }
  122.     /** Dimensions in IN */
  123.     public function getPackLength(): ?float
  124.     {
  125.         return null === $this->packLength null : (float) $this->packLength;
  126.     }
  127.     public function setPackLength(?float $packLength): void
  128.     {
  129.         $this->packLength null === $packLength null number_format($packLength3'.''');
  130.     }
  131.     public function getPackWidth(): ?float
  132.     {
  133.         return null === $this->packWidth null : (float) $this->packWidth;
  134.     }
  135.     public function setPackWidth(?float $packWidth): void
  136.     {
  137.         $this->packWidth null === $packWidth null number_format($packWidth3'.''');
  138.     }
  139.     public function getPackHeight(): ?float
  140.     {
  141.         return null === $this->packHeight null : (float) $this->packHeight;
  142.     }
  143.     public function setPackHeight(?float $packHeight): void
  144.     {
  145.         $this->packHeight null === $packHeight null number_format($packHeight3'.''');
  146.     }
  147.     /** Weight in LBS */
  148.     public function getPackWeight(): ?float
  149.     {
  150.         return null === $this->packWeight null : (float) $this->packWeight;
  151.     }
  152.     public function setPackWeight(?float $packWeight): void
  153.     {
  154.         $this->packWeight null === $packWeight null number_format($packWeight3'.''');
  155.     }
  156. }