Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

unopim/unopim

7807 stars
3 code files
View unopim/unopim on GitHub

packages/Webkul/Product/src/Jobs/ElasticSearch/UpdateCreateIndex.php

Open in GitHub
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Webkul\Product\Helpers\Indexers\ElasticSearch;
use Webkul\Product\Repositories\ProductRepository;
 
class UpdateCreateIndex implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
public function __construct(protected $productIds)
{
$this->productIds = $productIds;
}
 
public function handle()
{
if (core()->getConfigData('catalog.products.storefront.search_mode') != 'elastic') {
return;
}
 
$ids = implode(',', $this->productIds);
 
$products = app(ProductRepository::class)
->whereIn('id', $this->productIds)
->orderByRaw("FIELD(id, $ids)")
->get();
 
app(ElasticSearch::class)->reindexRows($products);
}
}

packages/Webkul/Product/src/Jobs/ElasticSearch/DeleteIndex.php

Open in GitHub
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Webkul\Product\Helpers\Indexers\ElasticSearch;
 
class DeleteIndex implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
public function __construct(protected $productIds)
{
$this->productIds = $productIds;
}
 
public function handle()
{
if (core()->getConfigData('catalog.products.storefront.search_mode') != 'elastic') {
return;
}
 
$removeIndices = [];
 
foreach (core()->getAllChannels() as $channel) {
foreach ($channel->locales as $locale) {
$index = 'products_'.$channel->code.'_'.$locale->code.'_index';
 
$removeIndices[$index] = $this->productIds;
}
}
 
app(ElasticSearch::class)->deleteIndices($removeIndices);
}
}

packages/Webkul/Product/src/Listeners/Product.php

Open in GitHub
use Illuminate\Support\Facades\Bus;
use Webkul\Product\Jobs\ElasticSearch\DeleteIndex as DeleteElasticSearchIndexJob;
use Webkul\Product\Jobs\ElasticSearch\UpdateCreateIndex as UpdateCreateElasticSearchIndexJob;
 
class Product
{
// ...
 
public function afterUpdate($product)
{
$productIds = $this->getAllRelatedProductIds($product);
 
Bus::chain([
new UpdateCreateElasticSearchIndexJob($productIds),
])->dispatch();
}
 
public function beforeDelete($productId)
{
if (core()->getConfigData('catalog.products.storefront.search_mode') != 'elastic') {
return;
}
 
DeleteElasticSearchIndexJob::dispatch([$productId]);
}
 
// ...
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.