Hi guys, I'm developing something like online shop with different products, and I wonder if someone of you ever needed to hide some search results under different conditions? In this particular case, I have products with expiration date and I want to hide the expired products from the search engine. I'm using the integrated October CMS Form Widget - Datepicker for setting the date in the backend.
Do someone know what would be the best solution for this case? Thanks!
Last updated
If you are building a custom plugin you can add a results provider event handler or class as documented here: https://github.com/OFFLINE-GmbH/oc-site-search-plugin/#example-to-search-for-custom-documents
Simply filter the products with
->where('expires_at', '>', now())->orWhereNull('expires_at');
Yes, I'm using exactly the same event handler for my custom plugins. So...I tried it, but maybe not in the right way. The query throws all products at once. What am I doing wrong?
// Search your plugin's contents
$product = Models\Product::->where('product_validation', '>', now())
->orWhereNull('product_validation')
->orWhere('product_name', 'like', "%${query}%")
->orWhere('product_tags', 'like', "%${query}%")
->orWhere('product_description', 'like', "%${query}%")
->get();
Last updated
Try this
$product = Models\Product::where(function($q) {
$q->where('product_validation', '>', now())
->orWhereNull('product_validation');
})->where(function($q) {
$q->where('product_name', 'like', "%${query}%")
->orWhere('product_tags', 'like', "%${query}%")
->orWhere('product_description', 'like', "%${query}%");
})
->get();
Last updated
Mmm I'm getting an error: Undefined variable: query
\Event::listen('offline.sitesearch.query', function ($query) {
// Search your plugin's contents
$product = Models\Product::where(function($q) {
$q->where('product_validation', '>', now())
->orWhereNull('product_validation');
})->where(function($q) {
$q->where('product_name', 'like', "%${query}%")
->orWhere('product_tags', 'like', "%${query}%")
->orWhere('product_description', 'like', "%${query}%");
})
->get();
}
If it's helpful, I can show you the whole boot function code here:
public function boot()
{
\Event::listen('offline.sitesearch.query', function ($query) {
// Search your plugin's contents
$product = Models\Product::where(function($q) {
$q->where('product_validation', '>', now())
->orWhereNull('product_validation');
})->where(function($q) {
$q->where('product_name', 'like', "%${query}%")
->orWhere('product_tags', 'like', "%${query}%")
->orWhere('product_description', 'like', "%${query}%");
})
->get();
// Now build a results array
$productResults = $product->map(function ($item) use ($query) {
// If the query is found in the title, set a relevance of 2
$relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;
if($item->product_img)
{
return [
'title' => $item->product_name,
'text' => $item->product_validation,
'url' => '/product/' . $item->product_slug,
'thumb' => $item->product_img, // Instance of System\Models\File
'relevance' => $relevance // higher relevance results in a higher
// position in the results listing
// 'meta' => 'data', // optional, any other information you want
// to associate with this result
// 'model' => $item, // optional, pass along the original model
];
} else {
return [
'title' => $item->product_name,
'text' => $item->product_validation,
'url' => '/product/' . $item->product_slug,
'relevance' => $relevance // higher relevance results in a higher
// position in the results listing
// 'meta' => 'data', // optional, any other information you want
// to associate with this result
// 'model' => $item, // optional, pass along the original model
];
}
});
return [
'provider' => '', // The badge to display for this result
'results' => $productResults,
];
});
Yes, you have to use
your query variable:
$product = Models\Product::where(function($q) {
$q->where('product_validation', '>', now())
->orWhereNull('product_validation');
})->where(function($q) use ($query) { // This is important.
$q->where('product_name', 'like', "%${query}%")
->orWhere('product_tags', 'like', "%${query}%")
->orWhere('product_description', 'like', "%${query}%");
})
->get();
OFFLINE said:
Yes, you have to
use
your query variable:$product = Models\Product::where(function($q) { $q->where('product_validation', '>', now()) ->orWhereNull('product_validation'); })->where(function($q) use ($query) { // This is important. $q->where('product_name', 'like', "%${query}%") ->orWhere('product_tags', 'like', "%${query}%") ->orWhere('product_description', 'like', "%${query}%"); }) ->get();
Maaaan, thank you!!! It works like charm!
I'm just not so good in php and...you see. Big thanks again! Cheers!
Last updated
1-8 of 8