time() - 24 * 60 * 60) { return; } $this->synchronizeShippingMethods(); } public function handle_method_added($instanceId, $methodId, $zoneId): void { $this->synchronizeShippingMethods(); } public function handle_method_deleted($instanceId, $zoneId): void { $this->synchronizeShippingMethods(); } public function deactivate(): void { delete_option('sendy_shipping_methods_last_sync'); } private function synchronizeShippingMethods(): void { $data = []; $zones = \WC_Shipping_Zones::get_zones(); foreach ($zones as $zone) { foreach ($zone['shipping_methods'] as $instanceId => $method) { $data[] = [ 'external_id' => (string) $instanceId, 'name' => "{$zone['zone_name']} - {$method->get_title()}", ]; } } // The 'Rest of the world' zone is treated differently than other zones and will always have '0' as the ID $defaultZone = new \WC_Shipping_Zone(0); foreach ($defaultZone->get_shipping_methods() as $instanceId => $method) { $data[] = [ 'external_id' => (string) $instanceId, 'name' => __('Rest of the world', 'sendy') . " - " . $method->get_title(), ]; } try { (new ShippingMethod())->sync($data); } catch (ApiException $exception) { // TODO Implement logging } update_option('sendy_shipping_methods_last_sync', time()); } }