<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\Model\Product\TraitClasses;
use Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\AvailabilityInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\AvailabilitySystemInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use App\Tool\PimcoreUrl;
trait Checkoutable
{
/**
* @return mixed
*/
public function getOSName() : ?string
{
return $this->getName();
}
/**
* @param int $quantityScale
*
* @return \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInterface
*/
public function getOSPrice($quantityScale = 1) : ?\Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInterface
{
return $this->getOSPriceInfo($quantityScale)->getPrice();
}
/**
* @return string
*/
public function getPriceSystemName() : string
{
return 'default';
}
/**
* @return \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceSystemInterfac
*/
public function getPriceSystemImplementation() : \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceSystemInterface
{
return Factory::getInstance()->getPriceSystem($this->getPriceSystemName());
}
/**
* @param int $quantityScale
*
* @return \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInfoInterface
*/
public function getOSPriceInfo($quantityScale = 1) : \Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInfoInterface
{
return $this->getPriceSystemImplementation()->getPriceInfo($this, $quantityScale, null);
}
/**
* @return string
*/
public function getOSProductNumber() : ?string
{
return strval($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();
}
/**
* returns instance of availability system implementation based on result of getAvailabilitySystemName()
*
* @return AvailabilitySystemInterface|null
*/
public function getAvailabilitySystemImplementation(): ?AvailabilitySystemInterface
{
return Factory::getInstance()->getAvailabilitySystem($this->getAvailabilitySystemName());
}
/**
* returns availability info based on given quantity
*
* @param int $quantity
*
* @return AvailabilityInterface
*/
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) : string
{
// add id
if (!array_key_exists('seofriendly', $params)) {
$params['seofriendly'] = $this->getUrlseofriendly();
}
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['document']) {
$params['prefix'] = substr($params['document']->getFullPath(), 1);
} else {
$language = \Pimcore::getContainer()->get('request_stack')->getCurrentRequest()->getLocale();
$params['prefix'] = substr($language, 0, 2) . '/productos';
}
}
// add name
if (!array_key_exists('name', $params) && !array_key_exists('category', $params)) {
// add category path
$category = $this->getFirstCategory();
if ($category) {
$root = ($params['rootCategory']) ?? null;
$path = $category->getNavigationPath($root, $params['document']);
$params['category'] = $category->getId();
$params['name'] = $path;
}
// add name
// $name = \Pimcore\File::getValidFilename($this->getUrlseofriendly());
}
//$params['product'] = $this->getUrlseofriendly() ; // preg_replace('#-{2,}#', '-', $name);
if (isset($params['rootCategory'])) {
unset($params['rootCategory']);
}
if (isset($params['document'])) {
unset($params['document']);
}
// create url
$urlHelper = \Pimcore::getContainer()->get('App\Tool\PimcoreUrl');
$pimcoreUrl = $urlHelper->getPimcoreUrl();
return str_replace('%prefix' , $params['prefix'], $pimcoreUrl->__invoke($params, $route, $reset));
}
/**
* @param string $thumbnail thumbnail name configured in the Pimcore admin
*
* @return string|null
*/
public function getFirstImage($thumbnail) : ?string
{
$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;
}
}