src/Entity/Product/Product.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Product;
  4. use App\Entity\Taxonomy\Taxon;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Sylius\Component\Core\Model\Product as BaseProduct;
  7. use Sylius\Component\Product\Model\ProductTranslationInterface;
  8. /**
  9.  * @ORM\Entity
  10.  * @ORM\Table(name="sylius_product")
  11.  */
  12. class Product extends BaseProduct
  13. {
  14.     /**
  15.      * @ORM\Column(type="datetime", nullable=true)
  16.      */
  17.     public $synced_at;
  18.     public $medias;
  19.     public function getMedias(){
  20.         return $this->medias;
  21.     }
  22.     public function getSku(): ?string{
  23.         return str_replace('_'' '$this->getCode());
  24.     }
  25.     public function setMedias($medias){
  26.         return $this->medias $medias;
  27.     }
  28.     public function getDescription(): ?string
  29.     {
  30.         $description parent::getDescription();
  31.         $description str_replace('<br/>'"\n"$description);
  32.         $description str_replace('<br>'"\n"$description);
  33.         return strip_tags($description);
  34.     }
  35.     /**
  36.      * @return ProductVariant|mixed
  37.      */
  38.     public function getVariant(){
  39.         $variants $this->getVariants();
  40.         return $variants[0]??false;
  41.     }
  42.     /**
  43.      * @return mixed
  44.      */
  45.     public function getSyncedAt():?\DateTimeInterface
  46.     {
  47.         return $this->synced_at;
  48.     }
  49.     /**
  50.      * @param mixed $synced_at
  51.      */
  52.     public function setSyncedAt(?\DateTimeInterface $synced_at): self
  53.     {
  54.         $this->synced_at $synced_at;
  55.         return $this;
  56.     }
  57.     protected function createTranslation(): ProductTranslationInterface
  58.     {
  59.         return new ProductTranslation();
  60.     }
  61.     public function getTitle(){
  62.         return $this->getName();
  63.     }
  64.     public function getMedia(){
  65.         return $this->getImages();
  66.     }
  67.     public function getCollections(){
  68.         /** @var Taxon[] $taxons */
  69.         $taxons $this->getTaxons();
  70.         $collections = [];
  71.         foreach ($taxons as $taxon){
  72.             if( $taxon->isEnabled() )
  73.                 $collections[] = $taxon;
  74.         }
  75.         usort($collections, function($a,$b){
  76.             $b_parent $b->getParent();
  77.             return $a->getId()-($b_parent?$b_parent->getId():0);
  78.         } );
  79.         return $collections;
  80.     }
  81.     public function getHas_compatibility(){
  82.         /** @var Taxon[] $taxons */
  83.         $taxons $this->getCollections();
  84.         foreach ($taxons as $taxon){
  85.             if( $taxon->getChildOf('vehicles') )
  86.                 return true;
  87.         }
  88.         return false;
  89.     }
  90.     public function getVehicles(){
  91.         /** @var Taxon[] $taxons */
  92.         $taxons $this->getCollections();
  93.         $vehicles = [];
  94.         foreach ($taxons as $taxon){
  95.             if( $taxon->getChildOf('vehicles') )
  96.                 $vehicles[] = $taxon;
  97.         }
  98.         return $vehicles;
  99.     }
  100.     public function getOptions_with_values(){
  101.         $options = [];
  102.         /** @var ProductOption[] $productOptions */
  103.         $productOptions $this->getOptions();
  104.         foreach ($productOptions as $index=>$productOption){
  105.             /** @var ProductOptionValue[] $productOptionsValues */
  106.             $productOptionsValues $productOption->getValues();
  107.             $values = [];
  108.             foreach ($productOptionsValues as $productOptionsValue){
  109.                 $values[] = $productOptionsValue->getCode();
  110.             }
  111.             $options[] = ['name'=>$productOption->getName(), 'code'=>$productOption->getCode(), 'position'=>$index+1'values'=>$values];
  112.         }
  113.         return $options;
  114.     }
  115.     public function getSelected_or_first_available_variant(){
  116.         return $this->getEnabledVariants()->first();
  117.     }
  118.     public function getAvailable(){
  119.         return count($this->getEnabledVariants())>0;
  120.     }
  121.     public function getUrl(){
  122.         return $this->getSlug();
  123.     }
  124.     public function getTags(){
  125.         if( $value $this->getAttribute('tags') )
  126.             return array_map('trim'explode(','$value));
  127.         return [];
  128.     }
  129.     public function getAttribute($code){
  130.         /** @var ProductAttributeValue[] $attributes */
  131.         $attributes $this->getAttributes();
  132.         foreach ($attributes as $attribute){
  133.             if( $attribute->getCode() == $code )
  134.                 return empty($attribute->getValue()) ? false $attribute->getValue();
  135.         }
  136.         return null;
  137.     }
  138.     public function getTotal_quantity(){
  139.         $stock 0;
  140.         /** @var ProductVariant[] $variants */
  141.         if( !$variants $this->getEnabledVariants() )
  142.             return $stock;
  143.         foreach ($variants as $variant)
  144.             $stock += $variant->getOnHand();
  145.         return $stock;
  146.     }
  147.     public function getImage($type=false){
  148.         /** @var ProductImage[] $images */
  149.         $images $this->getImages();
  150.         if( $type ){
  151.             foreach ($images as $image){
  152.                 if( $image->getType() == $type )
  153.                     return $image;
  154.             }
  155.             return false;
  156.         }
  157.         return $images[0]??false;
  158.     }
  159.     public function getFeatured_image(){
  160.         $images $this->getImagesByType('main');
  161.         if( !$images->count() )
  162.             $images $this->getImages();
  163.         if( $images->count() )
  164.             return $images->first()->getPath();
  165.         return false;
  166.     }
  167.     public function getPrice(){
  168.         /** @var ProductVariant[] $variants */
  169.         if( !$variants $this->getEnabledVariants() )
  170.             return 0;
  171.         $min false;
  172.         foreach ($variants as $variant){
  173.             $channelPricings $variant->getChannelPricings();
  174.             if( !$channelPricing $channelPricings->get('default') )
  175.                 return 0;
  176.             $price $channelPricing->getPrice();
  177.             if( $min === false ){
  178.                 $min $price;
  179.             }
  180.             else{
  181.                 $min min($min$price);
  182.             }
  183.         }
  184.         return $min;
  185.     }
  186.     public function getCompare_at_price(){
  187.         /** @var ProductVariant[] $variants */
  188.         if( !$variants $this->getEnabledVariants() )
  189.             return 0;
  190.         $min false;
  191.         foreach ($variants as $variant){
  192.             $channelPricings $variant->getChannelPricings();
  193.             if( !$channelPricing $channelPricings->get('default') )
  194.                 return 0;
  195.             $price $channelPricing->getOriginalPrice();
  196.             if( $min === false ){
  197.                 $min $price;
  198.             }
  199.             else{
  200.                 $min min($min$price);
  201.             }
  202.         }
  203.         return $min;
  204.     }
  205.     public function getRequire_quotation(){
  206.         /** @var Taxon[] $taxons */
  207.         $taxons $this->getTaxons();
  208.         foreach ($taxons as $taxon ){
  209.             if( $taxon->getQuotation() )
  210.                 return true;
  211.         }
  212.         return false;
  213.     }
  214.     public function getPrice_varies(){
  215.         /** @var ProductVariant[] $variants */
  216.         if( !$variants $this->getEnabledVariants() )
  217.             return 0;
  218.         $min $max false;
  219.         foreach ($variants as $variant){
  220.             $channelPricings $variant->getChannelPricings();
  221.             if( !$channelPricing $channelPricings->get('default') )
  222.                 return 0;
  223.             $price $channelPricing->getPrice();
  224.             if( $min === false ){
  225.                 $min $max $price;
  226.             }
  227.             else{
  228.                 $min min($min$price);
  229.                 $max max($max$price);
  230.             }
  231.         }
  232.         return $max != $min;
  233.     }
  234. }