Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes ALP facets for multiple stores #116

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions Controller/Ajax/FacetAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,46 @@ public function execute()

$categoryId = $this->getRequest()->getParam('category');
$facetKey = $this->getRequest()->getParam('facetkey');
//remove category id for now. It can give the wrong store id for the admin which results in the wrong tncid
//$facetRequest->addCategoryFilter($categoryId);
$facetRequest->addFacetKey($facetKey);
$filtertemplate = (int)$this->getRequest()->getParam('filtertemplate');
$allStores = $facetRequest->getStores();

if (!empty($facetKey)) {
$facetRequest->addFacetKey($facetKey);
}

if (!empty($filtertemplate)) {
$facetRequest->addParameter('tn_ft', $filtertemplate);
}

$result = [];
try {
$response = $this->client->request($facetRequest);
foreach ($response->getAttributes() as $attribute) {
$result[] = ['value' => $attribute['title'], 'label' => $attribute['title']];
foreach ($allStores as $store) {
$facetRequest->setStore($store->getId());
if (!empty($categoryId)) {
$facetRequest->addCategoryFilter($categoryId);
}
} catch (ApiException $e) {
if (!$e->getCode() == 404) {
throw $e;
try {
$response = $this->client->request($facetRequest);
} catch (ApiException $e) {
if (!$e->getCode() == 404) {
throw $e;
}
continue;
}

if (!empty($response->getAttributes())) {
foreach ($response->getAttributes() as $attribute) {
$result[] = ['value' => $attribute['title'], 'label' => $attribute['title']];
}
}
}

$result[] = ['value' => 'tw_other', 'label' => 'Other (text field)'];

$result = array_unique($result, SORT_REGULAR);

//prevent non sequential array keys. That causes json encode to act diffrently and creates objects instead of arrays
$result = array_values($result);

$json->setData(['data' => $result]);
return $json;
}
Expand Down
31 changes: 25 additions & 6 deletions Controller/Ajax/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,38 @@ public function __construct(Context $context, JsonFactory $jsonFactory, RequestF

public function execute()
{
$result = [];
$json = $this->resultFactory->create('json');
$facetRequest = $this->requestFactory->create();

$categoryId = $this->getRequest()->getParam('category');
//remove category id for now. It can give the wrong store id for the admin which results in the wrong tncid
//$facetRequest->addCategoryFilter($categoryId);
$filtertemplate = (int) $this->getRequest()->getParam('filtertemplate');
$allStores = $facetRequest->getStores();

$response = $this->client->request($facetRequest);
$result = [];
foreach ($response->getFacets() as $facet) {
$result[] = ['value' => $facet->getFacetSettings()->getUrlKey(), 'label' => $facet->getFacetSettings()->getTitle()];
if (!empty($filtertemplate)) {
$facetRequest->addParameter('tn_ft', $filtertemplate);
}

foreach ($allStores as $store) {
$facetRequest->setStore($store->getId());
if (!empty($categoryId)) {
$facetRequest->addCategoryFilter($categoryId);
}

$response = $this->client->request($facetRequest);

foreach ($response->getFacets() as $facet) {
$result[] = ['value' => $facet->getFacetSettings()->getUrlKey(), 'label' => $facet->getFacetSettings()->getTitle()];
}
}

$result[] = ['value' => 'tw_other', 'label' => 'Other (text field)'];

$result = array_unique($result, SORT_REGULAR);

//prevent non sequential array keys. That causes json encode to act differently and creates objects instead of arrays
$result = array_values($result);

$json->setData(['data' => $result]);
return $json;
}
Expand Down
17 changes: 17 additions & 0 deletions Model/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,23 @@ public function getCategoryPathFilter()
return implode('-', $categoryPath);
}

/**
* @param $storeId
* @return void
*/
public function setStore($storeId)
{
$this->storeManager->setCurrentStore((int) $storeId);
}

/**
* @return array|StoreInterface[]
*/
public function getStores()
{
return $this->storeManager->getStores();
}

/**
* @return StoreInterface|null
*/
Expand Down