true]); wp_enqueue_script('sendy-checkout', SENDY_WC_PLUGIN_DIR_URL . '/resources/js/checkout.js', ['jquery', 'sendy-api'], Plugin::VERSION, ['in_footer' => true]); } } /** * Render the button to select a pick-up point in the checkout */ public function render_pickup_point_field(): void { if (in_array('sendy_pickup_point', wc_get_chosen_shipping_method_ids())) { if (! is_null(sendy_shipping_method_instance_id())) { $carrier = $this->get_carrier_for_shipping_method(sendy_shipping_method_instance_id()); $selectedPickupPoint = WC()->session->get("sendy_selected_parcelshop_" . sendy_shipping_method_instance_id()); echo wp_kses( View::fromTemplate('checkout/pickup_point_selection.php')->render([ 'carrier' => $carrier, 'instance_id' => sendy_shipping_method_instance_id(), 'selected_pickup_point' => $selectedPickupPoint, ]), View::ALLOWED_TAGS, ); } } } /** * Store the selected pickup point in the session */ public function store_selected_pickup_point_in_session(): void { if (empty($_REQUEST['nonce']) || ! wp_verify_nonce(sanitize_key($_REQUEST['nonce']), 'sendy_store_pickup_point')) { wp_send_json_error('Nonce verification failed'); } $data = [ 'id' => sanitize_text_field(wp_unslash($_REQUEST['id'] ?? '')), 'name' => sanitize_text_field(wp_unslash($_REQUEST['name'] ?? '')), 'street' => sanitize_text_field(wp_unslash($_REQUEST['street'] ?? '')), 'number' => sanitize_text_field(wp_unslash($_REQUEST['number'] ?? '')), 'postal_code' => sanitize_text_field(wp_unslash($_REQUEST['postal_code'] ?? '')), 'city' => sanitize_text_field(wp_unslash($_REQUEST['city'] ?? '')), ]; WC()->session->set('sendy_selected_parcelshop_' . sanitize_key($_REQUEST['instance_id'] ?? ''), $data); wp_send_json_success(); } /** * Add the selected pickup point to the order metadata */ public function store_selected_pickup_point_in_order(\WC_Order $order): void { $instanceId = sendy_shipping_method_instance_id(); if ($data = WC()->session->get("sendy_selected_parcelshop_{$instanceId}")) { $order->update_meta_data('_sendy_pickup_point_id', $data['id']); $order->update_meta_data('_sendy_pickup_point_data', $data); WC()->session->set("sendy_selected_parcelshop_{$instanceId}", null); } } /** * Display the address of the selected pickup point on the order confirmation page */ public function show_selected_pickup_point_on_confirmation(string $addressType, \WC_Order $order): void { if ($addressType === 'shipping' && $order->meta_exists('_sendy_pickup_point_id')) { echo wp_kses( View::fromTemplate('checkout/order_confirmation.php')->render([ 'pickup_point' => $order->get_meta('_sendy_pickup_point_data'), ]), View::ALLOWED_TAGS, ); } } /** * Validate if the pickup point is selected */ public function validate_pickup_point(array $fields, WP_Error $errors): void { if (in_array('sendy_pickup_point', wc_get_chosen_shipping_method_ids())) { $instanceId = sendy_shipping_method_instance_id(); if (empty($instanceId)) { $errors->add('sendy_pickup_point_error', __('Please select a pick-up point.', 'sendy')); } else { $selectedPickupPoint = WC()->session->get("sendy_selected_parcelshop_{$instanceId}"); if (empty($selectedPickupPoint)) { $errors->add('sendy_pickup_point_error', __('Please select a pick-up point.', 'sendy')); } } } } /** * Get the carrier for the shipping method * * This method is used to load the correct pick-up points for a carrier in the checkout */ private function get_carrier_for_shipping_method(int $instance_id): ?string { return (new PickupPointDelivery($instance_id))->get_option('carrier'); } }