FreeGiftServiceProvider.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Longyi\FreeGift\Providers;
  3. use ApiPlatform\State\ProcessorInterface;
  4. use ApiPlatform\State\ProviderInterface;
  5. use Illuminate\Support\Facades\Event;
  6. use Illuminate\Support\ServiceProvider;
  7. use Longyi\FreeGift\Http\Requests\GiftCartRuleRequest;
  8. use Longyi\FreeGift\Repositories\GiftCartRuleRepository;
  9. use Longyi\FreeGift\State\GiftCartProcessor;
  10. use Longyi\FreeGift\State\GiftCartProvider;
  11. use Webkul\Admin\Http\Requests\CartRuleRequest;
  12. use Webkul\CartRule\Repositories\CartRuleRepository;
  13. class FreeGiftServiceProvider extends ServiceProvider
  14. {
  15. /**
  16. * Register services.
  17. */
  18. public function register(): void
  19. {
  20. $this->registerConfig();
  21. // GraphQL(API Platform)的 provider / processor 必须注册进容器,
  22. // 否则 CallableProcessor 的 service locator 找不到它们:
  23. // "Processor ... not found on operation create"
  24. $this->app->tag(GiftCartProvider::class, ProviderInterface::class);
  25. $this->app->tag(GiftCartProcessor::class, ProcessorInterface::class);
  26. // 复用 Bagisto 原生的营销规则(Marketing > Promotions > Cart Rules):
  27. // 规则本身的字段不重复造,只追加 free_gift[*] 配置。
  28. $this->app->bind(CartRuleRequest::class, GiftCartRuleRequest::class);
  29. $this->app->bind(CartRuleRepository::class, GiftCartRuleRepository::class);
  30. }
  31. /**
  32. * Bootstrap services.
  33. */
  34. public function boot(): void
  35. {
  36. $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
  37. $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'free_gift');
  38. $this->loadViewsFrom(__DIR__.'/../Resources/views', 'free_gift');
  39. $this->loadRoutesFrom(__DIR__.'/../Routes/shop-routes.php');
  40. // 后台:赠品 SKU 选择器(商品搜索接口)
  41. $this->loadRoutesFrom(__DIR__.'/../Routes/admin-routes.php');
  42. $this->app->register(EventServiceProvider::class);
  43. // 前台购物车页:赠品 Banner / 挑选弹窗
  44. Event::listen('bagisto.shop.checkout.cart.header.after', function ($viewRenderEventManager) {
  45. $viewRenderEventManager->addTemplate('free_gift::shop.cart.gift-container');
  46. });
  47. // 前台购物车行:赠品标记
  48. Event::listen('bagisto.shop.checkout.cart.item_name.after', function ($viewRenderEventManager) {
  49. $viewRenderEventManager->addTemplate('free_gift::shop.cart.gift-item-badge');
  50. });
  51. }
  52. /**
  53. * 系统配置项(System Configuration > Sales > Free Gift)。
  54. */
  55. protected function registerConfig(): void
  56. {
  57. $this->mergeConfigFrom(dirname(__DIR__).'/Config/system.php', 'core');
  58. }
  59. }