use Illuminate\Support\Facades\Bus;
use Webkul\Product\Jobs\ElasticSearch\DeleteIndex as DeleteElasticSearchIndexJob;
use Webkul\Product\Jobs\ElasticSearch\UpdateCreateIndex as UpdateCreateElasticSearchIndexJob;
use Webkul\Product\Repositories\ProductRepository;
class Product
{
public function __construct(
protected ProductRepository $productRepository,
) {}
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]);
}
public function getAllRelatedProductIds($product)
{
$productIds = [$product->id];
if ($product->type == 'simple') {
if ($product->parent_id) {
$productIds[] = $product->parent_id;
}
$productIds = array_merge(
$productIds,
);
} elseif ($product->type == 'configurable') {
$productIds = [
...$product->variants->pluck('id')->toArray(),
...$productIds,
];
}
return $productIds;
}
}