So, I have a custom product attribute:
$eavSetup->addAttribute('catalog_product', 'visible_in_country', [
'type' => 'varchar',
'backend_type' => 'text',
'backend_model' => MagentoEavModelEntityAttributeBackendArrayBackend::class,
'input' => 'multiselect',
'searchable' => true,
'filterable' => true,
'filterable_in_search' => false,
...
]);
In Elasticsearch, the values of the attribute are saved as a string and as an array:
{ ... ,"hits":[{ ... ,"_source":{ ... ,"sku":"test-sku", ... "visible_in_country":["DE","UK"], ... }},{ ... ,"_source":{ ... ,"sku":"test-sku-1", ... "visible_in_country":"UK" ... }}]}}
This is how I rewrite the product resolver to filter by country (which is obviously wrong):
<?php
declare(strict_types=1);
namespace TestCountryBasedCatalogForGraphQLPlugin;
class ProductResolverAlignOutput
{
public function __construct(
protected MagentoFrameworkAppRequestInterface $request
) {
}
public function beforeResolve(
MagentoCatalogGraphQlModelResolverProducts $subject,
MagentoFrameworkGraphQlConfigElementField $field,
MagentoFrameworkGraphQlQueryResolverContextInterface $context,
MagentoFrameworkGraphQlSchemaTypeResolveInfo $info,
array $value = null,
array $args = null
): array {
if ($country = $this->request->getHeader('country')) {
// $country = 'DE'
$args['filter']['visible_in_country'] = ['eq' => $country];
}
return [$field, $context, $info, $value, $args];
}
}
di.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogGraphQlModelResolverProducts">
<plugin name="ProductResolverAlignOutput"
type="TestCountryBasedCatalogForGraphQLPluginProductResolverAlignOutput"
sortOrder="1" disabled="false" />
</type>
</config>
This is the GraphQL query:
query {
products {
total_count
items {
name
sku
visible_in_country
saleable_in_country
stock_status
}
}
}
The result always contains 0 items.
How to rewrite the resolver to make filtering by country work? Or maybe I need to update search_request.xml?