| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace Longyi\FreeGift\Providers;
- use ApiPlatform\State\ProcessorInterface;
- use ApiPlatform\State\ProviderInterface;
- use Illuminate\Support\Facades\Event;
- use Illuminate\Support\ServiceProvider;
- use Longyi\FreeGift\Http\Requests\GiftCartRuleRequest;
- use Longyi\FreeGift\Repositories\GiftCartRuleRepository;
- use Longyi\FreeGift\State\GiftCartProcessor;
- use Longyi\FreeGift\State\GiftCartProvider;
- use Webkul\Admin\Http\Requests\CartRuleRequest;
- use Webkul\CartRule\Repositories\CartRuleRepository;
- class FreeGiftServiceProvider extends ServiceProvider
- {
- /**
- * Register services.
- */
- public function register(): void
- {
- $this->registerConfig();
- // GraphQL(API Platform)的 provider / processor 必须注册进容器,
- // 否则 CallableProcessor 的 service locator 找不到它们:
- // "Processor ... not found on operation create"
- $this->app->tag(GiftCartProvider::class, ProviderInterface::class);
- $this->app->tag(GiftCartProcessor::class, ProcessorInterface::class);
- // 复用 Bagisto 原生的营销规则(Marketing > Promotions > Cart Rules):
- // 规则本身的字段不重复造,只追加 free_gift[*] 配置。
- $this->app->bind(CartRuleRequest::class, GiftCartRuleRequest::class);
- $this->app->bind(CartRuleRepository::class, GiftCartRuleRepository::class);
- }
- /**
- * Bootstrap services.
- */
- public function boot(): void
- {
- $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
- $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'free_gift');
- $this->loadViewsFrom(__DIR__.'/../Resources/views', 'free_gift');
- $this->loadRoutesFrom(__DIR__.'/../Routes/shop-routes.php');
- // 后台:赠品 SKU 选择器(商品搜索接口)
- $this->loadRoutesFrom(__DIR__.'/../Routes/admin-routes.php');
- $this->app->register(EventServiceProvider::class);
- // 前台购物车页:赠品 Banner / 挑选弹窗
- Event::listen('bagisto.shop.checkout.cart.header.after', function ($viewRenderEventManager) {
- $viewRenderEventManager->addTemplate('free_gift::shop.cart.gift-container');
- });
- // 前台购物车行:赠品标记
- Event::listen('bagisto.shop.checkout.cart.item_name.after', function ($viewRenderEventManager) {
- $viewRenderEventManager->addTemplate('free_gift::shop.cart.gift-item-badge');
- });
- }
- /**
- * 系统配置项(System Configuration > Sales > Free Gift)。
- */
- protected function registerConfig(): void
- {
- $this->mergeConfigFrom(dirname(__DIR__).'/Config/system.php', 'core');
- }
- }
|