src/Model/Product.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Model;
  3. use App\Helpers\PageObjectRelationHelper;
  4. use Pimcore\Model\DataObject\Data\Hotspotimage;
  5. class Product extends \Pimcore\Model\DataObject\Product
  6. {
  7.     public function page()
  8.     {
  9.         return (new PageObjectRelationHelper())->getProductPageFromProductObject($this);
  10.     }
  11.     /**
  12.      * @return false|string
  13.      *
  14.      * Returns the thumbnail of the product. We use this instead of the direct .items twig function so we can
  15.      * centrally define what image will be used as a thumbnail and what fallback rules are used.
  16.      */
  17.     public function renderThumbnail($thumbnail "default"$options = [])
  18.     {
  19.         if (isset($this->images) && count($this->images->getItems())) {
  20.             /** @var Hotspotimage $image */
  21.             if ($image $this->images->getItems()[0] ?? null) {
  22.                 return $image->getImage()->getThumbnail($thumbnail)->getHtml($options) ?? null;
  23.             }
  24.         }
  25.         return false;
  26.     }
  27.     public function getDefaultFeaturedImage(): ?\Pimcore\Model\Asset\Image
  28.     {
  29.         return $this->getFeaturedImage() ?: null;
  30.     }
  31.     public function getFeaturedImage()
  32.     {
  33.         if (isset($this->images) && count($this->images->getItems())) {
  34.             /** @var Hotspotimage $image */
  35.             if ($image $this->images->getItems()[0] ?? null) {
  36.                 return $image->getImage() ?? null;
  37.             }
  38.         }
  39.         return false;
  40.     }
  41.     public function getCategory()
  42.     {
  43.         // if this product has a parent that is of the class productCategory then return that productCategory
  44.         // if in the future a product can be a child of another product keep traversing recursively until we find a productCategory
  45.         $category = function($object) use(&$category){
  46.             if ($object->getParent() instanceof \Pimcore\Model\DataObject\ProductCategory) {
  47.                 return $object->getParent();
  48.             }
  49.             if ($object->getParent() instanceof \Pimcore\Model\DataObject\Product) {
  50.                 return $category($object->getParent());
  51.             }
  52.             return null;
  53.         };
  54.         return $category($this);
  55.     }
  56. }