<?php declare(strict_types=1);
namespace Proc\ProcLivePrice\Subscriber;
use Proc\ProcOrderSimulate\Subscriber\OrderSimulateSubscriber;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CheapestPriceContainer;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\Price\Struct\ListPrice;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CalculatedListingPrice;
use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CalculatedCheapestPrice;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Swag\EnterpriseSearch\Search\Page\SearchPageLoadedEvent;
use Shopware\Core\Framework\Event\NestedEvent;
use Proc\ProcLivePrice\Helper\BuildRequestHelper;
use Proc\ProcFoundation\Helper\ErrorHandler;
use Proc\ProcFoundation\Helper\RequestHandler;
use Proc\ProcFoundation\Service\ProcFoundationService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
/**
* Class LivePriceSubscriber
* @package Proc\ProcLivePrice\Subscriber
*/
class LivePriceSubscriber implements EventSubscriberInterface
{
private const PATH = '/iman/live-price';
private const CUSTOM_ZLOTY = 'PLN';
/**
* @var RequestHandler
*/
private $requestHandler;
/**
* @var ErrorHandler
*/
private $errorHandler;
/**
* @var string
*/
private
$host,
$port,
$customerNumber;
private $error = null;
/**
* @var ProcFoundationService
*/
private $foundationService;
/**
* @var EntityRepositoryInterface
*/
private $productRepository;
/**
* @var ProductPageLoadedEvent | ProductListingResultEvent | SearchPageLoadedEvent
*/
private $event;
/**
* @var array
*/
private $result;
/**
* @var CalculatedTaxCollection
*/
private $calculatedTaxCollection;
/**
* @var TaxRuleCollection
*/
private $taxRuleCollection;
/**
* @var RouterInterface
*/
private RouterInterface $router;
private AbstractProductPriceCalculator $calculator;
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
// ProductPageLoadedEvent::class => ['onProductPageLoaded', 1],
// ProductListingResultEvent::class => ['onListingResultLoaded', 1],
'sales_channel.' . ProductEvents::PRODUCT_LOADED_EVENT => ['onSalesChanneLoaded', 10000],
// SearchPageLoadedEvent::class => ['onListingResultLoaded']
];
}
/**
* LivePriceSubscriber constructor.
* @param ProcFoundationService $foundationService
* @param RequestHandler $requestHandler
* @param ErrorHandler $errorHandler
* @param EntityRepositoryInterface $repositoryInterface
*/
function __construct(
ProcFoundationService $foundationService,
RequestHandler $requestHandler,
ErrorHandler $errorHandler,
EntityRepositoryInterface $repositoryInterface,
RouterInterface $router,
AbstractProductPriceCalculator $calculator,
) {
$this->foundationService = $foundationService;
$this->requestHandler = $requestHandler;
$this->errorHandler = $errorHandler;
$this->productRepository = $repositoryInterface;
$this->router = $router;
$this->calculator = $calculator;
}
/**
* @param NestedEvent $event
* @throws \Exception
*/
public function onListingResultLoaded(NestedEvent $event)
{
$this->event = $event;
/**
* Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
*/
if (!($this->customerNumber = $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
get_class($this)))) {
return;
}
/**
* Prüfen ob der Benutzer eine Artikel-Whitelist hat.
*/
$customFields = $this->event->getSalesChannelContext()->getCustomer()->getCustomFields();
if (array_key_exists('custom_product_listing_numbers', $customFields) &&
$customFields['custom_product_listing_numbers'] != null &&
$customFields['custom_product_listing_numbers'] != 0 &&
$customFields['custom_product_listing_numbers'] != '') {
$allowedArticles = explode(',', $customFields['custom_product_listing_numbers']);
} else {
return;
}
/**
* Configeinstellungen prüfen und setzen.
*/
if (!$this->checkConfig()) {
return;
}
$productArray = array();
if ($this->event instanceof ProductListingResultEvent) {
$resultItems = $this->event->getResult()->getIterator();
} else {
$resultItems = $this->event->getPage()->getListing()->getIterator();
}
$articles = [];
$counter = 0;
while ($resultItems->current() !== null) {
/**
* @var SalesChannelProductEntity $product
*/
$product = $resultItems->current();
// benutzer holen. -> custom field holen.(siehe oben) String in array umwandeln. Prüfen ob artikelnummer vorhanden. Wenn ja dann durchlaufen, wenn nein, continue.
if (!in_array($product->getProductNumber(), $allowedArticles)) {
$resultItems->next();
continue;
}
$counter++;
if ($product->getParentId()) {
/**
* @var ProductEntity $parentProduct
*/
$parentProduct = $this->getProduct($product->getParentId(), ['children']);
$childArticles = array();
foreach ($parentProduct->getChildren()->getElements() as $children) {
$childArticle['position-id'] = $counter;
$childArticle['article-number'] = $children->getProductNumber();
$childArticle['quantity'] = 1;
$childArticle['quantity-unit'] = $children->getPackUnit();
$childArticles[] = $childArticle;
}
$childResult = $this->getArticleInfo($childArticles);
if ($childResult['head']['status'] == 'NOK') {
return;
}
$countProductItems = sizeof($childResult);
$from = 0;
$to = 0;
$x = 0;
foreach ($childResult['articles']['article'] as $childArticle) {
// $unitPrice = (float)$childArticle['conditions']['condition'][0]['condition-value'];
// $mwstValue = (float)$childArticle['conditions']['condition'][2]['condition-value'];
// $unitPrice = $unitPrice + $mwstValue;
$unitPrice = (float)$childArticle['price'] / (float)$childArticle['quantity'];
if ($x === 0) {
$from = $unitPrice;
$to = $unitPrice;
} elseif ($unitPrice < $from) {
$from = $unitPrice;
} elseif ($unitPrice > $to) {
$to = $unitPrice;
}
$x++;
}
/**
* @var CalculatedCheapestPrice $productListingPrice
*/
$productListingPrice = $product->getCalculatedCheapestPrice();
/**
* @var CalculatedPrice $fromPrice , $toPrice
*/
$fromPrice = new CalculatedPrice(
$from,
$from,
$productListingPrice->getCalculatedTaxes(),
$productListingPrice->getTaxRules(),
1);
$toPrice = new CalculatedPrice(
$to,
$to,
$productListingPrice->getCalculatedTaxes(),
$productListingPrice->getTaxRules(),
1);
/**
* @var CalculatedCheapestPrice $newListingPrice
*/
$newListingPrice = new CalculatedCheapestPrice($from, $to, $productListingPrice->getCalculatedTaxes(),
$productListingPrice->getTaxRules());
$product->setCalculatedCheapestPrice($newListingPrice);
$product->getCheapestPrice()->getPrice()->first()->setGross($to);
$product->getCheapestPrice()->getPrice()->first()->setNet($to);
}
$productArray[] = $product;
$article['position-id'] = $counter;
$article['article-number'] = $product->getProductNumber();
$article['quantity'] = 1;
$article['quantity-unit'] = $product->getPackUnit();
$articles[] = $article;
$resultItems->next();
}
if (count($articles) <= 0) {
return;
}
$this->result = $this->getArticleInfo($articles);
if ($this->result['head']['status'] == 'NOK') {
return;
}
$countProductItems = sizeof($productArray);
$x = 0;
foreach ($productArray as $productItem) {
if ($countProductItems > 1) {
// $unitPrice = (float)$this->result['articles']['article'][$x]['conditions']['condition'][0]['condition-value'];
$totalPrice = (float)$this->result['articles']['article'][$x]['price'];
$unitPrice = (float)$totalPrice / (float)$this->result['articles']['article'][$x]['quantity'];
$mwstRate = 0.0; // (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-rate'];
$mwstValue = 0.0; // (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-value'];
} else {
// $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
$totalPrice = (float)$this->result['articles']['article']['price'];
$unitPrice = (float)$totalPrice / (float)$this->result['articles']['article']['quantity'];
$mwstRate = 0.0; // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
$mwstValue = 0.0; // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
}
/**
* @var CalculatedPrice $price
*/
$price = $product->getCalculatedPrice();
/**
* @var TaxRule $newTaxRule
*/
$newTaxRule = new TaxRule($mwstRate);
/**
* @var TaxRuleCollection $newTaxRuleCollection
*/
$newTaxRuleCollection = new TaxRuleCollection();
$newTaxRuleCollection->add($newTaxRule);
/**
* @var CalculatedTax $newCalculateTax
*/
$newCalculateTax = new CalculatedTax($mwstValue, $mwstRate, $totalPrice);
/**
* @var CalculatedTaxCollection $newCalculatedTaxCollection
*/
$newCalculatedTaxCollection = new CalculatedTaxCollection();
$newCalculatedTaxCollection->add($newCalculateTax);
/**
* @var CalculatedPrice $newPrice
*/
$newPrice = new CalculatedPrice(
($unitPrice + $mwstValue),
$totalPrice,
$newCalculatedTaxCollection,
$newTaxRuleCollection,
$price->getQuantity(),
$price->getReferencePrice(),
$price->getListPrice()
);
$productItem->setCalculatedPrice($newPrice);
$x++;
}
}
/**
* @param NestedEvent $event
* @throws \Exception
*/
public function onSalesChanneLoaded(SalesChannelEntityLoadedEvent $event)
{
if($event->getContext()->hasState('noLivePrice')){
return;
}
$this->event = $event;
/**
* Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
*/
if (!($this->customerNumber = $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
get_class($this)))) {
return;
}
/**
* Prüfen ob der Benutzer eine Artikel-Whitelist hat.
*/
$customFields = $this->event->getSalesChannelContext()->getCustomer()->getCustomFields();
if (array_key_exists('custom_product_listing_numbers', $customFields) &&
$customFields['custom_product_listing_numbers'] != null &&
$customFields['custom_product_listing_numbers'] != 0 &&
$customFields['custom_product_listing_numbers'] != '') {
$allowedArticles = explode(',', $customFields['custom_product_listing_numbers']);
} else {
return;
}
/**
* Configeinstellungen prüfen und setzen.
*/
if (!$this->checkConfig()) {
return;
}
$productArray = array();
$products = $event->getEntities();
//** Abort, if empty */
if (false === $products) {
return;
}
$articles = [];
$counter = 0;
/** @var SalesChannelProductEntity $product */
foreach($products as $product ) {
// benutzer holen. -> custom field holen.(siehe oben) String in array umwandeln. Prüfen ob artikelnummer vorhanden. Wenn ja dann durchlaufen, wenn nein, continue.
if (!in_array($product->getProductNumber(), $allowedArticles)) {
continue;
}
$counter++;
if ($product->getParentId()) {
/**
* @var ProductEntity $parentProduct
*/
$parentProduct = $this->getProduct($product->getParentId(), ['children']);
$childArticles = array();
foreach ($parentProduct->getChildren()->getElements() as $children) {
$childArticle['position-id'] = $counter;
$childArticle['article-number'] = $children->getProductNumber();
$childArticle['quantity'] = 1;
$childArticle['quantity-unit'] = $children->getPackUnit();
$childArticles[] = $childArticle;
}
$childResult = $this->getArticleInfo($childArticles);
if ($childResult['head']['status'] == 'NOK') {
return;
}
$countProductItems = sizeof($childResult);
$from = 0;
$to = 0;
$x = 0;
foreach ($childResult['articles']['article'] as $childArticle) {
// $unitPrice = (float)$childArticle['conditions']['condition'][0]['condition-value'];
// $mwstValue = (float)$childArticle['conditions']['condition'][2]['condition-value'];
// $unitPrice = $unitPrice + $mwstValue;
$unitPrice = (float)$childArticle['price'] / (float)$childArticle['quantity'];
if ($x === 0) {
$from = $unitPrice;
$to = $unitPrice;
} elseif ($unitPrice < $from) {
$from = $unitPrice;
} elseif ($unitPrice > $to) {
$to = $unitPrice;
}
$x++;
}
// /**
// * @var CalculatedCheapestPrice $productListingPrice
// */
// $productListingPrice = $product->getCalculatedCheapestPrice();
// /**
// * @var CalculatedPrice $fromPrice , $toPrice
// */
// $fromPrice = new CalculatedPrice(
// $from,
// $from,
// $productListingPrice->getCalculatedTaxes(),
// $productListingPrice->getTaxRules(),
// 1);
// $toPrice = new CalculatedPrice(
// $to,
// $to,
// $productListingPrice->getCalculatedTaxes(),
// $productListingPrice->getTaxRules(),
// 1);
// /**
// * @var CalculatedCheapestPrice $newListingPrice
// */
// $newListingPrice = new CalculatedCheapestPrice($from, $to, $productListingPrice->getCalculatedTaxes(),
// $productListingPrice->getTaxRules());
// $product->getCheapestPrice()->getPrice()->first()->setGross($to);
// $product->getCheapestPrice()->getPrice()->first()->setNet($to);
}
$productArray[] = $product;
$article['position-id'] = $counter;
$article['article-number'] = $product->getProductNumber();
$article['quantity'] = 1;
$article['quantity-unit'] = $product->getPackUnit();
$articles[] = $article;
}
if (count($articles) <= 0) {
return;
}
$this->result = $this->getArticleInfo($articles);
if ($this->result['head']['status'] == 'NOK') {
return;
}
$countProductItems = sizeof($productArray);
$x = 0;
foreach ($productArray as $productItem) {
if ($countProductItems > 1) {
// $unitPrice = (float)$this->result['articles']['article'][$x]['conditions']['condition'][0]['condition-value'];
$totalPrice = (float)$this->result['articles']['article'][$x]['price'];
$unitPrice = (float)$totalPrice / (float)$this->result['articles']['article'][$x]['quantity'];
$mwstRate = 0.0; // (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-rate'];
$mwstValue = 0.0; // (float)$this->result['articles']['article'][$x]['conditions']['condition'][2]['condition-value'];
} else {
// $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
$totalPrice = (float)$this->result['articles']['article']['price'];
$unitPrice = (float)$totalPrice / (float)$this->result['articles']['article']['quantity'];
$mwstRate = 0.0; // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
$mwstValue = 0.0; // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
}
/**
* @var TaxRule $newTaxRule
*/
$newTaxRule = new TaxRule($mwstRate);
/**
* @var TaxRuleCollection $newTaxRuleCollection
*/
$newTaxRuleCollection = new TaxRuleCollection();
$newTaxRuleCollection->add($newTaxRule);
/**
* @var CalculatedTax $newCalculateTax
*/
$newCalculateTax = new CalculatedTax($mwstValue, $mwstRate, $totalPrice);
/**
* @var CalculatedTaxCollection $newCalculatedTaxCollection
*/
$newCalculatedTaxCollection = new CalculatedTaxCollection();
$newCalculatedTaxCollection->add($newCalculateTax);
$newCalculatedCheapestPrice = new CalculatedCheapestPrice($totalPrice, $totalPrice, $newCalculatedTaxCollection,
$newTaxRuleCollection, 1);
$productItem->setCalculatedCheapestPrice($newCalculatedCheapestPrice);
$productItem->setPrice(new PriceCollection([
new Price(
$this->event->getContext()->getCurrencyId(),
($unitPrice + $mwstValue),
$totalPrice,
false
),
]));
$x++;
}
}
/**
* @param ProductPageLoadedEvent $event
*/
public function onProductPageLoaded(ProductPageLoadedEvent|SalesChannelEntityLoadedEvent $event)
{
$this->event = $event;
/**
* Prüfung ob Benutzer angemeldet ist, sonst keine Live-Abfrage.
*/
if (!($this->customerNumber = $this->foundationService->checkLogin($this->event->getSalesChannelContext(),
get_class($this)))) {
return;
} else {
if (array_key_exists('custom_product_listing_numbers',
$this->event->getSalesChannelContext()->getCustomer()->getCustomFields())) {
$articles = explode(',',
$this->event->getSalesChannelContext()->getCustomer()->getCustomFields()['custom_product_listing_numbers']);
if (!in_array($this->event->getPage()->getProduct()->getProductNumber(), $articles)) {
$redirectResponse = new RedirectResponse($this->router->generate('frontend.home.page'));
return $redirectResponse->send();
}
}
}
/**
* Configeinstellungen prüfen und setzen.
*/
if (!$this->checkConfig()) {
return;
}
/**
* @var SalesChannelProductEntity $product
*/
$product = $this->event->getPage()->getProduct();
/**
* @todo Die position-id noch richtig machen
* @var array $article
*/
$article['position-id'] = 1;
$article['article-number'] = $product->getProductNumber();
$article['quantity'] = 1;
$article['quantity-unit'] = $product->getPackUnit();
$this->result = $this->getArticleInfo([$article]);
if ($this->result['head']['status'] == 'NOK') {
return;
}
// $unitPrice = (float)$this->result['articles']['article']['conditions']['condition'][0]['condition-value'];
$totalPrice = (float)$this->result['articles']['article']['price'];
$unitPrice = $totalPrice / (float)$this->result['articles']['article']['quantity'];
$mwstRate = 0.0; // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-rate'];
$mwstValue = 0.0; // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value'];
/**
* @var CalculatedPrice $price
*/
$price = $product->getCalculatedPrice();
/**
* @var TaxRule $newTaxRule
*/
$newTaxRule = new TaxRule($mwstRate);
/** a
* @var TaxRuleCollection $newTaxRuleCollection
*/
$newTaxRuleCollection = new TaxRuleCollection();
$newTaxRuleCollection->add($newTaxRule);
/**
* @var CalculatedTax $newCalculateTax
*/
$newCalculateTax = new CalculatedTax($mwstValue, $mwstRate, $unitPrice);
/**
* @var CalculatedTaxCollection $newCalculatedTaxCollection
*/
$newCalculatedTaxCollection = new CalculatedTaxCollection();
$newCalculatedTaxCollection->add($newCalculateTax);
/**
* @var TaxRule $newTaxRule
*/
$newTaxRule = new TaxRule(0.0); // (float)$this->result['articles']['article']['conditions']['condition'][2]['condition-value']);
$price->getTaxRules()->add($newTaxRule);
// Get from request or pick default
if (array_key_exists('free-shipping-threshold', $this->result['shipping-cost'])) {
$freeShippingThreshold = ['freeShippingThreshold' => $this->result['shipping-cost']['free-shipping-threshold']];
} else {
$freeShippingThreshold = ['freeShippingThreshold' => $this->calculateShippingThreshold($event->getSalesChannelContext())];
}
$this->event->getContext()->addArrayExtension('deliveryInformations', $freeShippingThreshold);
/**
* @var CalculatedPrice $newPrice
*/
$newPrice = new CalculatedPrice(
$totalPrice,
$totalPrice,
$newCalculatedTaxCollection,
$newTaxRuleCollection,
1,
null,
ListPrice::createFromUnitPrice($totalPrice, $totalPrice)
);
$newCalculatedCheapestPrice = new CalculatedCheapestPrice($totalPrice, $totalPrice, $newCalculatedTaxCollection,
$newTaxRuleCollection, 1);
$product->setCalculatedCheapestPrice($newCalculatedCheapestPrice);
$product->setCalculatedPrice($newPrice);
$product->getPrice()->first()->setNet($totalPrice);
$product->getPrice()->first()->setGross($totalPrice);
$product->getCheapestPrice()->getPrice()->first()->setGross($totalPrice);
$product->getCheapestPrice()->getPrice()->first()->setNet($totalPrice);
$product->getCalculatedPrices()->clear();
$product->setCheapestPrice(null);
$newCPContainer = new CheapestPriceContainer([]);
$product->setCheapestPriceContainer($newCPContainer);
}
/**
* @param array $articles
* @param int $shopInstance
* @param string $currency
* @param string $language
* @return
*/
private function getArticleInfo(
array $articles,
int $shopInstance = 1,
string $language = 'DE'
) {
$params = [
'customer' => $this->customerNumber,
'shop-instance' => $shopInstance,
'currency' => $this->getCurrencyISO()
];
$buildRequestHelper = new BuildRequestHelper($params, $articles, $this->errorHandler);
/**
* @var string $request
*/
$request = $this->requestHandler->buildRequest($buildRequestHelper, get_class($this));
$this->error = $this->errorHandler->error($this->errorHandler::GENERAL_ERROR, get_class($this),
"Request: " . print_r($request, true));
$this->errorHandler->writeErrorLog($this->error);
if ($request !== '') {
/**
* @var string $result
*/
$result = $this->requestHandler->sendRequest($this->host, $this->port, self::PATH, $request);
} else {
$this->error = $this->errorHandler->error($this->errorHandler::UNKNOWN_REQUEST, get_class($this),
'Fehler im Aufbau des Request');
$this->errorHandler->writeErrorLog($this->error);
return;
}
/**
* @var array $resultArray
*/
$resultArray = $this->requestHandler->parseResult($result);
if ($resultArray['head']['status'] == 'NOK') {
$this->error = $this->errorHandler->error($this->errorHandler::SAP_RESPONSE_ERROR, get_class($this),
'SAP Status Fehler. - ' . print_r($resultArray, true));
$this->errorHandler->writeErrorLog($this->error);
// } else {
// $this->error = $this->errorHandler->error($this->errorHandler::WRONG_ARTICLE_ERROR, get_class($this), 'LivePrice-Response after success. - ' . print_r($resultArray, true));
// $this->errorHandler->writeErrorLog($this->error);
}
return $resultArray;
}
/**
* @return bool
*/
private function checkConfig(): bool
{
if ($this->foundationService->getConfigStatus()) {
$this->host = $this->foundationService->getHost();
$this->port = $this->foundationService->getPort();
return true;
}
$this->error = $this->errorHandler->error($this->errorHandler::CONFIG_NOT_VALID, get_class($this),
'Konnte die Konfiguration nicht ermitteln');
$this->errorHandler->writeErrorLog($this->error);
return false;
}
/**
* @param string $productID
* @return ProductEntity
*/
public function getProduct(string $productID, array $associations = null): ProductEntity
{
/**
* @var Criteria $criteria
*/
$criteria = new Criteria([$productID]);
if ($associations !== null) {
foreach ($associations as $association) {
$criteria->addAssociation($association);
}
}
/**
* @var EntityCollection $products
*/
$products = $this->productRepository->search($criteria,
\Shopware\Core\Framework\Context::createDefaultContext());
/**
* @var array $productList
*/
$productList = $products->getElements();
/**
* @var ProductEntity $product
*/
$product = $productList[$productID];
return $product;
}
/**
* fts - Retrieve currency iso code from saleschannel context
* @return string
*/
function getCurrencyISO(): string
{
return $this->event?->getSalesChannelContext()?->getCurrency()->getIsoCode() ?? "EUR";
}
/**
* If the current currency is not EUR, then the default threshold (250€)
* will be converted to the appropriate currency using the currency factor.
* @return int
*/
private function calculateShippingThreshold(SalesChannelContext $context): int
{
if($context->getCurrency()->getIsoCode() === "PLN") {
return OrderSimulateSubscriber::ZLOTY_THRESHOLD;
} else {
return OrderSimulateSubscriber::EUR_THRESHOLD;
}
}
}