1191 lines
49 KiB
PHP
1191 lines
49 KiB
PHP
<?php
|
|
|
|
use FS\InPost\PickupPoints\PickupPointsManager;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
} // Exit if accessed directly
|
|
|
|
require_once __DIR__ . '/shipX/class-api-shipx-status-translation.php';
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
class WPDesk_Paczkomaty {
|
|
|
|
/**
|
|
* @var WPDesk_Paczkomaty_Plugin
|
|
*/
|
|
private $plugin;
|
|
|
|
/**
|
|
* @var WPDesk_Paczkomaty_ShipX
|
|
*/
|
|
private $shipX = null; // phpcs:ignore
|
|
|
|
/**
|
|
* @var PickupPointsManager
|
|
*/
|
|
private $pickup_points_manager;
|
|
|
|
/**
|
|
* WPDesk_Paczkomaty constructor.
|
|
*
|
|
* @param WPDesk_Paczkomaty_Plugin $plugin .
|
|
* @param PickupPointsManager $pickup_points_manager .
|
|
*/
|
|
public function __construct( WPDesk_Paczkomaty_Plugin $plugin, PickupPointsManager $pickup_points_manager ) {
|
|
$this->plugin = $plugin;
|
|
$this->pickup_points_manager = $pickup_points_manager;
|
|
|
|
$this->hooks();
|
|
}
|
|
|
|
/**
|
|
* Init hooks
|
|
*/
|
|
public function hooks() {
|
|
|
|
add_action( 'admin_init', [ $this, 'adminInit' ] );
|
|
|
|
add_action( 'woocommerce_order_details_after_order_table', [ $this, 'orderItemsTable' ] );
|
|
|
|
add_action( 'woocommerce_email_order_meta', [ $this, 'orderItemsTable' ], 195 );
|
|
|
|
add_action( 'add_meta_boxes', [ $this, 'addAdminBox' ] );
|
|
|
|
add_action( 'wp_ajax_paczkomaty_save_package', [ $this, 'wp_ajax_paczkomaty_save_package' ] );
|
|
add_action( 'wp_ajax_paczkomaty_send_package', [ $this, 'wp_ajax_paczkomaty_send_package' ] );
|
|
|
|
add_action(
|
|
'wp_ajax_paczkomaty_refresh_package_status',
|
|
[
|
|
$this,
|
|
'wp_ajax_paczkomaty_refresh_package_status',
|
|
]
|
|
);
|
|
|
|
add_action( 'wp_ajax_paczkomaty_xml_api_notice', [ $this, 'wp_ajax_paczkomaty_xml_api_notice' ] );
|
|
|
|
add_action( 'template_redirect', [ $this, 'shipx_webhook' ] );
|
|
|
|
add_action( 'woocommerce_order_status_changed', [ $this, 'woocommerce_order_status_changed' ], 10, 3 );
|
|
|
|
add_action(
|
|
'paczkomaty_shipping_method_shipment_created',
|
|
[
|
|
$this,
|
|
'paczkomaty_shipping_method_shipment_created',
|
|
],
|
|
99999999
|
|
);
|
|
|
|
add_filter(
|
|
'paczkomaty_shipping_method_pickup_points',
|
|
[
|
|
$this,
|
|
'paczkomaty_shipping_method_pickup_points',
|
|
]
|
|
);
|
|
|
|
add_filter(
|
|
'flexible_shipping_add_shipping_options',
|
|
[
|
|
$this,
|
|
'flexible_shipping_add_shipping_options',
|
|
]
|
|
);
|
|
|
|
add_action( 'admin_notices', [ $this, 'admin_notices' ] );
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
public function wp_ajax_paczkomaty_xml_api_notice() {
|
|
update_option( $this->plugin->get_namespace() . '_xmlapi_dismiss', 1 );
|
|
}
|
|
|
|
/**
|
|
* @return WC_Paczkomaty_Shipping_Method .
|
|
*/
|
|
public function get_shipping_method(): WC_Paczkomaty_Shipping_Method {
|
|
$all_shipping_methods = WC()->shipping()->get_shipping_methods();
|
|
if ( ! isset( $all_shipping_methods['paczkomaty_shipping_method'] ) ) {
|
|
WC()->shipping()->load_shipping_methods();
|
|
}
|
|
|
|
return $all_shipping_methods['paczkomaty_shipping_method'] ?? new WC_Paczkomaty_Shipping_Method();
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
public function admin_notices() {
|
|
if ( defined( 'WC_VERSION' ) ) {
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
$dismiss_notice = (int) get_option( $this->plugin->get_namespace() . '_xmlapi_dismiss', '0' );
|
|
if ( $paczkomaty_shipping_method instanceof WC_Paczkomaty_Shipping_Method
|
|
&& $paczkomaty_shipping_method->get_option( 'paczkomaty', 'no' ) === 'yes'
|
|
&& 0 === $dismiss_notice
|
|
) {
|
|
$out = sprintf(
|
|
// translators: open tag, close tag.
|
|
__( 'InPost: API XML zostanie wyłączone przez InPost! Rekomendujemy uzyskanie dostępu do nowego API ShipX u opiekuna konta InPost. Nowe API oferuje więcej możliwości, %1$ssprawdź%2$s.', 'woocommerce-paczkomaty-inpost' ),
|
|
'<a target="_blank" href="https://www.wpdesk.pl/blog/api-shipx-api-xml/">',
|
|
'</a>'
|
|
);
|
|
|
|
$class = 'error notice is-dismissible paczkomaty-xml-api-notice';
|
|
echo '<div class="' . esc_attr( $class ) . '"> <p>' . wp_kses_post( $out ) . '</p></div>';
|
|
|
|
new \VendorInPost\WPDesk\Notice\Notice(
|
|
sprintf(
|
|
// Translators: link.
|
|
__( 'InPost: API XML wymaga conajmniej 512MB pamięci dostępnej dla procesów PHP. Rekomendujemy uzyskanie dostępu do nowego API ShipX u opiekuna konta InPost. Nowe API oferuje więcej możliwości, %1$ssprawdź%2$s.', 'woocommerce-paczkomaty-inpost' ),
|
|
'<a target="_blank" href="https://www.wpdesk.pl/blog/api-shipx-api-xml/">',
|
|
'</a>'
|
|
),
|
|
'error'
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*/
|
|
public function paczkomaty_shipping_method_shipment_created( WC_Order $order ) {
|
|
$shipping_method = $this->get_shipping_method();
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'confirmed' );
|
|
if ( $shipping_method->get_option( 'complete_order', 'no' ) === 'yes' ) {
|
|
$order->update_status( 'completed', __( 'Status zmieniony automatycznie po utworzeniu przesyłki - wtyczka InPost.', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $pickup_points .
|
|
*
|
|
* @return array|mixed
|
|
*/
|
|
public function paczkomaty_shipping_method_pickup_points( $pickup_points ) {
|
|
try {
|
|
if ( $this->get_shipping_method()->get_parcel_lockers_api() === WC_Paczkomaty_Shipping_Method::API_TYPE_XML ) {
|
|
return inpost_listmachines( 't' );
|
|
} else {
|
|
return $this->pickup_points_manager->get_points();
|
|
}
|
|
} catch ( Exception $e ) {
|
|
return $pickup_points;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return WPDesk_Paczkomaty_ShipX
|
|
*/
|
|
public function get_shipX() { // phpcs:ignore
|
|
if ( $this->shipX === null ) { // phpcs:ignore
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
$test_mode = true;
|
|
|
|
if ( $paczkomaty_shipping_method->get_option( 'test_mode', '' ) !== 'yes' ) {
|
|
$test_mode = false;
|
|
}
|
|
|
|
$organization_id = $paczkomaty_shipping_method->get_option( 'organization_id', '' );
|
|
$this->shipX = new WPDesk_Paczkomaty_ShipX( $paczkomaty_shipping_method->get_option( 'token', '' ), $paczkomaty_shipping_method->get_option( 'token_test_mode', '' ), $test_mode ); // phpcs:ignore
|
|
$this->shipX->set_organization_id( $organization_id ); // phpcs:ignore
|
|
}
|
|
|
|
return $this->shipX; // phpcs:ignore
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function send_package( WC_Order $order ) {
|
|
$poczkomaty_usluga = wpdesk_get_order_meta( $order, '_paczkomaty_usluga', true );
|
|
if ( $poczkomaty_usluga !== '' ) {
|
|
if ( $poczkomaty_usluga === 'paczkomaty' || $poczkomaty_usluga === 'polecony' ) {
|
|
if ( wpdesk_get_order_meta( $order, '_paczkomat_packcode', true ) !== '' ) {
|
|
throw new Exception( __( 'Przesyłka została już utworzona.', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
}
|
|
if ( $poczkomaty_usluga === 'kurier' ) {
|
|
if ( wpdesk_get_order_meta( $order, '_paczkomat_shipment_status', true ) !== '' ) {
|
|
throw new Exception( __( 'Przesyłka została już utworzona.', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
}
|
|
if ( $poczkomaty_usluga === 'paczkomaty' ) {
|
|
$this->send_paczkomaty( $order );
|
|
do_action( 'paczkomaty_shipping_method_shipment_created', $order );
|
|
}
|
|
if ( $poczkomaty_usluga === 'polecony' ) {
|
|
$this->send_polecony( $order );
|
|
do_action( 'paczkomaty_shipping_method_shipment_created', $order );
|
|
}
|
|
if ( $poczkomaty_usluga === 'kurier' ) {
|
|
$this->send_kurier( $order );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'created' );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $order_id .
|
|
* @param $old_status .
|
|
* @param $new_status .
|
|
*/
|
|
public function woocommerce_order_status_changed( $order_id, $old_status, $new_status ) {
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
$settings = $paczkomaty_shipping_method->settings;
|
|
if ( isset( $settings['auto_create'] ) && $settings['auto_create'] === 'auto' ) {
|
|
if ( isset( $settings['order_status'] ) && 'wc-' . $new_status === $settings['order_status'] ) {
|
|
$order = wc_get_order( $order_id );
|
|
$this->send_all_paczkomaty_shipments( $order );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sends all paczkomaty shipments in order.
|
|
*
|
|
* @param WC_Order $order .
|
|
*/
|
|
private function send_all_paczkomaty_shipments( WC_Order $order ) {
|
|
$shipments = fs_get_order_shipments( $order->get_id(), 'paczkomaty' );
|
|
|
|
$all_sent = true;
|
|
|
|
/** @var WPDesk_Flexible_Shipping_Shipment_Paczkomaty $shipment */
|
|
foreach ( $shipments as $shipment ) {
|
|
try {
|
|
$shipment->api_create();
|
|
} catch ( Exception $e ) {
|
|
$all_sent = false;
|
|
error_log( "Error while creating paczkomaty shipment {$e->getMessage()}", E_USER_WARNING );
|
|
}
|
|
}
|
|
|
|
try {
|
|
$this->send_package( $order );
|
|
} catch ( Exception $e ) {
|
|
$all_sent = false;
|
|
error_log( "Error while sending paczkomaty shipment {$e->getMessage()}", E_USER_WARNING );
|
|
}
|
|
}
|
|
|
|
public function shipx_webhook() {
|
|
if ( isset( $_GET['shipx_webhook'] ) ) {
|
|
try {
|
|
|
|
$settings = get_option( 'woocommerce_paczkomaty_shipping_method_settings', [] );
|
|
if ( empty( $settings['kurier_webhook_token'] ) || $_GET['shipx_webhook'] !== $settings['kurier_webhook_token'] ) {
|
|
die();
|
|
}
|
|
$request_body = file_get_contents( 'php://input' );
|
|
$request_json = json_decode( $request_body );
|
|
if ( json_last_error() !== JSON_ERROR_NONE || ! isset( $request_json->payload->shipment_id ) ) {
|
|
die();
|
|
}
|
|
|
|
$shipment_id = $request_json->payload->shipment_id;
|
|
|
|
$args = [
|
|
'post_type' => 'shipment',
|
|
'posts_per_page' => 1,
|
|
'post_status' => 'any',
|
|
'meta_query' => [
|
|
[
|
|
'key' => '_paczkomat_shipment_id',
|
|
'value' => $shipment_id,
|
|
],
|
|
],
|
|
];
|
|
$posts = get_posts( $args );
|
|
if ( count( $posts ) === 1 ) {
|
|
|
|
/** @var WPDesk_Flexible_Shipping_Shipment_Paczkomaty $shipment */
|
|
$shipment = fs_get_shipment( $posts[0]->ID );
|
|
$shipment->api_refresh();
|
|
} else {
|
|
$orders = wc_get_orders(
|
|
[
|
|
'limit' => 1,
|
|
'meta_key' => '_paczkomat_shipment_id',
|
|
'meta_value' => $shipment_id,
|
|
'meta_compare' => '=',
|
|
]
|
|
);
|
|
|
|
if ( count( $orders ) === 1 ) {
|
|
$this->refresh_package_status( $orders[0], $shipment_id );
|
|
}
|
|
}
|
|
} catch ( Exception $e ) {
|
|
error_log( 'Error in WPDesk_Paczkomaty:shipx_webhook: ' . $e->getMessage() . ' -> ' . $e->getTraceAsString() );
|
|
}
|
|
|
|
die();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
private function ajax_data() {
|
|
$data = [];
|
|
if ( isset( $_POST['post_id'] ) ) { // phpcs:ignore
|
|
$data['post_id'] = $_POST['post_id']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak identyfikatora zamówienia!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
$order = wc_get_order( $data['post_id'] );
|
|
if ( $order === false ) {
|
|
throw new Exception( __( 'Nieistniejące zamówienie!', 'woocommerce-paczkomaty-inpost' ) );
|
|
} else {
|
|
$data['order'] = $order;
|
|
}
|
|
if ( isset( $_POST['paczkomat_usluga'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_usluga'] = $_POST['paczkomat_usluga']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak usługi!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_punkt_nadania'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_punkt_nadania'] = $_POST['paczkomat_punkt_nadania']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak punktu nadaniai!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_id'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_id'] = $_POST['paczkomat_id']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak identyfikatora paczkomatu!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_size'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_size'] = $_POST['paczkomat_size']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak rozmiaru przesyłki!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_width'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_width'] = $_POST['paczkomat_width']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak szrokości przesyłki!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_height'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_height'] = $_POST['paczkomat_height']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak wysokości przesyłki!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_length'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_length'] = $_POST['paczkomat_length']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak długości przesyłki przesyłki!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_weight'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_weight'] = $_POST['paczkomat_weight']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak wagi przesyłki!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_how_to_give'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_how_to_give'] = $_POST['paczkomat_how_to_give']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak sposobu nadania!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_customer_ref'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_customer_ref'] = $_POST['paczkomat_customer_ref']; // phpcs:ignore
|
|
} else {
|
|
throw new Exception( __( 'Brak numeru referencyjnego!', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
if ( isset( $_POST['paczkomat_ubezpieczenie'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_ubezpieczenie'] = $_POST['paczkomat_ubezpieczenie']; // phpcs:ignore
|
|
} else {
|
|
$data['paczkomat_ubezpieczenie'] = '0';
|
|
}
|
|
if ( isset( $_POST['paczkomat_ubezpieczenie_amount'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_ubezpieczenie_amount'] = $_POST['paczkomat_ubezpieczenie_amount']; // phpcs:ignore
|
|
} else {
|
|
$data['paczkomat_ubezpieczenie_amount'] = '';
|
|
}
|
|
if ( isset( $_POST['paczkomat_cod'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_cod'] = $_POST['paczkomat_cod']; // phpcs:ignore
|
|
} else {
|
|
$data['paczkomat_cod'] = '0';
|
|
}
|
|
if ( isset( $_POST['paczkomat_cod_amount'] ) ) { // phpcs:ignore
|
|
$data['paczkomat_cod_amount'] = $_POST['paczkomat_cod_amount']; // phpcs:ignore
|
|
} else {
|
|
$data['paczkomat_cod_amount'] = '';
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @param $data .
|
|
*/
|
|
public function ajax_save_data( $data ) {
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomaty_usluga', $data['paczkomat_usluga'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_id', $data['paczkomat_id'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_punkt_nadania', $data['paczkomat_punkt_nadania'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_size', $data['paczkomat_size'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_width', $data['paczkomat_width'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_height', $data['paczkomat_height'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_length', $data['paczkomat_length'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_weight', $data['paczkomat_weight'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_how_to_give', $data['paczkomat_how_to_give'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_customer_ref', $data['paczkomat_customer_ref'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_ubezpieczenie', $data['paczkomat_ubezpieczenie'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_ubezpieczenie_amount', $data['paczkomat_ubezpieczenie_amount'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_cod', $data['paczkomat_cod'] );
|
|
wpdesk_update_order_meta( $data['order'], '_paczkomat_cod_amount', $data['paczkomat_cod_amount'] );
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
* @param $shipment_id .
|
|
*
|
|
* @throws Exception
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception
|
|
*/
|
|
public function refresh_package_status( WC_Order $order, $shipment_id ) {
|
|
$shipX = $this->get_shipX(); // phpcs:ignore
|
|
$shipment = $shipX->get_shipment( $shipment_id ); // phpcs:ignore
|
|
wpdesk_update_order_meta( $order, '_paczkomat_shipment', $shipment );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_shipment_id', $shipment->id );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_shipment_status', $shipment->status );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_packcode', $shipment->tracking_number );
|
|
if ( $shipment->status === 'confirmed' ) {
|
|
do_action( 'paczkomaty_shipping_method_shipment_created', $order );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
public function wp_ajax_paczkomaty_refresh_package_status() {
|
|
$json = [ 'status' => 'success' ];
|
|
$data = [];
|
|
if ( empty( $_REQUEST['security'] ) || ! wp_verify_nonce( $_REQUEST['security'], 'paczkomaty_metabox_nonce' ) ) { // phpcs:ignore
|
|
$json['status'] = 'fail';
|
|
$json['message'] = __( 'Błąd weryfikacji zabezpieczeń (nonce)', 'woocommerce-paczkomaty-inpost' );
|
|
}
|
|
try {
|
|
$data = $this->ajax_data();
|
|
$paczkomat_shipment_id = wpdesk_get_order_meta( $data['order'], '_paczkomat_shipment_id', true );
|
|
if ( $paczkomat_shipment_id === '' ) {
|
|
$json['status'] = 'fail';
|
|
$json['message'] = __( 'Nie znaleziono przesyłki do tego zamówienia.', 'woocommerce-paczkomaty-inpost' );
|
|
} else {
|
|
$this->refresh_package_status( $data['order'], $paczkomat_shipment_id );
|
|
}
|
|
} catch ( Exception $e ) {
|
|
$json['status'] = 'fail';
|
|
$json['message'] = $e->getMessage();
|
|
}
|
|
$json['html'] = $this->paczkomaty_admin_box_content( $data['order'], false );
|
|
echo json_encode( $json );
|
|
die;
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
public function wp_ajax_paczkomaty_save_package() {
|
|
$json = [ 'status' => 'success' ];
|
|
if ( empty( $_REQUEST['security'] ) || ! wp_verify_nonce( $_REQUEST['security'], 'paczkomaty_metabox_nonce' ) ) { // phpcs:ignore
|
|
$json['status'] = 'fail';
|
|
$json['message'] = __( 'Błąd weryfikacji zabezpieczeń (nonce)', 'woocommerce-paczkomaty-inpost' );
|
|
}
|
|
try {
|
|
$data = $this->ajax_data();
|
|
$this->ajax_save_data( $data );
|
|
$json['html'] = $this->paczkomaty_admin_box_content( $data['order'], false );
|
|
} catch ( Exception $e ) {
|
|
$json['status'] = 'fail';
|
|
$json['message'] = $e->getMessage();
|
|
}
|
|
echo json_encode( $json );
|
|
die;
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
public function wp_ajax_paczkomaty_send_package() {
|
|
$json = [ 'status' => 'success' ];
|
|
$data = [];
|
|
if ( empty( $_REQUEST['security'] ) || ! wp_verify_nonce( $_REQUEST['security'], 'paczkomaty_metabox_nonce' ) ) { // phpcs:ignore
|
|
$json['status'] = 'fail';
|
|
$json['message'] = __( 'Błąd weryfikacji zabezpieczeń (nonce)', 'woocommerce-paczkomaty-inpost' );
|
|
}
|
|
try {
|
|
$data = $this->ajax_data();
|
|
$this->ajax_save_data( $data );
|
|
$this->send_package( $data['order'] );
|
|
} catch ( Exception $e ) {
|
|
$json['status'] = 'fail';
|
|
$json['message'] = $e->getMessage();
|
|
}
|
|
$json['html'] = $this->paczkomaty_admin_box_content( $data['order'], false );
|
|
echo json_encode( $json );
|
|
die;
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function send_kurier( WC_Order $order ) {
|
|
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
|
|
$shipment_data = new stdClass();
|
|
|
|
$parcel = new stdClass();
|
|
|
|
$parcel->dimensions = new stdClass();
|
|
$parcel->dimensions->unit = 'mm';
|
|
$parcel->dimensions->width = wpdesk_get_order_meta( $order, '_paczkomat_width', true );
|
|
$parcel->dimensions->length = wpdesk_get_order_meta( $order, '_paczkomat_length', true );
|
|
$parcel->dimensions->height = wpdesk_get_order_meta( $order, '_paczkomat_height', true );
|
|
|
|
$parcel->weight = new stdClass();
|
|
$parcel->weight->amount = wpdesk_get_order_meta( $order, '_paczkomat_weight', true );
|
|
$parcel->weight->unit = 'kg';
|
|
|
|
$shipment_data->parcels = [];
|
|
$shipment_data->parcels[] = $parcel;
|
|
|
|
$shipment_data->service = 'inpost_courier_standard';
|
|
|
|
$address = trim( wpdesk_get_order_meta( $order, '_shipping_address_1', true ) . ' ' . wpdesk_get_order_meta( $order, '_shipping_address_2', true ) );
|
|
$address_2 = trim( substr( $address, strripos( $address, ' ' ) ) );
|
|
$address_1 = trim( substr( $address, 0, strripos( $address, ' ' ) ) );
|
|
|
|
if ( $address_1 === '' ) {
|
|
$address_1 = $address_2;
|
|
$address_2 = '';
|
|
}
|
|
|
|
$receiver_address = new stdClass();
|
|
$receiver_address->street = $address_1;
|
|
$receiver_address->building_number = $address_2;
|
|
$receiver_address->city = wpdesk_get_order_meta( $order, '_shipping_city', true );
|
|
$receiver_address->post_code = wpdesk_get_order_meta( $order, '_shipping_postcode', true );
|
|
$receiver_address->country_code = wpdesk_get_order_meta( $order, '_shipping_country', true );
|
|
|
|
$receiver = new stdClass();
|
|
|
|
$receiver->address = $receiver_address;
|
|
|
|
$receiver->company_name = wpdesk_get_order_meta( $order, '_shipping_company', true );
|
|
$receiver->first_name = wpdesk_get_order_meta( $order, '_shipping_first_name', true );
|
|
$receiver->last_name = wpdesk_get_order_meta( $order, '_shipping_last_name', true );
|
|
$receiver->email = wpdesk_get_order_meta( $order, '_billing_email', true );
|
|
$receiver->phone = wpdesk_get_order_meta( $order, '_billing_phone', true );
|
|
|
|
$shipment_data->receiver = $receiver;
|
|
|
|
if ( $paczkomaty_shipping_method->get_option( 'sender_address', 'no' ) === 'yes' ) {
|
|
$sender_address = new stdClass();
|
|
$sender_address->street = $paczkomaty_shipping_method->get_option( 'sender_street', '' );
|
|
$sender_address->building_number = $paczkomaty_shipping_method->get_option( 'sender_building_number', '' );
|
|
$sender_address->city = $paczkomaty_shipping_method->get_option( 'sender_city', '' );
|
|
$sender_address->post_code = $paczkomaty_shipping_method->get_option( 'sender_post_code', '' );
|
|
$sender_address->country_code = 'PL';
|
|
|
|
$sender = new stdClass();
|
|
$sender->company_name = $paczkomaty_shipping_method->get_option( 'sender_company_name', '' );
|
|
$sender->first_name = $paczkomaty_shipping_method->get_option( 'sender_first_name', '' );
|
|
$sender->last_name = $paczkomaty_shipping_method->get_option( 'sender_last_name', '' );
|
|
$sender->email = $paczkomaty_shipping_method->get_option( 'sender_email', '' );
|
|
$sender->phone = $paczkomaty_shipping_method->get_option( 'sender_phone', '' );
|
|
$sender->address = $sender_address;
|
|
|
|
$shipment_data->sender = $sender;
|
|
|
|
}
|
|
|
|
$shipment_data->reference = wpdesk_get_order_meta( $order, '_paczkomat_customer_ref', true );
|
|
|
|
$shipment_data->custom_attributes = [];
|
|
$shipment_data->custom_attributes['sending_method'] = 'dispatch_order';
|
|
|
|
$shipment_data->sending_method = 'dispatch_order';
|
|
|
|
if ( (int) wpdesk_get_order_meta( $order, '_paczkomat_ubezpieczenie', true ) === 1 ) {
|
|
$shipment_data->insurance = new stdClass();
|
|
$shipment_data->insurance->amount = wpdesk_get_order_meta( $order, '_paczkomat_ubezpieczenie_amount', true );
|
|
$shipment_data->insurance->currency = wpdesk_get_order_meta( $order, '_order_currency', true );
|
|
}
|
|
if ( (int) wpdesk_get_order_meta( $order, '_paczkomat_cod', true ) === 1 ) {
|
|
$shipment_data->cod = new stdClass();
|
|
$shipment_data->cod->amount = wpdesk_get_order_meta( $order, '_paczkomat_cod_amount', true );
|
|
$shipment_data->cod->currency = wpdesk_get_order_meta( $order, '_order_currency', true );
|
|
}
|
|
try {
|
|
$shipX = $this->get_shipX(); // phpcs:ignore
|
|
$shipment = $shipX->create_shipment( $shipment_data ); // phpcs:ignore
|
|
wpdesk_update_order_meta( $order, '_paczkomat_shipx', 1 );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_shipment', $shipment );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_shipment_id', $shipment->id );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_shipment_status', $shipment->status );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', '' );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'created' );
|
|
} catch ( Exception $e ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', $e->getMessage() );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'failed' );
|
|
throw new Exception( $e->getMessage() );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $order .
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function send_polecony( $order ) {
|
|
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
|
|
$address = trim( wpdesk_get_order_meta( $order, '_shipping_address_1', true ) . ' ' . wpdesk_get_order_meta( $order, '_shipping_address_2', true ) );
|
|
$address_2 = trim( substr( $address, strripos( $address, ' ' ) ) );
|
|
$address_1 = trim( substr( $address, 0, strripos( $address, ' ' ) ) );
|
|
|
|
if ( $address_1 === '' ) {
|
|
$address_1 = $address_2;
|
|
$address_2 = '';
|
|
}
|
|
|
|
$pack_data = [];
|
|
$pack_data['senderEmail'] = $paczkomaty_shipping_method->get_option( 'email', '' );
|
|
$pack_data['adreseeEmail'] = wpdesk_get_order_meta( $order, '_billing_email', true );
|
|
$pack_data['phoneNum'] = wpdesk_get_order_meta( $order, '_billing_phone', true );
|
|
$pack_data['senderBoxMachineName'] = wpdesk_get_order_meta( $order, '_paczkomat_punkt_nadania', true );
|
|
$pack_data['receiverAddress'] = [];
|
|
$pack_data['receiverAddress']['name'] = wpdesk_get_order_meta( $order, '_shipping_first_name', true );
|
|
$pack_data['receiverAddress']['surName'] = wpdesk_get_order_meta( $order, '_shipping_last_name', true );
|
|
$pack_data['receiverAddress']['companyName'] = wpdesk_get_order_meta( $order, '_shipping_company', true );
|
|
$pack_data['receiverAddress']['email'] = wpdesk_get_order_meta( $order, '_billing_email', true );
|
|
$pack_data['receiverAddress']['phoneNum'] = wpdesk_get_order_meta( $order, '_billing_phone', true );
|
|
$pack_data['receiverAddress']['street'] = $address_1;
|
|
$pack_data['receiverAddress']['buildingNo'] = $address_2;
|
|
$pack_data['receiverAddress']['town'] = wpdesk_get_order_meta( $order, '_shipping_city', true );
|
|
$pack_data['receiverAddress']['zipCode'] = wpdesk_get_order_meta( $order, '_shipping_postcode', true );
|
|
|
|
$pack_data['senderAddress'] = [];
|
|
$pack_data['senderAddress']['name'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_name', '' );
|
|
$pack_data['senderAddress']['surName'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_surName', '' );
|
|
$pack_data['senderAddress']['companyName'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_companyName', '' );
|
|
$pack_data['senderAddress']['email'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_email', '' );
|
|
$pack_data['senderAddress']['phoneNum'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_phoneNum', '' );
|
|
$pack_data['senderAddress']['street'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_street', '' );
|
|
$pack_data['senderAddress']['buildingNo'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_street_number', '' );
|
|
$pack_data['senderAddress']['town'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_town', '' );
|
|
$pack_data['senderAddress']['zipCode'] = $paczkomaty_shipping_method->get_option( 'polecony_sender_zipCode', '' );
|
|
|
|
$self_send = 0;
|
|
|
|
$_allegro_journal_deal = wpdesk_get_order_meta( $order, '_allegro_journal_deal', true );
|
|
$_allegro_tansaction_id = wpdesk_get_order_meta( $order, '_allegro_tansaction_id', true );
|
|
if ( $_allegro_journal_deal !== '' && $_allegro_tansaction_id !== '' ) {
|
|
$pack_data['allegro'] = [];
|
|
$pack_data['allegro']['userId'] = $_allegro_journal_deal->dealSellerId; // phpcs:ignore
|
|
$pack_data['allegro']['transactionId'] = $_allegro_tansaction_id;
|
|
}
|
|
|
|
$response = inpost_send_letters( $paczkomaty_shipping_method->get_option( 'email', '' ), $paczkomaty_shipping_method->get_option( 'password', '' ), [ $pack_data ], 1 );
|
|
|
|
if ( ! empty( $response['error']['key'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', $response['error']['message'] );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'failed' );
|
|
throw new Exception( $response['error']['message'] );
|
|
|
|
} elseif ( isset( $response[0]['packcode'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_packcode', $response[0]['packcode'] );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', '' );
|
|
} else {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', __( 'Wystąpił błąd podczas przetwarzania żądania.', 'woocommerce-paczkomaty-inpost' ) );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'failed' );
|
|
throw new Exception( __( 'Wystąpił błąd podczas przetwarzania żądania.', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $order .
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function send_paczkomaty( $order ) {
|
|
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
|
|
$pack_data = [];
|
|
$pack_data['senderEmail'] = $paczkomaty_shipping_method->get_option( 'email', '' );
|
|
$pack_data['boxMachineName'] = wpdesk_get_order_meta( $order, '_paczkomat_id', true );
|
|
$pack_data['adreseeEmail'] = wpdesk_get_order_meta( $order, '_billing_email', true );
|
|
$pack_data['customerRef'] = wpdesk_get_order_meta( $order, '_paczkomat_customer_ref', true );
|
|
$pack_data['phoneNum'] = wpdesk_get_order_meta( $order, '_billing_phone', true );
|
|
if ( (int) wpdesk_get_order_meta( $order, '_paczkomat_ubezpieczenie', true ) === 1 ) {
|
|
$pack_data['insuranceAmount'] = wpdesk_get_order_meta( $order, '_paczkomat_ubezpieczenie_amount', true );
|
|
}
|
|
if ( (int) wpdesk_get_order_meta( $order, '_paczkomat_cod', true ) === 1 ) {
|
|
$pack_data['onDeliveryAmount'] = wpdesk_get_order_meta( $order, '_paczkomat_cod_amount', true );
|
|
}
|
|
|
|
$self_send = 0;
|
|
if ( (int) wpdesk_get_order_meta( $order, '_paczkomat_how_to_give', true ) === 1 ) {
|
|
$self_send = 1;
|
|
} elseif ( (int) wpdesk_get_order_meta( $order, '_paczkomat_how_to_give', true ) === 2 ) {
|
|
$self_send = 0;
|
|
} elseif ( (int) wpdesk_get_order_meta( $order, '_paczkomat_how_to_give', true ) === 3 ) {
|
|
$self_send = 0;
|
|
} elseif ( (int) $paczkomaty_shipping_method->get_option( 'self_send', '' ) === 1 ) {
|
|
$self_send = (int) $paczkomaty_shipping_method->get_option( 'self_send' );
|
|
}
|
|
|
|
if ( wpdesk_get_order_meta( $order, '_paczkomat_size', true ) !== '' ) {
|
|
$pack_data['packType'] = wpdesk_get_order_meta( $order, '_paczkomat_size', true );
|
|
} else {
|
|
$pack_data['packType'] = $paczkomaty_shipping_method->get_option( 'default_package_size' );
|
|
}
|
|
|
|
$_allegro_journal_deal = wpdesk_get_order_meta( $order, '_allegro_journal_deal', true );
|
|
$_allegro_tansaction_id = wpdesk_get_order_meta( $order, '_allegro_tansaction_id', true );
|
|
if ( $_allegro_journal_deal !== '' && $_allegro_tansaction_id !== '' ) {
|
|
$pack_data['allegro'] = [];
|
|
$pack_data['allegro']['userId'] = $_allegro_journal_deal->dealSellerId; // phpcs:ignore
|
|
$pack_data['allegro']['transactionId'] = $_allegro_tansaction_id;
|
|
}
|
|
$response = inpost_send_packs( $paczkomaty_shipping_method->get_option( 'email', '' ), $paczkomaty_shipping_method->get_option( 'password', '' ), [ $pack_data ], 1, $self_send );
|
|
if ( ! empty( $response[0]['error_key'] ) || ! empty( $response[0]['error_message'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', $response[0]['error_message'] );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'failed' );
|
|
throw new Exception( $response[0]['error_message'] );
|
|
} elseif ( ! empty( $response['error'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', $response['error']['message'] );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'failed' );
|
|
throw new Exception( $response['error']['message'] );
|
|
} elseif ( isset( $response[0]['packcode'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_packcode', $response[0]['packcode'] );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', '' );
|
|
} else {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_error', __( 'Wystąpił błąd podczas przetwarzania żądania.', 'woocommerce-paczkomaty-inpost' ) );
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'failed' );
|
|
throw new Exception( __( 'Wystąpił błąd podczas przetwarzania żądania.', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
public function adminInit() { // phpcs:ignore
|
|
if ( isset( $_GET['generate_inpost_sticker'] ) && $_GET['generate_inpost_sticker'] == true && isset( $_GET['post'] ) && $_GET['post'] > 0 ) {
|
|
$post_id = (int) $_GET['post'];
|
|
$order = wc_get_order( $post_id );
|
|
$packcode = wpdesk_get_order_meta( $order, '_paczkomat_packcode', true );
|
|
try {
|
|
$result = $this->get_label( $order );
|
|
header( 'Content-type: application/pdf' );
|
|
header( 'Content-Disposition: attachment; filename=e-' . $packcode . '.' . $result['label_format'] );
|
|
echo $result['content']; // phpcs:ignore
|
|
} catch ( Exception $e ) {
|
|
echo $e->getMessage(); // phpcs:ignore
|
|
}
|
|
die();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*
|
|
* @return array
|
|
* @throws Exception|WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_label( WC_Order $order ) {
|
|
$result = [
|
|
'label_format' => 'pdf',
|
|
'content' => null,
|
|
];
|
|
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
$label_format = $paczkomaty_shipping_method->get_option( 'label_format', 'pdf' );
|
|
if ( $label_format === 'epl' ) {
|
|
$label_format = 'epl';
|
|
}
|
|
$result['label_format'] = $label_format;
|
|
$paczkomat_shipx = wpdesk_get_order_meta( $order, '_paczkomat_shipx', true );
|
|
if ( $paczkomat_shipx === '' ) {
|
|
$packcode = wpdesk_get_order_meta( $order, '_paczkomat_packcode', true );
|
|
if ( ! empty( $packcode ) ) {
|
|
$result['content'] = inpost_get_sticker( $paczkomaty_shipping_method->get_option( 'email', '' ), $paczkomaty_shipping_method->get_option( 'password', '' ), $packcode, '', $label_format );
|
|
|
|
if ( $result['content'] ) {
|
|
if ( is_array( $result['content'] ) && isset( $result['content']['error'] ) ) {
|
|
throw new Exception(
|
|
sprintf(
|
|
// translators: error message.
|
|
__( 'Error: %s', 'woocommerce-paczkomaty-inpost' ),
|
|
$result['content']['error']['message']
|
|
)
|
|
);
|
|
}
|
|
} else {
|
|
throw new Exception( __( 'Error: Nie udało się wygenerować etykiety', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
} else {
|
|
throw new Exception( __( 'Error: Niepoprawny numer paczki', 'woocommerce-paczkomaty-inpost' ) );
|
|
}
|
|
} else {
|
|
$shipment_id = wpdesk_get_order_meta( $order, '_paczkomat_shipment_id', true );
|
|
if ( $shipment_id ) {
|
|
$shipX = $this->get_shipX(); // phpcs:ignore
|
|
$result['content'] = $shipX->get_shipments_labels( $shipment_id, $label_format ); // phpcs:ignore
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
public function addAdminBox() { // phpcs:ignore
|
|
global $post_id;
|
|
|
|
$order = wc_get_order( $post_id );
|
|
|
|
if ( ! $order ) {
|
|
return;
|
|
}
|
|
|
|
$_paczkomaty_usluga = wpdesk_get_order_meta( $order, '_paczkomaty_usluga', true );
|
|
|
|
if ( $_paczkomaty_usluga !== '' ) {
|
|
add_meta_box(
|
|
'woocommerce-paczkomaty-box-new',
|
|
__( 'InPost', 'woocommerce-paczkomaty-inpost' ),
|
|
[
|
|
$this,
|
|
'paczkomaty_admin_box',
|
|
],
|
|
null,
|
|
'side',
|
|
'default'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @deprecated Method for pre CPT shipment orders. Can be deleted in the future.
|
|
*/
|
|
public function paczkomaty_admin_box() {
|
|
global $theorder;
|
|
$this->paczkomaty_admin_box_content( $theorder );
|
|
}
|
|
|
|
/**
|
|
* @param $order .
|
|
* @param bool $echo .
|
|
*
|
|
* @return string
|
|
* @deprecated Method for pre CPT shipment orders. Can be deleted in the future.
|
|
*/
|
|
public function paczkomaty_admin_box_content( $order, $echo = true ) {
|
|
|
|
$status_translator = new WPDesk_Paczkomaty_ShipX_Status_Translation( $this->get_shipX(), get_locale() );
|
|
|
|
$args = [];
|
|
|
|
$args['paczkomaty_usluga'] = wpdesk_get_order_meta( $order, '_paczkomaty_usluga', true );
|
|
$args['paczkomat_id'] = wpdesk_get_order_meta( $order, '_paczkomat_id', true );
|
|
$args['paczkomat_shipx'] = wpdesk_get_order_meta( $order, '_paczkomat_shipx', true );
|
|
$args['paczkomat_shipment'] = wpdesk_get_order_meta( $order, '_paczkomat_shipment', true );
|
|
$args['paczkomat_shipment_status'] = wpdesk_get_order_meta( $order, '_paczkomat_shipment_status', true );
|
|
$args['paczkomat_shipment_status_translated'] = $status_translator->translate_status( $args['paczkomat_shipment_status'] );
|
|
$args['paczkomat_size'] = wpdesk_get_order_meta( $order, '_paczkomat_size', true );
|
|
$args['paczkomat_width'] = wpdesk_get_order_meta( $order, '_paczkomat_width', true );
|
|
$args['paczkomat_height'] = wpdesk_get_order_meta( $order, '_paczkomat_height', true );
|
|
$args['paczkomat_length'] = wpdesk_get_order_meta( $order, '_paczkomat_length', true );
|
|
$args['paczkomat_weight'] = wpdesk_get_order_meta( $order, '_paczkomat_weight', true );
|
|
$args['phone'] = wpdesk_get_order_meta( $order, '_billing_phone', true );
|
|
$args['paczkomat_packcode'] = wpdesk_get_order_meta( $order, '_paczkomat_packcode', true );
|
|
$args['paczkomat_ubezpieczenie'] = wpdesk_get_order_meta( $order, '_paczkomat_ubezpieczenie', true );
|
|
$args['paczkomat_ubezpieczenie_amount'] = wpdesk_get_order_meta( $order, '_paczkomat_ubezpieczenie_amount', true );
|
|
$args['paczkomat_cod'] = wpdesk_get_order_meta( $order, '_paczkomat_cod', true );
|
|
$args['paczkomat_cod_amount'] = wpdesk_get_order_meta( $order, '_paczkomat_cod_amount', true );
|
|
$args['paczkomat_how_to_give'] = wpdesk_get_order_meta( $order, '_paczkomat_how_to_give', true );
|
|
$args['paczkomat_error'] = wpdesk_get_order_meta( $order, '_paczkomat_error', true );
|
|
$args['post_id'] = wpdesk_get_order_id( $order );
|
|
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
|
|
$args['paczkomaty_shipping_method'] = $paczkomaty_shipping_method;
|
|
|
|
$args['select2'] = true;
|
|
|
|
if ( $args['paczkomat_ubezpieczenie_amount'] === '' ) {
|
|
$args['paczkomat_ubezpieczenie_amount'] = $order->get_total();
|
|
}
|
|
if ( $args['paczkomat_cod_amount'] === '' ) {
|
|
$args['paczkomat_cod_amount'] = $order->get_total();
|
|
}
|
|
|
|
$args['paczkomat_customer_ref'] = wpdesk_get_order_meta( $order, '_paczkomat_customer_ref', true );
|
|
|
|
if ( $args['paczkomat_customer_ref'] === '' ) {
|
|
$args['paczkomat_customer_ref'] = sprintf(
|
|
// translators: order number.
|
|
__( 'Zamówienie %s', 'woocommerce-paczkomaty-inpost' ),
|
|
$order->get_order_number()
|
|
);
|
|
} else {
|
|
$args['paczkomat_customer_ref'] = sprintf( $args['paczkomat_customer_ref'], $order->get_order_number() );
|
|
}
|
|
|
|
$this->initMachinesList();
|
|
|
|
$args['punkty_nadania'] = inpost_listmachines( 't' );
|
|
$args['paczkomat_punkt_nadania'] = wpdesk_get_order_meta( $order, '_paczkomat_punkt_nadania', true );
|
|
ob_start();
|
|
|
|
wp_nonce_field( 'paczkomaty_metabox_nonce', 'paczkomaty_metabox_nonce', false );
|
|
include 'views/admin-box-content.php';
|
|
|
|
$content = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
if ( $echo != true ) {
|
|
return $content; // phpcs:ignore
|
|
} else {
|
|
echo $content; // phpcs:ignore
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*/
|
|
public function woocommerce_checkout_create_order( WC_Order $order ) {
|
|
$order_shipping_methods = $order->get_shipping_methods();
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
$count = 0;
|
|
foreach ( $order_shipping_methods as $key => $shipping_method ) {
|
|
if ( isset( $shipping_method['item_meta']['_fs_method'] ) ) {
|
|
$fs_method = $shipping_method['item_meta']['_fs_method'];
|
|
if ( $fs_method['method_integration'] === 'paczkomaty' ) {
|
|
if ( $fs_method['paczkomaty_usluga'] === 'paczkomaty' ) {
|
|
if ( $_POST['paczkomat_id'] ) { // phpcs:ignore
|
|
$order->update_meta_data( '_paczkomat_id', esc_attr( $_POST['paczkomat_id'] ) ); // phpcs:ignore
|
|
}
|
|
$order->update_meta_data( '_paczkomaty_usluga', 'paczkomaty' );
|
|
if ( isset( $fs_method['paczkomaty_package_size'] ) ) {
|
|
$order->update_meta_data( '_paczkomat_size', $fs_method['paczkomaty_package_size'] );
|
|
} else {
|
|
if ( $paczkomaty_shipping_method->get_option( 'default_package_size', '' ) ) {
|
|
$order->update_meta_data( '_paczkomat_size', $paczkomaty_shipping_method->get_option( 'default_package_size', '' ) );
|
|
}
|
|
}
|
|
if ( $paczkomaty_shipping_method->get_option( 'self_send', '' ) ) {
|
|
$order->update_meta_data( '_paczkomat_how_to_give', $paczkomaty_shipping_method->get_option( 'self_send', '' ) );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_ubezpiczenie'] ) ) {
|
|
$order->update_meta_data( '_paczkomat_ubezpieczenie', $fs_method['paczkomaty_ubezpiczenie'] );
|
|
} else {
|
|
$order->update_meta_data( '_paczkomat_ubezpieczenie', '0' );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_cod'] ) ) {
|
|
$order->update_meta_data( '_paczkomat_cod', $fs_method['paczkomaty_cod'] );
|
|
} else {
|
|
$order->update_meta_data( '_paczkomat_cod', '0' );
|
|
}
|
|
}
|
|
if ( $fs_method['paczkomaty_usluga'] === 'polecony' ) {
|
|
$order->update_meta_data( '_paczkomaty_usluga', 'polecony' );
|
|
$order->update_meta_data( '_paczkomat_punkt_nadania', $paczkomaty_shipping_method->get_option( 'domyslny_punkt_nadania', '' ) );
|
|
$order->update_meta_data( '_paczkomaty_punkt_nadania', $paczkomaty_shipping_method->get_option( 'domyslny_punkt_nadania', '' ) );
|
|
}
|
|
if ( $fs_method['paczkomaty_usluga'] === 'kurier' ) {
|
|
$order->update_meta_data( '_paczkomaty_usluga', 'kurier' );
|
|
if ( isset( $fs_method['paczkomaty_package_width'] ) ) {
|
|
$order->update_meta_data( '_paczkomat_width', $fs_method['paczkomaty_package_width'] );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_package_height'] ) ) {
|
|
$order->update_meta_data( '_paczkomat_height', $fs_method['paczkomaty_package_height'] );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_package_length'] ) ) {
|
|
$order->update_meta_data( '_paczkomat_length', $fs_method['paczkomaty_package_length'] );
|
|
}
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_ubezpieczenie'] ) && (int) $fs_method['paczkomaty_ubezpieczenie'] === 1 ) {
|
|
$order->update_meta_data( '_paczkomat_ubezpieczenie', 1 );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_cod'] ) && $fs_method['paczkomaty_cod'] == '1' ) {
|
|
$order->update_meta_data( '_paczkomat_cod', 1 );
|
|
}
|
|
|
|
$ref = sprintf(
|
|
// translators: order number.
|
|
__( 'Zamówienie %s', 'woocommerce-paczkomaty-inpost' ),
|
|
$order->get_order_number()
|
|
);
|
|
$order->update_meta_data( '_paczkomat_customer_ref', $ref );
|
|
|
|
$order->update_meta_data( '_paczkomat_ubezpieczenie_amount', $order->get_total() );
|
|
$order->update_meta_data( '_paczkomat_cod_amount', $order->get_total() );
|
|
|
|
$order->update_meta_data( '_paczkomat_weight', ( new WooCommerce_Order_Weight_Calculator( $order ) )->get_order_weight_kg() );
|
|
|
|
$order->update_meta_data( '_flexible_shipping_status', 'new' );
|
|
}
|
|
}
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $order_id .
|
|
*/
|
|
public function woocommerce_checkout_update_order_meta( $order_id ) {
|
|
$order = wc_get_order( $order_id );
|
|
$order_shipping_methods = $order->get_shipping_methods();
|
|
$paczkomaty_shipping_method = $this->get_shipping_method();
|
|
$count = 0;
|
|
foreach ( $order_shipping_methods as $key => $shipping_method ) {
|
|
if ( isset( $shipping_method['item_meta']['_fs_method'][0] ) ) {
|
|
$fs_method = unserialize( $shipping_method['item_meta']['_fs_method'][0] );
|
|
if ( $fs_method['method_integration'] === 'paczkomaty' ) {
|
|
if ( $fs_method['paczkomaty_usluga'] === 'paczkomaty' ) {
|
|
if ( $_POST['paczkomat_id'] ) { // phpcs:ignore
|
|
wpdesk_update_order_meta( $order, '_paczkomat_id', esc_attr( $_POST['paczkomat_id'] ) ); // phpcs:ignore
|
|
}
|
|
wpdesk_update_order_meta( $order, '_paczkomaty_usluga', 'paczkomaty' );
|
|
if ( isset( $fs_method['paczkomaty_package_size'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_size', $fs_method['paczkomaty_package_size'] );
|
|
} else {
|
|
if ( $paczkomaty_shipping_method->get_option( 'default_package_size', '' ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_size', $paczkomaty_shipping_method->get_option( 'default_package_size', '' ) );
|
|
}
|
|
}
|
|
if ( $paczkomaty_shipping_method->get_option( 'self_send', '' ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_how_to_give', $paczkomaty_shipping_method->get_option( 'self_send', '' ) );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_ubezpiczenie'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_ubezpieczenie', $fs_method['paczkomaty_ubezpiczenie'] );
|
|
} else {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_ubezpieczenie', '0' );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_cod'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_cod', $fs_method['paczkomaty_cod'] );
|
|
} else {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_cod', '0' );
|
|
}
|
|
}
|
|
if ( $fs_method['paczkomaty_usluga'] === 'polecony' ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomaty_usluga', 'polecony' );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_punkt_nadania', $paczkomaty_shipping_method->get_option( 'domyslny_punkt_nadania', '' ) );
|
|
wpdesk_update_order_meta( $order, '_paczkomaty_punkt_nadania', $paczkomaty_shipping_method->get_option( 'domyslny_punkt_nadania', '' ) );
|
|
}
|
|
if ( $fs_method['paczkomaty_usluga'] === 'kurier' ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomaty_usluga', 'kurier' );
|
|
if ( isset( $fs_method['paczkomaty_package_width'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_width', $fs_method['paczkomaty_package_width'] );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_package_height'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_height', $fs_method['paczkomaty_package_height'] );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_package_length'] ) ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_length', $fs_method['paczkomaty_package_length'] );
|
|
}
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_ubezpieczenie'] ) && (int) $fs_method['paczkomaty_ubezpieczenie'] === 1 ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_ubezpieczenie', 1 );
|
|
}
|
|
if ( isset( $fs_method['paczkomaty_cod'] ) && (int) $fs_method['paczkomaty_cod'] === 1 ) {
|
|
wpdesk_update_order_meta( $order, '_paczkomat_cod', 1 );
|
|
}
|
|
|
|
$ref = sprintf(
|
|
// translators: order number.
|
|
__( 'Zamówienie %s', 'woocommerce-paczkomaty-inpost' ),
|
|
$order->get_order_number()
|
|
);
|
|
wpdesk_update_order_meta( $order, '_paczkomat_customer_ref', $ref );
|
|
|
|
wpdesk_update_order_meta( $order, '_paczkomat_ubezpieczenie_amount', $order->get_total() );
|
|
wpdesk_update_order_meta( $order, '_paczkomat_cod_amount', $order->get_total() );
|
|
|
|
wpdesk_update_order_meta( $order, '_paczkomat_weight', ( new WooCommerce_Order_Weight_Calculator( $order ) )->get_order_weight_kg() );
|
|
|
|
wpdesk_update_order_meta( $order, '_flexible_shipping_status', 'new' );
|
|
|
|
}
|
|
}
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $order .
|
|
*
|
|
* @deprecated Method for pre CPT shipment orders. Can be deleted in the future.
|
|
*/
|
|
public function orderItemsTable( $order ) { // phpcs:ignore
|
|
if ( ! $order ) {
|
|
return;
|
|
}
|
|
$paczkomat = wpdesk_get_order_meta( $order, '_paczkomat_id', true );
|
|
if ( ! empty( $paczkomat ) ) {
|
|
$args = [ 'paczkomat_id' => $paczkomat ];
|
|
$this->initMachinesList();
|
|
$args['paczkomat'] = $this->adminMachines[ $args['paczkomat_id'] ]; // phpcs:ignore
|
|
echo $this->plugin->load_template( 'order-items-table', '', $args ); // phpcs:ignore
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $options .
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function flexible_shipping_add_shipping_options( $options ) {
|
|
$options['paczkomaty'] = 'InPost';
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* @param bool $cod .
|
|
*
|
|
* @deprecated Method for pre CPT shipment orders. Can be deleted in the future.
|
|
*/
|
|
public function initMachinesList( $cod = false ) {
|
|
$paczkomaty_shipping_method = $this->plugin->paczkomaty->get_shipping_method();
|
|
if ( $paczkomaty_shipping_method->get_parcel_lockers_api() === $paczkomaty_shipping_method::API_TYPE_XML ) {
|
|
if ( empty( $this->allMachines ) ) { // phpcs:ignore
|
|
$machines = inpost_get_machine_list();
|
|
$this->machines = [ '' => '-- ' . __( 'wybierz', 'woocommerce-paczkomaty-inpost' ) . ' --' ];
|
|
$this->allMachines = []; // phpcs:ignore
|
|
foreach ( $machines as $machine ) {
|
|
if ( $cod === true && (int) $machine['paymentavailable'] !== 1 ) {
|
|
continue;
|
|
}
|
|
$this->machines[ $machine['name'] ] = $machine['town'] . ', ' . $machine['street'] . ' ' . $machine['buildingnumber'];
|
|
$this->adminMachines[ $machine['name'] ] = '[' . $machine['name'] . '] ' . $machine['town'] . ', ' . $machine['street'] . ' ' . $machine['buildingnumber']; // phpcs:ignore
|
|
$this->allMachines[ $machine['name'] ] = $machine['town'] . ', ' . $machine['street'] . ' ' . $machine['buildingnumber']; // phpcs:ignore
|
|
$this->machinesInfo[ $machine['name'] ] = $machine; // phpcs:ignore
|
|
}
|
|
}
|
|
} else {
|
|
$this->machines = $this->pickup_points_manager->get_points( WPDesk_Paczkomaty_ShipX_Cache::PARCEL_LOCKER );
|
|
$this->allMachines = $this->machines; // phpcs:ignore
|
|
$this->adminMachines = $this->machines; // phpcs:ignore
|
|
}
|
|
}
|
|
}
|