Skip to content

Commit

Permalink
Configs
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin committed Jan 31, 2025
1 parent fd4475e commit 8b7a378
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 55 deletions.
15 changes: 3 additions & 12 deletions config/rapidez/indexer.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?php

return [
// TODO: This configuration is a bit lonely here,
// maybe we can move this to Scout later?
// Which product visibilities should be indexed?
// VISIBILITY_NOT_VISIBLE = 1
// VISIBILITY_IN_CATALOG = 2
// VISIBILITY_IN_SEARCH = 3
// VISIBILITY_BOTH = 4
'visibility' => [2, 3, 4],

// Additional searchable attributes with the search weight.
'searchable' => [
// 'attribute' => 4.0,
],

// From Magento only "Yes/No, Dropdown, Multiple Select and Price" attribute types
// can be configured as filter. If you'd like to have a filter for an attribute
// with, for example, the type of "Text", you can specify the attribute_code here.
'additional_filters' => [
// eav_attribute attribute_code. e.g. brand
],
];
56 changes: 56 additions & 0 deletions config/rapidez/searchkit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

// See: https://www.searchkit.co/docs/api-documentation/searchkit#search_settings-configuration
return [
// Attributes that are used to highlight the search results.
'highlight_attributes' => [
// 'name',
],

// Attributes that are used to search the results.
// This will be merged with the searchable
// attributes configured in Magento.
'search_attributes' => [
// ['field' => 'attribute_code', 'weight' => 4.0],
],

// Attributes that are returned in the search result response.
// Don't want to keep track of this? An empty array will
// return all attributes, but that's not recommended!
'result_attributes' => [
'entity_id',
'name',
'sku',
'price',
'special_price',
'image',
'images',
'url',
'thumbnail',
'in_stock',
'children',
'super_*',
'reviews_count',
'reviews_score',
],

// Attributes that are used to create facets.
// From Magento only "Yes/No, Dropdown, Multiple Select and Price" attribute types
// can be configured as filter. If you'd like to have a filter for an attribute
// with, for example, the type of "Text", you can specify the attribute_code here.
'facet_attributes' => [
// ['attribute' => 'brand', 'field' => 'brand.keyword', 'type' => 'string'],
],

// Attributes that are used to create filters.
// TODO: Do we really need this? With ReactiveSearch
// we didn't need to keep a list to filter.
'filter_attributes' => [
['attribute' => 'entity_id', 'field' => 'entity_id', 'type' => 'numeric'],
['attribute' => 'category_ids', 'field' => 'category_ids', 'type' => 'numeric'],
['attribute' => 'visibility', 'field' => 'visibility', 'type' => 'numeric'],
],

// TODO: Sorting...
// 'sorting' => []
];
34 changes: 20 additions & 14 deletions resources/js/components/Listing/Listing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import useAttributes from '../../stores/useAttributes.js'
export default {
props: {
// TODO: Do we still use/need this?
// Maybe transform it to a callback
// so the items can be manipulated?
additionalFilters: {
type: Array,
default: () => [],
Expand Down Expand Up @@ -94,25 +96,26 @@ export default {
password: url.password,
},
},
search_settings: {
// Are we using this? In the autocomplete maybe?
// highlight_attributes: ['title'],
search_attributes: Object.entries(config.searchable).map(([field, weight]) => ({ field, weight })),
// We could make the response smaller with this
// result_attributes: ['title', 'actors', 'poster', 'plot'],
// TODO: Maybe just do: search_settings: config.searchkit
// so it's possible to add anything to the PHP config
// and that will appear here?
search_settings: {
highlight_attributes: config.searchkit.highlight_attributes,
search_attributes: config.searchkit.search_attributes,
result_attributes: config.searchkit.result_attributes,
// TODO: For consistency maybe make it possible to do this:
// facet_attributes: config.searchkit.facet_attributes,
facet_attributes: this.facets,
// TODO: Do we really need this? With ReactiveSearch
// we didn't need to keep a list to filter.
filter_attributes: [
{ attribute: 'entity_id', field: 'entity_id', type: 'numeric' },
{ attribute: 'category_ids', field: 'category_ids', type: 'numeric' },
{ attribute: 'visibility', field: 'visibility', type: 'numeric' },
],
filter_attributes: config.searchkit.filter_attributes,
// TODO: Let's also change this to a PHP config.
// So we start there and that will be merged
// with the Magento configured attributes
// and lastly from a prop it's possible
// to manipulate it from a callback?
sorting: this.sortOptions.reduce((acc, item) => {
acc[item.key] = {
field: item.field,
Expand All @@ -128,6 +131,9 @@ export default {
return searchkit
},
// TODO: Maybe move this completely to PHP?
// Any drawbacks? A window.config that
// becomes to big? Is that an issue?
filters: function () {
return Object.values(this.attributes)
.filter((attribute) => attribute.filter)
Expand Down
100 changes: 71 additions & 29 deletions src/Http/ViewComposers/ConfigComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@
use Illuminate\View\View;
use Rapidez\Core\Facades\Rapidez;

// TODO: Can we improve anything in this file?
// It doesn't feel very clean currently.
class ConfigComposer
{
public function compose(View $view)
{
$this
->exposeGraphqlQueries()
->configureSearchkit();

$exposedFrontendConfigValues = Arr::only(
array_merge_recursive(config('rapidez'), config('rapidez.frontend')),
array_merge(config('rapidez.frontend.exposed'), ['store_code', 'index'])
array_merge_recursive(
config('rapidez'),
config('rapidez.frontend'),
config('rapidez.searchkit'),
),
array_merge(
config('rapidez.frontend.exposed'),
['store_code', 'index', 'searchkit'],
),
);

Config::set('frontend', array_merge(
Expand All @@ -25,37 +38,11 @@ public function compose(View $view)
$this->getConfig()
));

Config::set('frontend.queries', [
'customer' => view('rapidez::customer.queries.customer')->renderOneliner(),
'setGuestEmailOnCart' => view('rapidez::checkout.queries.setGuestEmailOnCart')->renderOneliner(),
'setNewShippingAddressesOnCart' => view('rapidez::checkout.queries.setNewShippingAddressesOnCart')->renderOneliner(),
'setExistingShippingAddressesOnCart' => view('rapidez::checkout.queries.setExistingShippingAddressesOnCart')->renderOneliner(),
'setNewBillingAddressOnCart' => view('rapidez::checkout.queries.setNewBillingAddressOnCart')->renderOneliner(),
'setExistingBillingAddressOnCart' => view('rapidez::checkout.queries.setExistingBillingAddressOnCart')->renderOneliner(),
'setShippingMethodsOnCart' => view('rapidez::checkout.queries.setShippingMethodsOnCart')->renderOneliner(),
'setPaymentMethodOnCart' => view('rapidez::checkout.queries.setPaymentMethodOnCart')->renderOneliner(),
'placeOrder' => view('rapidez::checkout.queries.placeOrder')->renderOneliner(),
]);

Config::set('frontend.fragments', [
'cart' => view('rapidez::cart.queries.fragments.cart')->renderOneliner(),
'order' => view('rapidez::checkout.queries.fragments.order')->renderOneliner(),
'orderV2' => view('rapidez::checkout.queries.fragments.orderV2')->renderOneliner(),
]);

Event::dispatch('rapidez:frontend-config-composed');
}

public function getConfig(): array
{
$attributeModel = config('rapidez.models.attribute');
$searchableAttributes = Arr::pluck(
$attributeModel::getCachedWhere(fn ($attribute) => $attribute['search'] && in_array($attribute['type'], ['text', 'varchar', 'static'])
),
'search_weight',
'code'
);

return [
'locale' => Rapidez::config('general/locale/code', 'en_US'),
'default_country' => Rapidez::config('general/country/default', 'NL'),
Expand All @@ -65,7 +52,6 @@ public function getConfig(): array
'show_swatches' => (bool) Rapidez::config('catalog/frontend/show_swatches_in_product_list'),
'translations' => __('rapidez::frontend'),
'recaptcha' => Rapidez::config('recaptcha_frontend/type_recaptcha_v3/public_key', null, true),
'searchable' => array_merge($searchableAttributes, config('rapidez.indexer.searchable')),
'show_customer_address_fields' => $this->getCustomerAddressFields(),
'street_lines' => Rapidez::config('customer/address/street_lines', 2),
'show_tax' => (bool) Rapidez::config('tax/display/type', 1),
Expand Down Expand Up @@ -94,4 +80,60 @@ public function getCustomerAddressFields(): array
'fax' => Rapidez::config('customer/address/fax_show', 'opt'),
];
}

public function exposeGraphqlQueries(): self
{
$checkoutQueries = [
'setGuestEmailOnCart',
'setNewShippingAddressesOnCart',
'setExistingShippingAddressesOnCart',
'setNewBillingAddressOnCart',
'setExistingBillingAddressOnCart',
'setShippingMethodsOnCart',
'setPaymentMethodOnCart',
'placeOrder',
];

// TODO: Maybe limit this to just the checkout pages?
foreach ($checkoutQueries as $checkoutQuery) {
Config::set(
'frontend.queries.'.$checkoutQuery,
view('rapidez::checkout.queries.'.$checkoutQuery)->renderOneliner()
);
}

Config::set(
'frontend.queries.customer',
view('rapidez::customer.queries.customer')->renderOneliner()
);

Config::set('frontend.fragments', [
'cart' => view('rapidez::cart.queries.fragments.cart')->renderOneliner(),
'order' => view('rapidez::checkout.queries.fragments.order')->renderOneliner(),
'orderV2' => view('rapidez::checkout.queries.fragments.orderV2')->renderOneliner(),
]);

return $this;
}

public function configureSearchkit(): self
{
$attributeModel = config('rapidez.models.attribute');

// Get all searchable attributes from Magento.
$searchableAttributes = $attributeModel::getCachedWhere(function ($attribute) {
return $attribute['search']
&& in_array($attribute['type'], ['text', 'varchar', 'static']);
});

// Map and merge them with the config.
$searchableAttributes = collect($searchableAttributes)->map(fn ($attribute) => [
'field' => $attribute['code'],
'weight' => $attribute['search_weight'],
])->merge(config('rapidez.searchkit.search_attributes'))->values()->toArray();

Config::set('rapidez.searchkit.search_attributes', $searchableAttributes);

return $this;
}
}
1 change: 1 addition & 0 deletions src/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected static function booting()

protected function filter(): CastsAttribute
{
// TODO: Double check; this config has been (re)moved.
return CastsAttribute::make(
get: fn ($value) => $value || in_array($this->code, config('rapidez.indexer.additional_filters')),
)->shouldCache();
Expand Down
1 change: 1 addition & 0 deletions src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class RapidezServiceProvider extends ServiceProvider
'jwt',
'models',
'routing',
'searchkit',
'system',
];

Expand Down

0 comments on commit 8b7a378

Please sign in to comment.