[{"data":1,"prerenderedAt":31},["ShallowReactive",2],{"$f2xqp7inh72n5t":3,"$fgph23vl6tmr9":23},{"content":4,"package":5,"version":22},"# Hooking the Estimator\n\nWhen a user submits the form to request a shipping estimate, the controller will dispatch an event that allows developers to manipulate the address that will be used for the estimate or cancel the request.\n\nThe `BabDev\\SyliusShippingEstimatePlugin\\Event\\BeforeEstimateShippingEvent` event is dispatched, which contains the current cart and the address data from the estimate form.\n\n## Examples\n\n### Adding Data To The Address\n\nA dynamic shipping calculator may require more data than just the country and postal code, so an event listener may be used to add extra data to the address.\n\n```php\n\u003C?php\n\nnamespace App\\EventListener;\n\nuse App\\Service\\ShippingApisService;\nuse BabDev\\SyliusShippingEstimatePlugin\\Event\\BeforeEstimateShippingEvent;\n\nfinal class AddProvinceToShippingEstimateAddressListener\n{\n    private ShippingApisService $shippingApisService;\n\n    public function __construct(ShippingApisService $shippingApisService)\n    {\n        $this->shippingApisService = $shippingApisService;\n    }\n\n    public function __invoke(BeforeEstimateShippingEvent $event): void\n    {\n        \u002F\u002F Only applies to US customers\n        if ($event->getAddress()->getCountryCode() !== 'US') {\n            return;\n        }\n\n        \u002F\u002F Uses the shippingapis.com API to get the state for the given postal code, stripping off the 4-digit suffix\n        $state = $this->shippingApisService->getState(substr(trim($event->getAddress()->getPostcode()), 0, 5));\n\n        $event->getAddress()->setProvinceCode('US-' . $state);\n    }\n}\n```\n\n### Cancelling The Request\n\nA shipping estimate can be cancelled by calling the plugin's `cancelEstimate` method, providing a reason for the cancellation. Note, the reason will be displayed on the site frontend with the default JavaScript integration.\n\n```php\n\u003C?php\n\nnamespace App\\EventListener;\n\nuse BabDev\\SyliusShippingEstimatePlugin\\Event\\BeforeEstimateShippingEvent;\n\nfinal class CancelShippingEstimatesForUnitedStatesCustomersListener\n{\n    public function __invoke(BeforeEstimateShippingEvent $event): void\n    {\n        \u002F\u002F Only applies to US customers\n        if ($event->getAddress()->getCountryCode() !== 'US') {\n            return;\n        }\n\n        $event->cancelEstimate('Sorry, we do not provide shipping estimates for US customers.');\n    }\n}\n```\n",{"name":6,"slug":7,"previousSlugs":8,"description":10,"github":11,"packagistName":14,"packageType":15,"hasDocumentation":16,"supported":16,"visible":16,"versions":17},"Sylius Shipping Estimate Plugin","shipping-estimate-plugin",[9],"syliusshippingestimateplugin","Adds a shipping estimator widget to a Sylius website's cart",{"owner":12,"repo":13},"BabDev","SyliusShippingEstimatePlugin","babdev\u002Fsylius-shipping-estimate-plugin","sylius-plugin",true,[18],{"version":19,"gitBranch":20,"released":21},"1.x","0.3",false,{"version":19,"gitBranch":20,"released":21},{"content":24,"package":25,"version":30},"- [Introduction](\u002Fopen-source\u002Fpackages\u002Fshipping-estimate-plugin\u002Fdocs\u002F1.x\u002Fintro)\n- [Installation & Setup](\u002Fopen-source\u002Fpackages\u002Fshipping-estimate-plugin\u002Fdocs\u002F1.x\u002Finstallation)\n- [Customize the Output](\u002Fopen-source\u002Fpackages\u002Fshipping-estimate-plugin\u002Fdocs\u002F1.x\u002Fcustomize-the-output)\n- [Hooking The Estimator](\u002Fopen-source\u002Fpackages\u002Fshipping-estimate-plugin\u002Fdocs\u002F1.x\u002Fhooking-the-estimator)\n- [Rate Limiting](\u002Fopen-source\u002Fpackages\u002Fshipping-estimate-plugin\u002Fdocs\u002F1.x\u002Frate-limiting)\n",{"name":6,"slug":7,"previousSlugs":26,"description":10,"github":27,"packagistName":14,"packageType":15,"hasDocumentation":16,"supported":16,"visible":16,"versions":28},[9],{"owner":12,"repo":13},[29],{"version":19,"gitBranch":20,"released":21},{"version":19,"gitBranch":20,"released":21},1790007747520]