Files
kupshop/bundles/KupShop/GTMBundle/Ecommerce/SearchResult.php
2025-08-02 16:30:27 +02:00

91 lines
2.9 KiB
PHP

<?php
namespace KupShop\GTMBundle\Ecommerce;
use Doctrine\Common\Collections\ArrayCollection;
class SearchResult extends AbstractEcommerce
{
public function getData(&$dataContainer)
{
$search = new \stdClass();
$search->searchTerm = $this->view->getTitle();
$search->results = $this->buildSearchData();
$dataContainer->search = $search;
$dataContainer->event = 'searchResults';
}
public function buildSearchData(): array
{
$results = [];
$termForEmptyResult = null;
$items = $this->view->getTemplateData()['body']['results'] ?? [];
foreach ($items as $title => $item) {
if ($title === 'sections') {
$title = 'categories';
}
$list = $item['list'] ?? null;
if ($list instanceof ArrayCollection) {
$list = $list->toArray();
}
$fitProperty = fn ($k, $object = null) => [$k => $object ?? null];
$newList = [];
foreach ($list as $v) {
if ($title === 'products') {
$newList[] = array_merge(
$fitProperty('id', $v['id']),
$fitProperty('name', $v['title']),
$fitProperty('price', $v['price_array']['price_without_vat']),
$fitProperty('brand', $v['producer']['name']),
);
}
if ($title === 'categories') {
$newList[] = array_merge(
$fitProperty('id', $v['id']),
$fitProperty('name', $v['name']),
$fitProperty('path', $v['original_url']),
$fitProperty('label', $v['title']),
$fitProperty('imageUrl', $v['photo_src']),
);
}
if ($title === 'articles') {
$newList[] = array_merge(
$fitProperty('id', $v['id']),
$fitProperty('image', $v['url']),
$fitProperty('label', $v['title']),
);
}
if ($title === 'producers') {
$newList[] = array_merge(
$fitProperty('id', $v['id']),
$fitProperty('name', $v['title']),
$fitProperty('label', $v['title'])
);
}
if ($title === 'pages') {
$newList[] = array_merge(
$fitProperty('id', $v['id']),
$fitProperty('name', $v['name']),
$fitProperty('path', $v['original_url']),
);
}
$list = $newList;
}
$results[$title] = $list === [] ? $termForEmptyResult : $list;
}
return $results;
}
}