<?php declare(strict_types=1);
namespace XioniCmsTwig\Core\Content\Product\Subscriber;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\Events\ProductListingRouteCacheKeyEvent;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
use Shopware\Core\Framework\Uuid\Uuid;
class ProductListingSubscriber implements EventSubscriberInterface
{
private $requestStack;
public function __construct(RequestStack $requestStack) {
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
ProductListingResultEvent::class => 'onListing',
ProductListingRouteCacheKeyEvent::class => 'onRouteCacheKey'
];
}
public function onListing(ProductListingResultEvent $event): void
{
$request = $event->getRequest();
$chosen_values = "";
$variants = array();
$product_numbers = array();
$new_products = array();
$xlisting = false;
$count = 0;
for ($i = 0; $i<=50 ; $i++) {
if ($request->get("xoption_".$i) != null) {
$xlisting = true;
break;
}
}
if ($xlisting) {
for ($i = 0; $i<=50 ; $i++) {
if ($request->get("xoption_".$i) == null) {
continue;
} else {
$count = $count+1;
}
if(is_array($request->get("xoption_".$i))) {
foreach($request->get("xoption_".$i) as $index => $item) {
if ($chosen_values != "") {
$chosen_values = $chosen_values . "," . $item;
} else {
$chosen_values = $item;
}
}
} else {
if ($chosen_values != "") {
$chosen_values = $chosen_values . "," . $request->get("xoption_".$i);
} else {
$chosen_values = $request->get("xoption_".$i);
}
}
}
if($chosen_values != "" && $count != 0) {
$variants = $this->getVariantsByValues($chosen_values, $count);
$variants = $variants['data'];
}
foreach ($variants as $index => $item) {
$product_number = $this->getProductNumberByMatrixId($item['base']['id'])['data'];
$product_numbers[] = $product_number;
$configuration = $this->getConfigurationByMatrixId($item['base']['id'])['data'];
$configuration = explode(':', $configuration);
$new_products[$index]['groups'] = $configuration[1];
$new_products[$index]['values'] = $configuration[0];
$new_products[$index]['matrix_id'] = $item['base']['id'];
$new_products[$index]['name'] = $item['product'][0]['base']['description_long'];
$new_products[$index]['price'] = $item['product'][0]['prices'][0]['price'];
$new_products[$index]['description'] = $item['base']['name'];
if($item['product'][0]['media_details']) {
$new_products[$index]['media'] = $item['product'][0]['media_details'][0]['media_path'];
} else {
$new_products[$index]['media'] = null;
}
$new_products[$index]['product_number'] = $product_number;
}
$listing_products = $event->getResult()->getElements();
$original_products = array();
foreach ($listing_products as $index => $item) {
$product_number = $item->getProductNumber();
if(!in_array($product_number, $product_numbers)) {
$event->getResult()->remove($index);
} else {
$original_products[] = $item;
$event->getResult()->remove($index);
}
}
foreach($new_products as $index => $item) {
foreach ($original_products as $l_index => $l_item) {
$product_number = $l_item->getProductNumber();
if ($item['product_number'] == $product_number) {
$copied = clone($l_item);
$copied->setUniqueIdentifier(Uuid::randomHex());
$copied->setVersionId(Uuid::randomHex());
$copied->setName($item['name']);
$copied->addTranslated("name", $item['name']);
$price = floatval($item['price']);
$price = new CalculatedPrice($price, $price, new CalculatedTaxCollection(), new TaxRuleCollection());
$copied->setCalculatedPrice($price);
$copied->addExtension('xconfig', new ArrayEntity([
'matrix_id' => $item['matrix_id'],
'media' => $item['media'],
'new_name' => $item['name'],
'product_number' => $item['product_number'],
'groups' => $item['groups'],
'values' => $item['values'],
'description' => $item['description']
]));
$event->getResult()->add($copied);
unset($copied);
break;
}
}
}
}
}
public function onRouteCacheKey(ProductListingRouteCacheKeyEvent $event): void
{
$xlisting = false;
for ($i = 0; $i<=50 ; $i++) {
if ($event->getRequest()->get("xoption_".$i) != null) {
$xlisting = true;
break;
}
}
if ($xlisting) {
for ($i = 0; $i<=50 ; $i++) {
if ($event->getRequest()->get('xoption_'.$i) == null) {
continue;
}
if(is_array($event->getRequest()->get('xoption_'.$i))) {
foreach($event->getRequest()->get('xoption_'.$i) as $index => $item) {
$event->addPart(urlencode((string)$item));
}
} else {
$event->addPart(urlencode((string)$event->getRequest()->get('xoption_'.$i)));
}
}
}
}
private function getVariantsByValues($chosen_values, $count)
{
$url = 'http://cloud8-vm443.de-nserver.de:8080/api/configurators/variantsbyvalues/get/';
$data = array('chosen_values' => $chosen_values, 'count' => $count);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$result = json_decode($result, true);
return $result;
}
private function getProductNumberByMatrixId($matrix_id)
{
$url = 'http://cloud8-vm443.de-nserver.de:8080/api/configurators/matrix/getproductnumber/'.$matrix_id;
$value = file_get_contents($url);
$value = json_decode($value, true);
return $value;
}
private function getConfigurationByMatrixId($matrix_id)
{
$url = 'http://cloud8-vm443.de-nserver.de:8080/api/configurators/configurationbyvariant/get/'.$matrix_id;
$value = file_get_contents($url);
$value = json_decode($value, true);
return $value;
}
}