<?php
namespace App\Model\DataObject\Product\TraitClasses;
use Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\IAvailability;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\AvailabilityInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\AvailabilitySystemInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInfoInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceSystemInterface;
use Pimcore\Twig\Extension\Templating\PimcoreUrl;
trait Checkoutable
{
// protected $pimcoreUrl;
// public function __construct(PimcoreUrl $pimcoreUrl){
// $this->pimcoreUrl = $pimcoreUrl;
// }
/**
* @return mixed
*/
public function getOSName():?string
{
return $this->getName();
}
/**
* @param int $quantityScale
*
* @return \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\IPrice
*/
public function getOSPrice($quantityScale = 1):?PriceInterface
{
return $this->getOSPriceInfo($quantityScale)->getPrice();
}
/**
* @return string
*/
public function getPriceSystemName(): ?string
{
return 'default';
}
/**
* @return \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\IPriceSystem
*/
public function getPriceSystemImplementation(): ?PriceSystemInterface
{
return Factory::getInstance()->getPriceSystem($this->getPriceSystemName());
}
/**
* @param int $quantityScale
*
* @return \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\IPriceInfo
*/
public function getOSPriceInfo($quantityScale = 1): ?PriceInfoInterface
{
return $this->getPriceSystemImplementation()->getPriceInfo($this, $quantityScale, null);
}
/**
* @return int
*/
public function getOSProductNumber(): ?string
{
return $this->getId();
}
/**
* @return string
*/
public function getAvailabilitySystemName(): ?string
{
return 'default';
}
/**
* @param int $quantityScale
*
* @return bool
*/
public function getOSIsBookable($quantityScale = 1): bool
{
$price = $this->getOSPrice($quantityScale);
return !empty($price) && $this->isActive();
}
/**
* @return \Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\IAvailabilitySystem
*/
public function getAvailabilitySystemImplementation(): ?AvailabilitySystemInterface
{
return Factory::getInstance()->getAvailabilitySystem($this->getAvailabilitySystemName());
}
/**
* returns availability info based on given quantity
*
* @param int $quantity
*
* @return IAvailability
*/
public function getOSAvailabilityInfo($quantity = null): ?AvailabilityInterface
{
return $this->getAvailabilitySystemImplementation()->getAvailabilityInfo($this, $quantity);
}
/**
* @param array $params
* @param string $route
* @param bool|true $reset
*
* @return string
*/
public function getDetailUrl(array $params = [], $route = 'shop-detail', $reset = true)
{
$preview = (isset($_GET['pimcore_preview'])) ? $_GET['pimcore_preview'] : null;
$editmode = (isset($_GET['pimcore_editmode'])) ? $_GET['pimcore_editmode'] : null;
if(!isset($params['rootCategory'])){
$params['rootCategory'] = null;
}
if(!isset($params['document'])){
$params['document'] = null;
}
if(isset($preview)){
$prevflag = $preview;
}else{
$prevflag = false;
}
if(isset($editmode)){
$editflag = $editmode;
}else{
$editflag = false;
}
// add id
if (!array_key_exists('product', $params)) {
$params['product'] = $this->getId();
}
//add prefix / by default language/shop
if (!array_key_exists('prefix', $params)) {
if (isset($params["document"])) {
$params['prefix'] = substr($params['document']->getFullPath(), 1);
} else {
$params['prefix'] = strtolower(\Pimcore::getContainer()->get('pimcore.locale')->getLocale());
}
}
if (!array_key_exists('name', $params)) {
$params['name'] = '';
//if (!array_key_exists('name', $params) && isset($params["rootCategory"])) {
// add category path
$category = $this->getFirstCategory();
if ($category) {
$path = $category->getNavigationPath($params['rootCategory'], $params['document']);
$params['name'] = $path;
}
// add name
$name = \Pimcore\File::getValidFilename($this->getOSName());
$params['name'] .= preg_replace('#-{2,}#', '-', $name);
}
unset($params['rootCategory']);
unset($params['document']);
// create url
//$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.engine.twig');
$container = \Pimcore::getKernel()->getContainer();
return ($prevflag || $editflag)?'#':$container->get('router')->generate($route, $params, $reset);
}
/**
* @param string $thumbnail thumbnail name configured in the Pimcore admin
*
* @return string|null
*/
public function getFirstImage($thumbnail)
{
$firstImageAsset = $this->getFirstImageAsset();
if ($firstImageAsset) {
return $firstImageAsset->getThumbnail($thumbnail);
}
return null;
}
/**
* @param $view
*
* @return array
*/
public function getHeroAttributes($view)
{
$heroAttributes = [];
if ($this->getArtno()) {
$heroAttributes[] = '<strong>' . $view->translate('shop.sku') . ':</strong> ' . $this->getArtno();
}
if ($this->getEan()) {
$heroAttributes[] = '<strong>' . $view->translate('shop.ean') . ':</strong> ' . $this->getEan();
}
if ($this->getSize() && $this->getSize() != ' ') {
$heroAttributes[] = '<strong>' . $view->translate('shop.size') . ':</strong> ' . $this->getSize();
}
if ($this->getColor()) {
$colors = $this->getColor();
$translatedColors = [];
foreach ($colors as $c) {
$translatedColors[] = $view->translate('optionvalue.' . $c);
}
$heroAttributes[] = '<strong>' . $view->translate('shop.color') . ':</strong> ' . implode(', ', $translatedColors);
}
return $heroAttributes;
}
}