<?php
namespace App\Model;
use App\Helpers\PageObjectRelationHelper;
use Pimcore\Model\DataObject\Data\Hotspotimage;
class Product extends \Pimcore\Model\DataObject\Product
{
public function page()
{
return (new PageObjectRelationHelper())->getProductPageFromProductObject($this);
}
/**
* @return false|string
*
* Returns the thumbnail of the product. We use this instead of the direct .items twig function so we can
* centrally define what image will be used as a thumbnail and what fallback rules are used.
*/
public function renderThumbnail($thumbnail = "default", $options = [])
{
if (isset($this->images) && count($this->images->getItems())) {
/** @var Hotspotimage $image */
if ($image = $this->images->getItems()[0] ?? null) {
return $image->getImage()->getThumbnail($thumbnail)->getHtml($options) ?? null;
}
}
return false;
}
public function getDefaultFeaturedImage(): ?\Pimcore\Model\Asset\Image
{
return $this->getFeaturedImage() ?: null;
}
public function getFeaturedImage()
{
if (isset($this->images) && count($this->images->getItems())) {
/** @var Hotspotimage $image */
if ($image = $this->images->getItems()[0] ?? null) {
return $image->getImage() ?? null;
}
}
return false;
}
public function getCategory()
{
// if this product has a parent that is of the class productCategory then return that productCategory
// if in the future a product can be a child of another product keep traversing recursively until we find a productCategory
$category = function($object) use(&$category){
if ($object->getParent() instanceof \Pimcore\Model\DataObject\ProductCategory) {
return $object->getParent();
}
if ($object->getParent() instanceof \Pimcore\Model\DataObject\Product) {
return $category($object->getParent());
}
return null;
};
return $category($this);
}
}