paczkomaty = $paczkomaty; add_filter( 'flexible_shipping_integration_options', array( $this, 'flexible_shipping_integration_options', ), 10 ); add_filter( 'flexible_shipping_method_settings', array( $this, 'flexible_shipping_method_settings', ), 10, 2 ); add_action( 'flexible_shipping_method_script', array( $this, 'flexible_shipping_method_script' ) ); add_filter( 'flexible_shipping_process_admin_options', array( $this, 'flexible_shipping_process_admin_options', ), 10, 1 ); add_filter( 'flexible_shipping_method_integration_col', array( $this, 'flexible_shipping_method_integration_col', ), 10, 2 ); add_filter( 'flexible_shipping_method_rate_id', array( $this, 'flexible_shipping_method_rate_id' ), 10, 2 ); add_filter( 'flexible_shipping_add_method', array( $this, 'flexible_shipping_add_method' ), 10, 3 ); add_filter( 'flexible_shipping_shipping_data', array( $this, 'flexible_shipping_shipping_data' ), 10, 2 ); add_filter( 'flexible_shipping_bulk_send', array( $this, 'flexible_shipping_bulk_send' ), 10, 2 ); add_filter( 'flexible_shipping_bulk_label', array( $this, 'flexible_shipping_bulk_label' ), 10, 2 ); add_filter( 'flexible_shipping_shipment_post_title_paczkomaty', array( $this, 'flexible_shipping_shipment_post_title_paczkomaty', ), 10, 2 ); } /** * Returns paczkomaty_shipping_method * * @return WC_Paczkomaty_Shipping_Method */ private function get_shipping_method() { $all_shipping_methods = WC()->shipping()->get_shipping_methods(); /** @var WC_Paczkomaty_Shipping_Method $paczkomaty_shipping_method */ // phpcs:ignore $paczkomaty_shipping_method = $all_shipping_methods['paczkomaty_shipping_method']; return $paczkomaty_shipping_method; } /** * . * * @return WPDesk_Paczkomaty_ShipX */ private function get_shipX() { // phpcs:ignore return $this->get_shipping_method()->get_shipX(); } /** * . * * @param array $options . * * @return array */ public function flexible_shipping_integration_options( $options ) { $options['paczkomaty'] = __( 'InPost', 'woocommerce-paczkomaty-inpost' ); return $options; } /** * . * * @param array $shipping_method . * @param string $checkbox_field . * * @return array */ private function prepare_checkbox_custom_attributes( $shipping_method, $checkbox_field ) { return isset( $shipping_method[ $checkbox_field ] ) && 1 === intval( $shipping_method[ $checkbox_field ] ) ? array( 'checked' => 'checked' ) : array(); } /** * . * * @param array $flexible_shipping_settings . * @param array $shipping_method . * * @return array */ public function flexible_shipping_method_settings( $flexible_shipping_settings, $shipping_method ) { $paczkomaty_shipping_method = $this->get_shipping_method(); $flexible_shipping_settings['method_integration']['options']['paczkomaty'] = __( 'InPost', 'woocommerce-paczkomaty-inpost' ); $package_sizes = new WPDesk_Paczkomaty_Package_Sizes(); $usluga_options = [ 'paczkomaty' => __( 'Paczkomat', 'woocommerce-paczkomaty-inpost' ), 'kurier' => __( 'Kurier', 'woocommerce-paczkomaty-inpost' ), ]; if ( isset( $shipping_method ) && isset( $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] ) && 'polecony' === $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] ) { $usluga_options['polecony'] = __( 'Paczka E-Commerce (wyłączona)', 'woocommerce-paczkomaty-inpost' ); } $organization = false; $ship_x = $this->get_shipX(); if ( ! empty( $ship_x ) ) { try { $organization = $ship_x->get_organization( $paczkomaty_shipping_method->get_option( 'organization_id' ) ); } catch ( Exception $e ) { // phpcs:ignore // do nothing. } } $kurier_usluga_options = []; if ( 'yes' === $paczkomaty_shipping_method->get_option( 'paczkomaty', 'no' ) ) { $usluga_options['paczkomaty'] = __( 'Paczkomat', 'woocommerce-paczkomaty-inpost' ); } else { if ( $organization && ! empty( $organization->services ) && is_array( $organization->services ) ) { if ( in_array( 'inpost_locker_standard', $organization->services, true ) ) { $usluga_options['paczkomaty'] = __( 'Paczkomat', 'woocommerce-paczkomaty-inpost' ); } if ( in_array( 'inpost_courier_standard', $organization->services, true ) ) { $usluga_options['kurier'] = __( 'Kurier', 'woocommerce-paczkomaty-inpost' ); $kurier_usluga_options[ self::STANDARD ] = __( 'Kurier Standard', 'woocommerce-paczkomaty-inpost' ); } if ( in_array( 'inpost_courier_c2c', $organization->services, true ) ) { $usluga_options['kurier'] = __( 'Kurier', 'woocommerce-paczkomaty-inpost' ); $kurier_usluga_options[ self::STANDARD_C2C ] = __( 'Kurier Standard C2C', 'woocommerce-paczkomaty-inpost' ); } } else { $kurier_usluga_options = [ self::STANDARD_C2C => __( 'Kurier Standard C2C', 'woocommerce-paczkomaty-inpost' ), self::STANDARD => __( 'Kurier Standard', 'woocommerce-paczkomaty-inpost' ), ]; } } $settings = [ self::SETTING_PACZKOMATY_USLUGA => [ 'title' => __( 'Usługa InPost', 'woocommerce-paczkomaty-inpost' ), 'type' => 'select', 'default' => isset( $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] ) ? $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] : '', 'options' => $usluga_options, ], self::SETTING_PACZKOMATY_KURIER_USLUGA => [ 'title' => __( 'Usługa Kurier', 'woocommerce-paczkomaty-inpost' ), 'type' => 'select', 'default' => isset( $shipping_method[ self::SETTING_PACZKOMATY_KURIER_USLUGA ] ) ? $shipping_method[ self::SETTING_PACZKOMATY_KURIER_USLUGA ] : '', 'options' => $kurier_usluga_options, ], self::SETTING_PACZKOMATY_PACKAGE_SIZE => [ 'title' => __( 'Rozmiar paczki', 'woocommerce-paczkomaty-inpost' ), 'type' => 'select', 'desc_tip' => __( 'Wybierz rozmiar przesyłki paczkomatowej, który będzie domyślnie stosowany dla tej metody wysyłki. Wybrana w tym miejscu opcja może zostać później zmieniona z poziomu metaboxa InPost na ekranie szczegółów zamówienia.', 'woocommerce-paczkomaty-inpost' ), 'default' => isset( $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_SIZE ] ) ? $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_SIZE ] : '', 'options' => $package_sizes->get_package_sizes_with_select_label( __( 'Pobierz z Ustawień głównych wtyczki InPost', 'woocommerce-paczkomaty-inpost' ) ), ], self::SETTING_PACZKOMATY_PACKAGE_LENGTH => [ 'title' => __( 'Długość paczki [mm]', 'woocommerce-paczkomaty-inpost' ), 'type' => 'number', 'default' => isset( $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_LENGTH ] ) ? $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_LENGTH ] : '', ], self::SETTING_PACZKOMATY_PACKAGE_WIDTH => [ 'title' => __( 'Szerokość paczki [mm]', 'woocommerce-paczkomaty-inpost' ), 'type' => 'number', 'default' => isset( $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_WIDTH ] ) ? $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_WIDTH ] : '', ], self::SETTING_PACZKOMATY_PACKAGE_HEIGHT => [ 'title' => __( 'Wysokość paczki [mm]', 'woocommerce-paczkomaty-inpost' ), 'type' => 'number', 'default' => isset( $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_HEIGHT ] ) ? $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_HEIGHT ] : '', ], self::SETTING_PACZKOMATY_PACKAGE_DEFAULT_WEIGHT => [ 'title' => __( 'Domyślna waga paczki [kg]', 'woocommerce-paczkomaty-inpost' ), 'type' => 'number', 'custom_attributes' => [ 'min' => 0, 'step' => '0.001', ], 'desc_tip' => __( 'Wprowadź domyślną wagę przesyłki, która zostanie użyta, jeśli dla zamawianych produktów nie została uzupełniona indywidualna waga każdego z nich.', 'woocommerce-paczkomaty-inpost' ), 'default' => isset( $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_DEFAULT_WEIGHT ] ) ? $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_DEFAULT_WEIGHT ] : '', ], self::SETTING_UBEZPIECZENIE => [ 'title' => __( 'Ubezpieczenie', 'woocommerce-paczkomaty-inpost' ), 'type' => 'checkbox', 'custom_attributes' => $this->prepare_checkbox_custom_attributes( $shipping_method, self::SETTING_UBEZPIECZENIE ), ], self::SETTING_COD => [ 'title' => __( 'Pobranie', 'woocommerce-paczkomaty-inpost' ), 'type' => 'checkbox', 'custom_attributes' => $this->prepare_checkbox_custom_attributes( $shipping_method, self::SETTING_COD ), 'description' => sprintf( // Translators: docs link. __( 'Dowiedz się %1$sjak poprawnie skonfigurować w swoim sklepie wysyłkę za pobraniem z wykorzystaniem wtyczek Flexible Shipping i InPost WooCommerce →%2$s', 'woocommerce-paczkomaty-inpost' ), '', '' ), ], self::SETTING_END_OF_WEEK_COLLECTION => [ 'title' => __( 'Paczka w Weekend', 'woocommerce-paczkomaty-inpost' ), 'type' => 'checkbox', 'custom_attributes' => $this->prepare_checkbox_custom_attributes( $shipping_method, self::SETTING_END_OF_WEEK_COLLECTION ), 'description' => sprintf( // Translators: docs link. __( 'Zaoferuj swoim klientom dostawę paczek do paczkomatów w weekend. Dowiedz się %1$sjak to działa →%2$s', 'woocommerce-paczkomaty-inpost' ), '', '' ), ], self::SETTING_SMS_NOTIFICATION => [ 'title' => __( 'Powiadomienie SMS', 'woocommerce-paczkomaty-inpost' ), 'type' => 'checkbox', 'custom_attributes' => $this->prepare_checkbox_custom_attributes( $shipping_method, self::SETTING_SMS_NOTIFICATION ), 'description' => __( 'Klient otrzyma powiadomienia SMS z informacją o aktualnym statusie doręczenia przesyłki', 'woocommerce-paczkomaty-inpost' ), ], self::SETTING_EMAIL_NOTIFICATION => [ 'title' => __( 'Powiadomienie E-mail', 'woocommerce-paczkomaty-inpost' ), 'type' => 'checkbox', 'custom_attributes' => $this->prepare_checkbox_custom_attributes( $shipping_method, self::SETTING_EMAIL_NOTIFICATION ), 'description' => __( 'Klient otrzyma powiadomienia E-mail z informacją o aktualnym statusie doręczenia przesyłki', 'woocommerce-paczkomaty-inpost' ), ], self::SETTING_SATURDAY => [ 'title' => __( 'Doręczenie w sobotę', 'woocommerce-paczkomaty-inpost' ), 'type' => 'checkbox', 'custom_attributes' => $this->prepare_checkbox_custom_attributes( $shipping_method, self::SETTING_SATURDAY ), 'description' => __( 'Przesyłka będzie mogła zostać doręczona w sobotę, jeżeli zostanie nadana w piątek do miast z oddziałami sieci kurierskiej InPost.', 'woocommerce-paczkomaty-inpost' ), ], ]; foreach ( $settings as $field => $field_setting ) { $settings[ $field ]['class'] = self::SHIPPING_METHOD_INTEGRATION; } return array_merge( $flexible_shipping_settings, $settings ); } /** * . */ public function flexible_shipping_method_script() { include __DIR__ . '/views/flexible-shipping-method-script.php'; } /** * . * * @param array $post_data . * @param string $option_name . * * @return int */ private function process_checkbox_option( $post_data, $option_name ) { return isset( $post_data[ 'woocommerce_flexible_shipping_' . $option_name ] ) ? sanitize_text_field( $post_data[ 'woocommerce_flexible_shipping_' . $option_name ] ) : '0'; } /** * Process options for shipping method. * * @param array $shipping_method . * * @return array */ public function flexible_shipping_process_admin_options( $shipping_method ) { $post_data = $_POST; // phpcs:ignore if ( isset( $post_data['woocommerce_flexible_shipping_paczkomaty_usluga'] ) ) { $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] = sanitize_text_field( $post_data['woocommerce_flexible_shipping_paczkomaty_usluga'] ); } if ( isset( $post_data['woocommerce_flexible_shipping_paczkomaty_package_size'] ) ) { $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_SIZE ] = sanitize_text_field( $post_data['woocommerce_flexible_shipping_paczkomaty_package_size'] ); } if ( isset( $post_data['woocommerce_flexible_shipping_paczkomaty_package_width'] ) ) { $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_WIDTH ] = sanitize_text_field( $post_data['woocommerce_flexible_shipping_paczkomaty_package_width'] ); } if ( isset( $post_data['woocommerce_flexible_shipping_paczkomaty_package_height'] ) ) { $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_HEIGHT ] = sanitize_text_field( $post_data['woocommerce_flexible_shipping_paczkomaty_package_height'] ); } if ( isset( $post_data['woocommerce_flexible_shipping_paczkomaty_package_length'] ) ) { $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_LENGTH ] = sanitize_text_field( $post_data['woocommerce_flexible_shipping_paczkomaty_package_length'] ); } if ( isset( $post_data['woocommerce_flexible_shipping_paczkomaty_package_default_weight'] ) ) { $shipping_method[ self::SETTING_PACZKOMATY_PACKAGE_DEFAULT_WEIGHT ] = sanitize_text_field( $post_data['woocommerce_flexible_shipping_paczkomaty_package_default_weight'] ); } if ( isset( $post_data['woocommerce_flexible_shipping_paczkomaty_kurier_usluga'] ) ) { $shipping_method[ self::SETTING_PACZKOMATY_KURIER_USLUGA ] = sanitize_text_field( $post_data['woocommerce_flexible_shipping_paczkomaty_kurier_usluga'] ); } $shipping_method[ self::SETTING_UBEZPIECZENIE ] = $this->process_checkbox_option( $post_data, self::SETTING_UBEZPIECZENIE ); $shipping_method[ self::SETTING_COD ] = $this->process_checkbox_option( $post_data, self::SETTING_COD ); $shipping_method[ self::SETTING_END_OF_WEEK_COLLECTION ] = $this->process_checkbox_option( $post_data, self::SETTING_END_OF_WEEK_COLLECTION ); $shipping_method[ self::SETTING_SMS_NOTIFICATION ] = $this->process_checkbox_option( $post_data, self::SETTING_SMS_NOTIFICATION ); $shipping_method[ self::SETTING_EMAIL_NOTIFICATION ] = $this->process_checkbox_option( $post_data, self::SETTING_EMAIL_NOTIFICATION ); $shipping_method[ self::SETTING_SATURDAY ] = $this->process_checkbox_option( $post_data, self::SETTING_SATURDAY ); return $shipping_method; } /** * . * * @param string $col . * @param array $shipping_method . * * @return string */ public function flexible_shipping_method_integration_col( $col, $shipping_method ) { $shipping_methods = WC()->shipping->get_shipping_methods(); if ( 'yes' === $shipping_methods['paczkomaty_shipping_method']->enabled ) { if ( isset( $shipping_method['method_integration'] ) && 'paczkomaty' === $shipping_method['method_integration'] ) { ob_start(); include __DIR__ . '/views/flexible-shipping-method-integration-col.php'; $col = ob_get_contents(); ob_end_clean(); } } return $col; } /** * . * * @param string $rate_id . * @param array $shipping_method . * * @return string */ public function flexible_shipping_method_rate_id( $rate_id, $shipping_method ) { if ( isset( $shipping_method['method_integration'] ) && 'paczkomaty' === $shipping_method['method_integration'] ) { $rate_id = $rate_id . '_paczkomaty'; if ( isset( $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] ) && 'polecony' === $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] ) { $rate_id = $rate_id . '_paczkomaty_polecony'; } else { $rate_id = $rate_id . '_paczkomaty_paczkomaty'; if ( isset( $shipping_method[ self::SETTING_UBEZPIECZENIE ] ) ) { $rate_id .= '_u' . $shipping_method[ self::SETTING_UBEZPIECZENIE ]; } if ( isset( $shipping_method[ self::SETTING_COD ] ) ) { $rate_id .= '_cod' . $shipping_method[ self::SETTING_COD ]; } } } return $rate_id; } /** * . * * @param bool $add_method . * @param array $shipping_method . * @param array $package . * * @return bool */ public function flexible_shipping_add_method( $add_method, $shipping_method, $package ) { if ( isset( $shipping_method['method_integration'] ) && 'paczkomaty' === $shipping_method['method_integration'] && isset( $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] ) ) { if ( 'polecony' === $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] ) { if ( 'PL' !== $package['destination']['country'] ) { return false; } } foreach ( $package['contents'] as $item ) { if ( 'paczkomaty' === $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] && 'yes' === wpdesk_get_product_meta( $item['product_id'], '_paczkomat_disabled', true ) ) { return false; } if ( 'polecony' === $shipping_method[ self::SETTING_PACZKOMATY_USLUGA ] && 'yes' === wpdesk_get_product_meta( $item['product_id'], '_paczkomat_polecony_disabled', true ) ) { return false; } } } return $add_method; } /** * . * * @param array $shippings . * * @return array */ public function flexible_shipping_shipping_data( $shippings, $order = null ) { global $the_order; $order = wc_get_order( $order ?? $the_order ); if ( ! $order ) { return $shippings; } $paczkomaty_usluga = wpdesk_get_order_meta( $order, '_paczkomaty_usluga', true ); if ( '' !== $paczkomaty_usluga ) { $shipping = array(); $shipping['order_id'] = $order->get_id(); $shipping['integration'] = 'paczkomaty_shipping_method'; $shipping['url'] = admin_url( 'post.php?post=' . $order->get_id() . '&action=edit#woocommerce-paczkomaty-box-new' ); $shipping['error'] = wpdesk_get_order_meta( $order, '_paczkomat_error', true ); $paczkomat_packcode = wpdesk_get_order_meta( $order, '_paczkomat_packcode', true ); if ( '' === $paczkomat_packcode ) { $shipping['status'] = 'new'; if ( 'kurier' === $paczkomaty_usluga && '' !== wpdesk_get_order_meta( $order, '_paczkomat_shipment_status', true ) ) { $shipping['status'] = wpdesk_get_order_meta( $order, '_paczkomat_shipment_status', true ); } } else { $shipping['status'] = 'confirmed'; $shipping['tracking_number'] = $paczkomat_packcode; $shipping['paczkomat_packcode'] = $paczkomat_packcode; $shipping['label_url'] = wp_nonce_url( admin_url( '?generate_inpost_sticker=true&post=' . $order->get_id() ), 'generate_inpost_sticker' ); $shipping['tracking_url'] = 'https://inpost.pl/pl/pomoc/znajdz-przesylke?parcel=' . $paczkomat_packcode; } if ( '' !== $shipping['error'] ) { $shipping['status'] = 'error'; } $shippings[] = $shipping; } return $shippings; } /** * . * * @param array $message . * @param int $post_id . * * @return array */ public function flexible_shipping_bulk_send( $message, $post_id ) { $order = wc_get_order( $post_id ); $poczkomaty_usluga = wpdesk_get_order_meta( $order, '_paczkomaty_usluga', true ); if ( '' !== $poczkomaty_usluga ) { try { $this->paczkomaty->send_package( $order ); return array( 'status' => 'created', 'message' => __( 'Przesyłka została utworzona.', 'woocommerce-paczkomaty-inpost' ), ); } catch ( Exception $e ) { return array( 'status' => 'error', 'message' => $e->getMessage(), ); } } return $message; } /** * . * * @param array $labels . * @param int $post_id . * * @return array */ public function flexible_shipping_bulk_label( $labels, $post_id ) { $order = wc_get_order( $post_id ); $poczkomaty_usluga = wpdesk_get_order_meta( $order, '_paczkomaty_usluga', true ); if ( '' !== $poczkomaty_usluga ) { try { $packcode = wpdesk_get_order_meta( $order, '_paczkomat_packcode', true ); $label = $this->paczkomaty->get_label( $order ); $labels[] = array( 'status' => 'created', 'message' => __( 'Etykieta została pobrana.', 'woocommerce-paczkomaty-inpost' ), 'content' => $label['content'], 'file_name' => $label['file_name'], ); } catch ( Exception $e ) { $labels[] = array( 'status' => 'error', 'message' => $e->getMessage(), ); } } return $labels; } /** * . * * @param string $post_title . * @param array $fs_method . * * @return string */ public function flexible_shipping_shipment_post_title_paczkomaty( $post_title, $fs_method ) { $post_title = $post_title . ', ' . $fs_method[ self::SETTING_PACZKOMATY_USLUGA ]; return $post_title; } } }