Hotfix: Correcting ProductResolver when importing Products

This commit is contained in:
2026-08-27 00:46:43 +03:00
parent b86fe78852
commit d01d27f7ea
@@ -7,13 +7,23 @@ use Lunar\Models\Url;
class ProductResolver
{
/**
* A slug can have more than one `lunar_urls` row pointing at it across import
* batches — e.g. a product soft-deleted and re-imported leaves its old URL row
* behind, still matching the same slug. Picking "whichever Url row matches
* first" (as a plain Url::where('slug', ...)->first() would) can resolve to a
* soft-deleted product, silently failing every downstream write for that
* product (e.g. JudgeMeExportImporter logging "no product found" for a handle
* that, in isolation, clearly exists). Join against `lunar_products` directly
* so only a URL pointing at a live (non-deleted) product resolves.
*/
public function resolve(string $handle): ?Product
{
$url = Url::query()
->where('slug', $handle)
->where('element_type', (new Product)->getMorphClass())
return Product::query()
->join('lunar_urls', 'lunar_urls.element_id', '=', 'lunar_products.id')
->where('lunar_urls.slug', $handle)
->where('lunar_urls.element_type', (new Product)->getMorphClass())
->select('lunar_products.*')
->first();
return $url?->element;
}
}