-
packages/Webkul/Product/src/Providers/EventServiceProvider.php
Open in GitHubuse Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { protected $listen = [ 'catalog.product.update.after' => [ 'Webkul\Product\Listeners\Product@afterUpdate', ], 'catalog.product.delete.before' => [ 'Webkul\Product\Listeners\Product@beforeDelete', ], ]; }
-
packages/Webkul/Product/src/Listeners/Product.php
Open in GitHubuse 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; } }
-
packages/Webkul/Admin/src/Http/Controllers/Catalog/ProductController.php
Open in GitHubuse Illuminate\Support\Facades\Event; use Webkul\Admin\Http\Controllers\Controller; use Webkul\Admin\Http\Requests\ProductForm; class ProductController extends Controller { // ... public function update(ProductForm $request, int $id) { Event::dispatch('catalog.product.update.before', $id); // ... $product = $this->productRepository->update($data, $id); Event::dispatch('catalog.product.update.after', $product); session()->flash('success', trans('admin::app.catalog.products.update-success')); return redirect()->route('admin.catalog.products.edit', [ 'id' => $id, 'channel' => core()->getRequestedChannelCode(), 'locale' => core()->getRequestedLocaleCode(), ]); } // ... }