set( 'settings', static fn(): SettingsRepository => new SettingsRepository() ); $c->set( 'repository', static fn(): OptionExtraRepository => new OptionExtraRepository() ); $c->set( 'migrations', static fn(): Migrations => new Migrations() ); $c->set( 'registry.types', static fn(): TypeRegistry => new TypeRegistry() ); $c->set( 'registry.pricing', static fn( Container $c ): PricingRegistry => new PricingRegistry( $c->get( 'settings' ) ) ); $c->set( 'registry.conditions', static fn(): ConditionRegistry => new ConditionRegistry() ); $c->set( 'registry.locations', static fn(): LocationRegistry => new LocationRegistry() ); $c->set( 'schema', static fn( Container $c ): Schema => new Schema( $c->get( 'registry.types' ), $c->get( 'registry.pricing' ), $c->get( 'registry.conditions' ) ) ); $c->set( 'rules.evaluator', static fn( Container $c ): RuleEvaluator => new RuleEvaluator( $c->get( 'registry.conditions' ) ) ); $c->set( 'pricing.tax', static fn(): TaxHelper => new TaxHelper() ); $c->set( 'pricing.calculator', static fn( Container $c ): PriceCalculator => new PriceCalculator( $c->get( 'registry.types' ), $c->get( 'registry.pricing' ), $c->get( 'pricing.tax' ) ) ); $c->set( 'selection.store', static fn(): SelectionStore => new SelectionStore() ); $c->set( 'context.factory', static fn(): ContextFactory => new ContextFactory() ); $c->set( 'extra.service', static fn( Container $c ): ExtraService => new ExtraService( $c->get( 'repository' ), $c->get( 'registry.types' ), $c->get( 'registry.pricing' ), $c->get( 'registry.locations' ), $c->get( 'rules.evaluator' ), $c->get( 'pricing.calculator' ), $c->get( 'selection.store' ) ) ); $c->set( 'goals.free_shipping', static fn( Container $c ): FreeShippingGoal => new FreeShippingGoal( $c->get( 'settings' ) ) ); $c->set( 'goals.service', static fn( Container $c ): GoalService => new GoalService( $c->get( 'settings' ), $c->get( 'goals.free_shipping' ) ) ); } /** * {@inheritDoc} * * @param Container $c Container. */ public function boot( Container $c ): void { add_action( 'admin_init', static fn() => $c->get( 'migrations' )->maybe_run() ); // Selections live until the cart is emptied (after a successful order). They are not cleared // on order creation so a failed payment in the Checkout block keeps the customer's extras. add_action( 'woocommerce_cart_emptied', static fn() => $c->get( 'selection.store' )->clear() ); // Removing the last product one by one does not call empty_cart(): clear them too, so // the selections never carry over to the next cart of the same session. add_action( 'woocommerce_cart_item_removed', static function ( $cart_item_key, $cart ) use ( $c ): void { if ( $cart instanceof \WC_Cart && $cart->is_empty() ) { $c->get( 'selection.store' )->clear(); } }, 10, 2 ); // Keep the memoised extras in sync if the option is changed by other code (imports, tests). $flush = static function () use ( $c ): void { $repository = $c->get( 'repository' ); if ( $repository instanceof OptionExtraRepository ) { $repository->flush_cache(); } }; add_action( 'add_option_' . OptionExtraRepository::OPTION, $flush ); add_action( 'update_option_' . OptionExtraRepository::OPTION, $flush ); add_action( 'delete_option_' . OptionExtraRepository::OPTION, $flush ); } }