first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,345 @@
<?php
/**
* DPD API.
*
* @package WooCommerce DPD
*/
use DPDVendor\Psr\Log\LoggerInterface;
use DPDVendor\WPDesk\SOAP\SoapClientWithLogger;
/**
* Can send DPD API requests.
*/
class WPDesk_WooCommerce_DPD_API {
/**
* @var LoggerInterface
*/
private $logger;
/**
* .
*
* @var WPDesk_WooCommerce_DPD_Auth_Data
*/
private $auth_data;
/**
* .
*
* @var bool
*/
private $test_api;
/**
*
* .
*
* @var bool
*/
private $disable_ssl_verification = false;
/**
* .
*
* @var bool
*/
private $disable_cache_wsdl = false;
/**
* .
*
* @var bool
*/
private $client = false;
/**
* .
*
* @var array
*/
private $messages = array();
/**
* WPDesk_WooCommerce_DPD_API constructor.
*
* @param WPDesk_WooCommerce_DPD_Auth_Data $auth_data .
* @param LoggerInterface $logger .
* @param bool $test_api .
* @param bool $disable_ssl_verification .
* @param bool $disable_cache_wsdl .
*/
public function __construct(
WPDesk_WooCommerce_DPD_Auth_Data $auth_data,
LoggerInterface $logger,
$test_api = false,
$disable_ssl_verification = false,
$disable_cache_wsdl = false
) {
$this->auth_data = $auth_data;
$this->test_api = $test_api;
$this->messages = array(
'DISALLOWED_FID' => __( 'Błędny numer klienta', 'woocommerce-dpd' ),
'Login failed' => __( 'Błędny login lub hasło', 'woocommerce-dpd' ),
);
$this->disable_ssl_verification = $disable_ssl_verification;
$this->disable_cache_wsdl = $disable_cache_wsdl;
$this->logger = $logger;
}
/**
* .
*
* @param string $message .
*
* @return string
*/
public function translate_messages( $message ) {
if ( isset( $this->messages[ $message ] ) ) {
return $this->messages[ $message ];
}
return $message;
}
/**
* .
*/
public function clear_cache() {
global $wpdb;
$transients = $wpdb->get_results("SELECT option_name AS name, option_value AS value FROM $wpdb->options WHERE option_name LIKE '_transient_dpd%'" ); // phpcs:ignore
foreach ( $transients as $transient ) {
delete_transient( substr( $transient->name, 11 ) );
}
}
/**
* .
*
* @return bool|SoapClient
*
* @throws SoapFault .
*/
public function get_client() {
if ( ! $this->client ) {
$dpd_webapi_url = 'https://dpdservices.dpd.com.pl/DPDPackageObjServicesService/DPDPackageObjServices?WSDL';
if ( $this->test_api ) {
$dpd_webapi_url = 'https://dpdservicesdemo.dpd.com.pl/DPDPackageObjServicesService/DPDPackageObjServices?WSDL';
}
$client_options = array(
'keep-alive' => true,
'connection_timeout' => 30,
'trace' => 1,
);
if ( $this->disable_ssl_verification ) {
$context = stream_context_create(
array(
'ssl' => array(
// set some SSL/TLS specific options.
'verify_peer' => false,
'verify_peer_name' => false,
),
)
);
$client_options['stream_context'] = $context;
}
if ( $this->disable_cache_wsdl ) {
$client_options['cache_wsdl'] = 0;
}
$this->client = new SoapClientWithLogger(
new SoapClient(
$dpd_webapi_url,
$client_options
),
$this->logger
);
}
return $this->client;
}
/**
* .
*
* @return mixed
*
* @throws SoapFault .
*/
public function ping() {
$params = array(
'authDataV1' => $this->auth_data->get_auth_data_v1(),
'postalCodeV1' => array(
'countryCode' => 'PL',
'zipCode' => '22500',
),
);
return $this->get_client()->findPostalCodeV1( $params );
}
/**
* .
*
* @param array $shipments .
*
* @return stdClass
*
* @throws Exception .
*/
public function create_shipment( $shipments ) {
$client = $this->get_client();
$params = array(
'authDataV1' => $this->auth_data->get_auth_data_v1(),
'openUMLFeV3' => $shipments,
'policyV1' => array(),
'langCode' => 'PL',
);
try {
return $client->generatePackagesNumbersV4( $params );
} catch ( Exception $e ) {
// Translators: error message.
throw new Exception( sprintf( __( 'Komunikat API DPD: %1$s', 'woocommerce-dpd' ), $e->getMessage() ) );
}
}
/**
* .
*
* @param array $packages .
* @param string $session_type .
* @param array $pickup_address .
* @see WPDesk_Flexible_Shipping_Manifest_dpd::prepare_pickup_address()
* @return mixed
*
* @throws Exception .
*/
public function create_manifest( $packages, $session_type, $pickup_address ) {
$client = $this->get_client();
$params = array(
'authDataV1' => $this->auth_data->get_auth_data_v1(),
'dpdServicesParamsV1' => array(
'pickupAddress' => $pickup_address,
'policy' => 'IGNORE_ERRORS',
'session' => array(
'sessionType' => $session_type,
'packages' => array(),
),
),
'outputDocFormatV1' => 'PDF',
'outputDocPageFormatV1' => 'A4',
);
foreach ( $packages as $package ) {
$params['dpdServicesParamsV1']['session']['packages'][] = array( 'packageId' => $package );
}
try {
$ret = $client->generateProtocolV2( $params );
} catch ( Exception $e ) {
// Translators: error message.
throw new Exception( sprintf( __( 'Komunikat API DPD: %1$s', 'woocommerce-dpd' ), $e->getMessage() ) );
}
return $ret;
}
/**
* .
*
* @param string $package_id .
* @param string $session_type .
* @param string $label_format .
* @param string $label_page_format .
* @param string $label_type .
* @return mixed .
* @throws Exception .
*/
public function get_label( $package_id, $session_type, $label_format, $label_page_format, $label_type ) {
return $this->get_labels_in_single_file( array( $package_id ), $session_type, $label_format, $label_page_format, $label_type );
}
/**
* Get labels for multiple packages in single file.
*
* @param string[] $packages .
* @param string $session_type .
* @param string $label_format .
* @param string $label_page_format .
* @param string $label_type .
*
* @return mixed
* @throws Exception .
*/
public function get_labels_in_single_file( array $packages, $session_type, $label_format, $label_page_format, $label_type ) {
$client = $this->get_client();
$params = array(
'authDataV1' => $this->auth_data->get_auth_data_v1(),
'dpdServicesParamsV1' => array(
'policy' => 'IGNORE_ERRORS',
'session' => array(
'sessionType' => $session_type,
),
),
'outputDocFormatV1' => $label_format,
'outputDocPageFormatV1' => $label_page_format,
'outputLabelType' => $label_type,
);
foreach ( $packages as $package_id ) {
$param_package = array( 'packageId' => $package_id );
$params['dpdServicesParamsV1']['session']['packages'][] = $param_package;
}
try {
$api_response = $client->generateSpedLabelsV4( $params );
if ( ! isset( $api_response->return, $api_response->return->documentData ) ) {
throw new \WPDesk\DPD\Exceptions\EmptyLabelException( __( 'API DPD zwróciło błędną odpowiedz: brak pliku etykiety. Skontaktuj się z DPD.', 'woocommerce-dpd' ) );
}
} catch ( \WPDesk\DPD\Exceptions\EmptyLabelException $ele ) {
throw $ele;
} catch ( Exception $e ) {
// Translators: exception message.
throw new Exception( sprintf( __( 'Komunikat API DPD: %1$s', 'woocommerce-dpd' ), $e->getMessage() ) );
}
return $api_response;
}
/**
* .
*
* @param stdClass $package .
* @return string
*/
public function parse_shipment_errors( $package ) {
$parsed_errors = '';
if ( isset( $package->return->Status ) && 'OK' !== $package->return->Status ) {
$parsed_errors = $this->translate_messages( $package->return->Status );
if ( isset( $package->return->Info ) ) {
$parsed_errors = $package->return->Info;
}
}
if ( isset( $package->return->Packages->Package->ValidationDetails ) ) {
if ( ! is_array( $package->return->Packages->Package->ValidationDetails->ValidationInfo ) ) {
$package->return->Packages->Package->ValidationDetails->ValidationInfo = array( $package->return->Packages->Package->ValidationDetails->ValidationInfo );
}
foreach ( $package->return->Packages->Package->ValidationDetails->ValidationInfo as $validation_info ) {
if ( '' !== $parsed_errors ) {
$parsed_errors .= ', ';
}
$parsed_errors .= $validation_info->Info; // phpcs:ignore
}
}
if ( isset( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails ) ) {
if ( ! is_array( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo ) ) {
$package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo = array( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo );
}
foreach ( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo as $validation_info ) {
if ( '' !== $parsed_errors ) {
$parsed_errors .= ', ';
}
$parsed_errors .= $validation_info->Info; // phpcs:ignore
}
}
return $parsed_errors;
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* DPD Auth data.
*
* @package WooCommerce DPD
*/
/**
* DPD Auth data.
*/
class WPDesk_WooCommerce_DPD_Auth_Data {
/**
* @var string
*/
private $login;
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $fid;
/**
* WPDesk_WooCommerce_DPD_Auth_Data constructor.
*
* @param string $login .
* @param string $password .
* @param string $fid .
*/
public function __construct( $login, $password, $fid ) {
$this->login = $login;
$this->password = $password;
$this->fid = $fid;
}
/**
* @return array
*/
public function get_auth_data_v1() {
return array(
'login' => $this->login,
'password' => $this->password,
'masterFid' => $this->fid,
);
}
}

View File

@@ -0,0 +1,121 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_WooCommerce_DPD_Flexible_Printing_Integration' ) ) {
class WPDesk_WooCommerce_DPD_Flexible_Printing_Integration extends Flexible_Printing_Integration {
private $plugin = null;
public function __construct( WPDesk_WooCommerce_DPD_Plugin $plugin ) {
$this->plugin = $plugin;
$this->id = 'dpd';
$this->title = 'DPD';
add_action( 'flexible_shipping_shipping_actions_html', array( $this, 'flexible_shipping_shipping_actions_html' ) );
add_action( 'flexible_shipping_shipment_status_updated', array( $this, 'flexible_shipping_shipment_status_updated' ), 10, 3 );
}
/**
* @param $old_status string
* @param $new_status string
* @param $shipment WPDesk_Flexible_Shipping_Shipment|WPDesk_Flexible_Shipping_Shipment_Interface
*/
public function flexible_shipping_shipment_status_updated( $old_status, $new_status, $shipment ) {
if ( $new_status != $old_status && $new_status == 'fs-confirmed' && $shipment->get_integration() == 'dpd' ) {
$all_shipping_methods = WC()->shipping()->get_shipping_methods();
$shipping_method = $all_shipping_methods['dpd'];
if ( $shipping_method->get_option( 'auto_print', 'no' ) == 'yes'
) {
$label_data = $shipment->get_label();
try {
$content_type = 'application/pdf';
if ( $label_data['label_format'] == 'zpl' ) {
$content_type = 'application/zpl';
}
else if ( $label_data['label_format'] == 'epl' ) {
$content_type = 'application/epl';
}
$this->do_print(
$label_data['file_name'],
$label_data['content'],
$content_type,
false
);
}
catch ( Exception $e ) {
error_log( sprintf( __( 'Blad drukowania: %s', 'woocommerce-dpd' ), $e->getMessage() ) );
}
}
}
}
public function options() {
$options = array();
/*
$options[] = array(
'id' => 'auto_print',
'name' => __('Automatyczne drukowanie', 'woocommerce-dpd'),
'desc' => __('Włącz (po utworzeniu etykieta zostanie automatycznie wydrukowana)', 'woocommerce-dpd'),
'type' => 'checkbox',
'std' => '',
);
*/
return $options;
}
public function do_print_action( $args ) {
if ( !empty( $args['data']['shippment_id'] ) ) {
$shipment = fs_get_shipment( $args['data']['shippment_id'] );
/* @var $shipment WPDesk_Flexible_Shipping_Shipment|WPDesk_Flexible_Shipping_Shipment_Interface */
$label_data = $shipment->get_label();
$args = array(
'title' => $label_data['file_name'],
'content' => $label_data['content'],
'content_type' => 'application/' . $label_data['label_format'],
'silent' => false
);
}
else {
$shipment_id = $args['data']['shipment_id'];
$label_data = $this->plugin->dpd->get_label( $shipment_id );
$content_type = 'application/pdf';
if ($label_data->getLabelsResult->item->labelType == 'ZBLP') {
$content_type = 'application/zpl';
}
$args = array(
'title' => 'dpd_' . $label_data->getLabelsResult->item->labelType . '_' . $label_data->getLabelsResult->item->labelName,
'content' => base64_decode($label_data->getLabelsResult->item->labelData),
'content_type' => $content_type,
'silent' => false
);
}
do_action( 'flexible_printing_print', 'dpd', $args );
}
public function flexible_shipping_shipping_actions_html( $shipping ) {
if ( !empty( $shipping['shipment'] ) ) {
$shipment = $shipping['shipment'];
/* @var $shipment WPDesk_Flexible_Shipping_Shipment|WPDesk_Flexible_Shipping_Shipment_Interface */
if ( $shipment->get_meta( '_integration', '' ) == 'dpd' ) {
if ( $shipment->get_label_url() != null ) {
echo apply_filters( 'flexible_printing_print_button', '', 'dpd',
array(
'content' => 'print',
'icon' => true,
'id' => str_replace( ', ', '-', $shipment->get_meta('_dpd_package_number' ) ),
'tip' => __( 'Drukuj na: %s', 'woocommerce-dpd' ),
'data' => array(
'shippment_id' => $shipment->get_id(),
'dpd_package_number' => $shipment->get_meta('_dpd_package_number' ),
),
)
);
}
}
}
}
}
}

View File

@@ -0,0 +1,259 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_WooCommerce_DPD_FS_Hooks' ) ) {
class WPDesk_WooCommerce_DPD_FS_Hooks {
private $plugin = null;
public function __construct( WPDesk_WooCommerce_DPD_Plugin $plugin ) {
$this->plugin = $plugin;
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 );
}
function flexible_shipping_integration_options( $options ) {
$options['dpd'] = __( 'DPD', 'woocommerce-dpd' );
return $options;
}
public function flexible_shipping_method_settings( $flexible_shipping_settings, $shipping_method ) {
$shipping_methods = WC()->shipping->get_shipping_methods();
if ( 1 == 1 || $shipping_methods['dpd']->enabled == 'yes' ) { // always available
$declared_value_custom_attributes = array();
if ( isset( $shipping_method['dpd_declared_value'] ) && $shipping_method['dpd_declared_value'] == '1' ) {
$declared_value_custom_attributes = array( 'checked' => 'checked' );
}
$cod_custom_attributes = array();
if ( isset( $shipping_method['dpd_cod'] ) && $shipping_method['dpd_cod'] == '1' ) {
$cod_custom_attributes = array( 'checked' => 'checked' );
}
$saturday_custom_attributes = array();
if ( isset( $shipping_method['dpd_saturday'] ) && $shipping_method['dpd_saturday'] == '1' ) {
$saturday_custom_attributes = array( 'checked' => 'checked' );
}
$next_day_custom_attributes = array();
if ( isset( $shipping_method['dpd_next_day'] ) && $shipping_method['dpd_next_day'] == '1' ) {
$next_day_custom_attributes = array( 'checked' => 'checked' );
}
$pickup_custom_attributes = array();
if ( isset( $shipping_method['dpd_pickup'] ) && (int) $shipping_method['dpd_pickup'] === 1 ) {
$pickup_custom_attributes = array( 'checked' => 'checked' );
}
$settings = array(
'dpd_product' => array(
'title' => __( 'Usługa DPD', 'woocommerce-dpd' ),
'type' => 'select',
'default' => isset( $shipping_method['dpd_product'] ) ? $shipping_method['dpd_product'] : '',
'options' => WPDesk_WooCommerce_DPD::get_products(),
),
'dpd_package_settings' => array(
'title' => __( 'Domyślne ustawienia paczki', 'woocommerce-dpd' ),
'type' => 'title',
),
'dpd_package_width' => array(
'title' => __( 'Długość [cm]', 'woocommerce-dpd' ),
'type' => 'number',
'default' => isset( $shipping_method['dpd_package_width'] ) ? $shipping_method['dpd_package_width'] : '',
'custom_attributes' => array(
'step' => 'any'
)
),
'dpd_package_height' => array(
'title' => __( 'Wysokość [cm]', 'woocommerce-dpd' ),
'type' => 'number',
'default' => isset( $shipping_method['dpd_package_height'] ) ? $shipping_method['dpd_package_height'] : '',
'custom_attributes' => array(
'step' => 'any'
)
),
'dpd_package_depth' => array(
'title' => __( 'Głębokość [cm]', 'woocommerce-dpd' ),
'type' => 'number',
'default' => isset( $shipping_method['dpd_package_depth'] ) ? $shipping_method['dpd_package_depth'] : '',
'custom_attributes' => array(
'step' => 'any'
)
),
'dpd_package_content' => array(
'title' => __( 'Zawartość', 'woocommerce-dpd' ),
'default' => isset( $shipping_method['dpd_package_content'] ) ? $shipping_method['dpd_package_content'] : '',
'type' => 'text',
),
'dpd_declared_value' => array(
'title' => __( 'Zadeklarowana wartość', 'woocommerce-dpd' ),
'type' => 'checkbox',
'custom_attributes' => $declared_value_custom_attributes
),
'dpd_cod' => array(
'title' => __( 'Pobranie', 'woocommerce-dpd' ),
'type' => 'checkbox',
'custom_attributes' => $cod_custom_attributes
),
'dpd_pickup' => array(
'title' => __( 'Doręczenie do punktu odbioru', 'woocommerce-dpd' ),
'type' => 'checkbox',
'custom_attributes' => $pickup_custom_attributes
),
'dpd_saturday' => array(
'title' => __( 'Doręczenie w sobotę', 'woocommerce-dpd' ),
'type' => 'checkbox',
'custom_attributes' => $saturday_custom_attributes
),
'dpd_next_day' => array(
'title' => __( 'DPD Next Day', 'woocommerce-dpd' ),
'label' => __( 'Dostawa następnego dnia', 'woocommerce-dpd' ),
'type' => 'checkbox',
'custom_attributes' => $next_day_custom_attributes
),
);
return array_merge( $flexible_shipping_settings, $settings );
}
return $flexible_shipping_settings;
}
public function flexible_shipping_method_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
function dpdOptions() {
if ( jQuery('#woocommerce_flexible_shipping_method_integration').val() == 'dpd' ) {
jQuery('#woocommerce_flexible_shipping_dpd_product').closest('tr').css('display','none');
//jQuery('#woocommerce_flexible_shipping_dpd_product').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_package_settings').css('display','block');
jQuery('#woocommerce_flexible_shipping_dpd_package_width').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_package_height').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_package_depth').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_package_content').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_declared_value').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_cod').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_saturday').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_next_day').closest('tr').css('display','table-row');
jQuery('#woocommerce_flexible_shipping_dpd_pickup').closest('tr').css('display','table-row');
}
else {
jQuery('#woocommerce_flexible_shipping_dpd_product').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_package_settings').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_package_width').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_package_height').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_package_depth').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_package_content').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_declared_value').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_cod').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_saturday').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_next_day').closest('tr').css('display','none');
jQuery('#woocommerce_flexible_shipping_dpd_pickup').closest('tr').css('display','none');
}
}
jQuery('#woocommerce_flexible_shipping_method_integration').change(function() {
dpdOptions();
});
jQuery('#woocommerce_flexible_shipping_dpd_product').change(function() {
dpdOptions();
});
dpdOptions();
});
</script>
<?php
}
public function flexible_shipping_process_admin_options( $shipping_method ) {
$shipping_method['dpd_product'] = $_POST['woocommerce_flexible_shipping_dpd_product'];
$shipping_method['dpd_package_width'] = $_POST['woocommerce_flexible_shipping_dpd_package_width'];
$shipping_method['dpd_package_height'] = $_POST['woocommerce_flexible_shipping_dpd_package_height'];
$shipping_method['dpd_package_depth'] = $_POST['woocommerce_flexible_shipping_dpd_package_depth'];
$shipping_method['dpd_package_content'] = $_POST['woocommerce_flexible_shipping_dpd_package_content'];
$shipping_method['dpd_declared_value'] = 0;
if ( isset( $_POST['woocommerce_flexible_shipping_dpd_declared_value'] ) ) {
$shipping_method['dpd_declared_value'] = $_POST['woocommerce_flexible_shipping_dpd_declared_value'];
}
$shipping_method['dpd_cod'] = 0;
if ( isset( $_POST['woocommerce_flexible_shipping_dpd_cod'] ) ) {
$shipping_method['dpd_cod'] = $_POST['woocommerce_flexible_shipping_dpd_cod'];
}
$shipping_method['dpd_saturday'] = 0;
if ( isset( $_POST['woocommerce_flexible_shipping_dpd_saturday'] ) ) {
$shipping_method['dpd_saturday'] = $_POST['woocommerce_flexible_shipping_dpd_saturday'];
}
$shipping_method['dpd_next_day'] = 0;
if ( isset( $_POST['woocommerce_flexible_shipping_dpd_next_day'] ) ) {
$shipping_method['dpd_next_day'] = $_POST['woocommerce_flexible_shipping_dpd_next_day'];
}
return $shipping_method;
}
public function flexible_shipping_method_integration_col( $col, $shipping_method ) {
$shipping_methods = WC()->shipping->get_shipping_methods();
if ( $shipping_methods['dpd']->enabled == 'yes' ) {
if ( isset( $shipping_method['method_integration'] ) && 'dpd' === $shipping_method['method_integration'] ) {
ob_start();
$tip = __( 'Brak', 'woocommerce-dpd' );
$products = WPDesk_WooCommerce_dpd::get_products();
if ( isset( $products[$shipping_method['dpd_product']] ) ) {
$tip = $products[$shipping_method['dpd_product']];
}
/*
$package_types = WPDesk_WooCommerce_DPD::get_package_types();
if ( isset( $package_types[$shipping_method['dpd_package_type']] ) ) {
$tip .= ', ' . $package_types[$shipping_method['dpd_package_type']];
}
*/
?>
<td width="1%" class="integration default">
<span class="tips" data-tip="<?php echo $tip; ?>">
<?php echo $shipping_methods['dpd']->title; ?>
</span>
</td>
<?php
$col = ob_get_contents();
ob_end_clean();
}
}
return $col;
}
public function flexible_shipping_method_rate_id( $rate_id, $shipping_method ) {
if ( isset( $shipping_method['method_integration'] ) && 'dpd' === $shipping_method['method_integration'] ) {
//$rate_id = $rate_id . '_dpd_' . sanitize_title( $shipping_method['dpd_product'] ) . '_' . $shipping_method['dpd_package_type'] . '_' . $shipping_method['dpd_insurance'];
$rate_id = $rate_id . '_dpd_' . sanitize_title( $shipping_method['dpd_product'] );
if ( isset( $shipping_method['dpd_cod'] ) ) {
$rate_id .= '_' . $shipping_method['dpd_cod'];
}
}
return $rate_id;
}
public function flexible_shipping_add_method( $add_method, $shipping_method, $package ) {
if ( isset( $shipping_method['method_integration'] ) && 'dpd' === $shipping_method['method_integration']
&& isset( $shipping_method['dpd_product'] )
) {
}
return $add_method;
}
}
}

View File

@@ -0,0 +1,197 @@
<?php
/**
* Manifest.
*
* @package WooCommerce DPD
*/
/**
* Implements Manifest methods.
*/
class WPDesk_Flexible_Shipping_Manifest_dpd extends WPDesk_Flexible_Shipping_Manifest implements WPDesk_Flexible_Shipping_Manifest_Interface { // phpcs:ignore
const META_NUMBER = '_number';
const COUNTRY_PL = 'PL';
/**
* Get manifest number.
*
* @return string|null
*/
public function get_number() {
return $this->get_meta( self::META_NUMBER );
}
/**
* Get manifest file.
*
* @return array
*/
public function get_manifest() {
return array(
'file_name' => 'dpd_manifest_' . $this->get_number() . '.pdf',
'content' => base64_decode( $this->get_meta( '_document_data', '' ) ),
);
}
/**
* Generate manifest.
*
* @throws Exception|WPDesk_WooCommerce_DPD_Manifest_Creation_Exception .
*/
public function generate() {
$api = $this->get_api();
$shipping_method = $this->get_shipping_method();
$settings = $shipping_method->settings;
$fid = $settings['fid'];
if ( 'yes' === $settings['test_mode'] ) {
$fid = $settings['fid_testmode'];
}
$packages = array();
$shipments = $this->get_meta( '_shipments', array() );
$session_type = '';
$sender_address_id = false;
foreach ( $shipments as $shipment_id ) {
$shipment = fs_get_shipment( $shipment_id );
$order = $shipment->get_order();
if ( self::COUNTRY_PL !== $order->get_shipping_country() ) {
$current_session_type = WPDesk_Flexible_Shipping_Shipment_dpd::INTERNATIONAL;
} else {
$current_session_type = WPDesk_Flexible_Shipping_Shipment_dpd::DOMESTIC;
}
if ( '' !== $shipment->get_meta( '_dpd_session_type', '' ) ) {
$current_session_type = $shipment->get_meta( '_dpd_session_type', '' );
}
if ( '' === $session_type ) {
$session_type = $current_session_type;
}
if ( $session_type !== $current_session_type ) {
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( __( 'Protokół może zawierać tylko przesyłki krajowe, lub tylko międzynarodowe.', 'woocommerce-dpd' ) );
}
$sender_address_id = $this->verify_sender_address_id( $sender_address_id, $shipment );
$packages[] = $shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::DPD_PACKAGE_ID, '' );
}
$manifest = $api->create_manifest( $packages, $session_type, $this->prepare_pickup_address( $fid, $sender_address_id ) );
if ( isset( $manifest->return, $manifest->return->session, $manifest->return->session->statusInfo ) ) {
if ( 'OK' === $manifest->return->session->statusInfo->status && isset( $manifest->return->documentData ) ) {
$this->set_meta( '_document_data', base64_encode( $manifest->return->documentData ) );
$this->set_meta( self::META_NUMBER, $manifest->return->documentId );
} else {
if ( isset( $manifest->return->session->statusInfo->description ) ) {
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( $manifest->return->session->statusInfo->description );
} elseif ( ! isset( $manifest->return->documentData ) ) {
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( __( 'API zwróciło błędną zawartość, brak dokumentu!', 'woocommerce-dpd' ) );
} else {
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( $manifest->return->session->statusInfo->status );
}
}
}
}
/**
* Prepare pickup address.
*
* @param string $fid .
* @param string $sender_address_id .
* @return array
*
* @throws WPDesk_WooCommerce_DPD_Manifest_Creation_Exception .
*/
private function prepare_pickup_address( $fid, $sender_address_id ) {
$pickup_address = false;
$shipping_method = $this->get_shipping_method();
if ( '' === $sender_address_id ) {
$pickup_address = array(
'name' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_NAME, '' ),
'company' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_COMPANY, '' ),
'address' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_ADDRESS, '' ),
'city' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_CITY, '' ),
'countryCode' => self::COUNTRY_PL,
'postalCode' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_POSTAL_CODE, '' ),
'phone' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_PHONE, '' ),
'email' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_EMAIL, '' ),
);
} else {
$additional_sender_addresses = $shipping_method->get_additional_sender_addresses();
foreach ( $additional_sender_addresses as $additional_sender_address ) {
if ( $sender_address_id === $additional_sender_address->get_address_id() ) {
$pickup_address = array(
'name' => $additional_sender_address->get_name(),
'company' => $additional_sender_address->get_company(),
'address' => $additional_sender_address->get_address(),
'city' => $additional_sender_address->get_city(),
'countryCode' => self::COUNTRY_PL,
'postalCode' => $additional_sender_address->get_postal_code(),
'phone' => $additional_sender_address->get_phone(),
'email' => $additional_sender_address->get_email(),
);
break;
}
}
}
if ( false === $pickup_address ) {
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception(
// Translators: sender address id.
sprintf( __( 'Błędny identyfikator adresu nadawcy przesyłki: %1$s', 'woocommerce-dpd' ), $sender_address_id )
);
}
return $pickup_address;
}
/**
* Verify sender address id.
* All shipments in manifest should have same sender address.
*
* @param string|bool $current_sender_address_id .
* @param WPDesk_Flexible_Shipping_Shipment_dpd $shipment .
* @return string
*
* @throws WPDesk_WooCommerce_DPD_Manifest_Creation_Exception .
*/
private function verify_sender_address_id( $current_sender_address_id, $shipment ) {
$shipment_sender_address_id = $shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::DPD_SENDER_ADDRESS_ID, '' );
if ( false !== $current_sender_address_id ) {
if ( $current_sender_address_id !== $shipment_sender_address_id ) {
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( __( 'Protokół może zawierać tylko przesyłki dla jednego adresu nadania.', 'woocommerce-dpd' ) );
}
}
return $shipment_sender_address_id;
}
/**
* Cancel manifest.
*/
public function cancel() {
$this->delete_meta( self::META_NUMBER );
}
/**
* Get API.
*
* @return bool|WPDesk_WooCommerce_DPD_API
*/
public function get_api() {
$shipping_method = $this->get_shipping_method();
return $shipping_method->get_api();
}
/**
* Get shipping method.
*
* @return WPDesk_WooCommerce_DPD_Shipping_Method
*/
public function get_shipping_method() {
$shipping_methods = WC()->shipping()->shipping_methods;
if ( empty( $shipping_methods ) || ! is_array( $shipping_methods ) || 0 === count( $shipping_methods ) ) {
$shipping_methods = WC()->shipping()->load_shipping_methods();
}
return $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ];
}
}

View File

@@ -0,0 +1,785 @@
<?php
/**
* Shipment.
*
* @package Woocommerce Dpd
*/
use DPDVendor\WPDesk\HttpClient\HttpClientRequestException;
use WPDesk\DPD\PickupPoints\Api;
use WPDesk\DPD\PickupPoints\PickupPointsManager;
use WPDesk\DPD\SanitizeTrait;
/**
* Shipment functionality from Flexible Shipping.
*/
class WPDesk_Flexible_Shipping_Shipment_dpd extends WPDesk_Flexible_Shipping_Shipment implements WPDesk_Flexible_Shipping_Shipment_Interface { // phpcs:ignore
use SanitizeTrait;
const META_DPD_COD = '_dpd_cod';
const META_DPD_PICKUP = '_dpd_pickup';
const META_DPD_PICKUP_POINT_ID = '_dpd_pickup_point_id';
const META_DPD_SATURDAY = '_dpd_saturday';
const META_DPD_NEXT_DAY = '_dpd_next_day';
const DOMESTIC = 'DOMESTIC';
const INTERNATIONAL = 'INTERNATIONAL';
const DPD_PACKAGE = '_dpd_package';
const DPD_PACKAGE_ID = '_dpd_package_id';
const DPD_SENDER_ADDRESS_ID = '_dpd_sender_address_id';
const DELIVERY_FIELD_ID = 'delivery_point_id';
const DELIVERY_FIELD_NAME = 'delivery_point_name';
const DELIVERY_FIELD_ADDRESS = 'delivery_point_address';
const DELIVERY_FIELD_POSTCODE = 'delivery_point_postcode';
const DELIVERY_FIELD_CITY = 'delivery_point_city';
const TEST_PICKUP_POINT = 'PL99999';
/**
* @var PickupPointsManager
*/
private static $pickup_points_manager;
/**
* @var Api
*/
private static $pickup_points_api;
/**
* One time message.
*
* @var null|string
*/
private $dpd_one_time_message = null;
/**
* Shipment date.
*
* @var null
*/
public static $shipment_date = null;
/**
* DPD plugin.
*
* @var WPDesk_WooCommerce_DPD_Plugin
*/
public static $woocommerce_dpd_plugin;
/**
* WPDesk_Flexible_Shipping_Shipment_dpd constructor.
*
* @param int|WP_Post|WPDesk_Flexible_Shipping_Shipment $shipment Shipment or shipment ID.
* @param WC_Order|null $order .
*/
public function __construct( $shipment, WC_Order $order = null ) {
parent::__construct( $shipment, $order );
}
/**
* @param PickupPointsManager $pickup_points_manager .
*/
public static function set_pickup_points( PickupPointsManager $pickup_points_manager ) {
self::$pickup_points_manager = $pickup_points_manager;
}
/**
* @param Api $pickup_points_api .
*/
public static function set_pickup_points_api( Api $pickup_points_api ) {
self::$pickup_points_api = $pickup_points_api;
}
/**
* Checkout.
*
* @param array $fs_method .
* @param array $package .
*/
public function checkout( array $fs_method, $package ) {
/** @var WC_Order $order */
$order = $this->get_order();
if ( isset( $fs_method['dpd_product'] ) ) {
$this->set_meta( '_dpd_product', $fs_method['dpd_product'] );
}
if ( isset( $fs_method['dpd_package_width'] ) ) {
$this->set_meta( '_dpd_package_width', $fs_method['dpd_package_width'] );
}
if ( isset( $fs_method['dpd_package_height'] ) ) {
$this->set_meta( '_dpd_package_height', $fs_method['dpd_package_height'] );
}
if ( isset( $fs_method['dpd_package_depth'] ) ) {
$this->set_meta( '_dpd_package_depth', $fs_method['dpd_package_depth'] );
}
if ( isset( $fs_method['dpd_declared_value'] ) ) {
$this->set_meta( '_dpd_declared_value', $fs_method['dpd_declared_value'] );
}
if ( isset( $fs_method['dpd_cod'] ) ) {
$this->set_meta( self::META_DPD_COD, $fs_method['dpd_cod'] );
}
if ( isset( $fs_method['dpd_pickup'] ) ) {
$point_id = $order->get_meta( '_dpd_pickup_point_id' );
if ( $point_id ) {
try {
$this->save_pickup_details( $point_id );
$this->set_meta( self::META_DPD_PICKUP, $fs_method['dpd_pickup'] );
} catch ( HttpClientRequestException $e ) { // phpcs:ignore
// TODO: write exception to log.
}
}
}
if ( isset( $fs_method['dpd_saturday'] ) ) {
$this->set_meta( self::META_DPD_SATURDAY, $fs_method['dpd_saturday'] );
}
if ( isset( $fs_method['dpd_next_day'] ) ) {
$this->set_meta( self::META_DPD_NEXT_DAY, $fs_method['dpd_next_day'] );
}
if ( isset( $fs_method['dpd_package_content'] ) ) {
$this->set_meta( '_dpd_package_content', $fs_method['dpd_package_content'] );
}
$this->set_meta( '_dpd_cod_value', $order->get_total() );
$this->set_meta( '_dpd_declared_value_value', $order->get_total() );
$this->set_meta( '_dpd_package_weight', wc_get_weight( fs_calculate_package_weight( $package ), 'kg' ) );
// Translators: order number.
$this->set_meta( '_dpd_ref', sprintf( __( 'Zamówienie %s', 'woocommerce-dpd' ), $order->get_order_number() ) );
}
/**
* Get metaboc content.
*
* @return false|string
*/
public function order_metabox_content() {
if ( ! function_exists( 'woocommerce_form_field' ) ) {
$wc_template_functions = trailingslashit( dirname( __FILE__ ) ) . '../../woocommerce/includes/wc-template-functions.php';
if ( file_exists( $wc_template_functions ) ) {
include_once $wc_template_functions;
}
}
$settings = $this->get_shipping_method()->settings;
$order = $this->get_order();
$id = $this->get_id();
$dpd_status = $this->get_meta( '_dpd_status' );
$disabled = false;
if ( isset( $dpd_status ) && 'ok' === $dpd_status ) {
$disabled = true;
}
$dpd_product = $this->get_meta( '_dpd_product' );
$dpd_package_width = $this->get_meta( '_dpd_package_width' );
$dpd_package_height = $this->get_meta( '_dpd_package_height' );
$dpd_package_depth = $this->get_meta( '_dpd_package_depth' );
$dpd_package_weight = $this->get_meta( '_dpd_package_weight' );
$dpd_package_content = $this->get_meta( '_dpd_package_content' );
$dpd_ref = $this->get_meta( '_dpd_ref' );
$dpd_declared_value = $this->get_meta( '_dpd_declared_value' );
$dpd_declared_value_value = $this->get_meta( '_dpd_declared_value_value' );
$dpd_cod = $this->get_meta( self::META_DPD_COD );
$dpd_pickup = $this->get_bool_value( $this->get_meta( self::META_DPD_PICKUP ) );
$dpd_pickup_point_id = $this->get_meta( self::META_DPD_PICKUP_POINT_ID );
$dpd_cod_value = $this->get_meta( '_dpd_cod_value' );
$dpd_saturday = $this->get_meta( self::META_DPD_SATURDAY );
$dpd_next_day = $this->get_meta( self::META_DPD_NEXT_DAY );
$additional_packages = $this->get_meta( '_additional_packages', [] );
$dpd_one_time_message = $this->get_meta( '_dpd_one_time_message', '' );
$dpd_message = $this->get_meta( '_dpd_message' );
$tracking_url = $this->get_tracking_url();
$dpd_package_id = $this->get_meta( self::DPD_PACKAGE_ID );
$dpd_package_number = $this->get_tracking_number();
$label_url = $this->get_label_url();
$label_avaliable = $this->label_avaliable();
$dpd_one_time_message = $this->dpd_one_time_message;
$label_avaliable = $this->label_avaliable();
$dpd_sender_address_id = $this->get_meta( self::DPD_SENDER_ADDRESS_ID, '' );
$sender_address_options = $this->prepare_sender_address_options();
$pickup_points = array_merge( [ '' => __( '-- wybierz --', 'woocommerce-dpd' ) ], self::$pickup_points_manager->get_points( $this->get_bool_value( $dpd_cod ) ) );
ob_start();
$echo = false;
include 'views/order-metabox-content.php';
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Prepare options for sender address select field in metabox.
*
* @return array
*/
private function prepare_sender_address_options() {
$sender_address_options = [ '' => __( 'Adres główny', 'woocommerce-dpd' ) ];
if ( 'yes' === $this->get_shipping_method()->get_option( WPDesk_WooCommerce_DPD_Shipping_Method::ADDITIONAL_SENDER_ADDRESSES_ENABLED, 'no' ) ) {
$additional_addresses = $this->get_shipping_method()->get_additional_sender_addresses();
foreach ( $additional_addresses as $additional_address ) {
$sender_address_options[ $additional_address->get_address_id() ] = $additional_address->get_address_id();
}
}
return $sender_address_options;
}
/**
* Order metabox.
*/
public function order_metabox() {
echo $this->order_metabox_content(); // wpcs: XSS ok.
}
/**
* Metabox title.
*
* @return string
*/
public function get_order_metabox_title() {
return __( 'DPD', 'woocommerce-dpd' );
}
/**
* Ajax request.
*
* @param string $action .
* @param array $data .
*
* @return array|void
* @throws Exception .
*/
public function ajax_request( $action, $data ) {
$ret = [];
if ( 'save' === $action ) {
$this->save_ajax_data( $data );
$this->save();
$ret['message'] = __( 'Przesyłka została zapisana.', 'woocommerce-dpd' );
} elseif ( 'send' === $action ) {
$this->save_ajax_data( $data );
$this->api_create();
$ret['message'] = __( 'Przesyłka została utworzona.', 'woocommerce-dpd' );
} elseif ( 'cancel' === $action ) {
$dpd_package_number = $this->get_meta( '_dpd_package_number' );
$this->save_ajax_data( $data );
$this->api_cancel();
$ret['message'] = __( 'Przesyłka została anulowana.', 'woocommerce-dpd' );
} elseif ( 'refresh' === $action ) {
$this->api_refresh();
} else {
// Translators: action.
throw new Exception( sprintf( __( 'Nieznana akcja: %1$s', 'woocommerce-dpd' ), $action ) );
}
$ret['content'] = $this->order_metabox_content();
return $ret;
}
/**
* Api refresh.
*/
public function api_refresh() {
}
/**
* Save AJAX data.
*
* @param array $data .
*/
public function save_ajax_data( $data ) {
if ( isset( $data['dpd_product'] ) ) {
$this->set_meta( '_dpd_product', $data['dpd_product'] );
} else {
$this->delete_meta( '_dpd_product' );
}
if ( isset( $data['dpd_sender_address_id'] ) ) {
$this->set_meta( self::DPD_SENDER_ADDRESS_ID, wp_unslash( $data['dpd_sender_address_id'] ) );
} else {
$this->delete_meta( self::DPD_SENDER_ADDRESS_ID );
}
if ( isset( $data['dpd_package_weight'] ) ) {
$this->set_meta( '_dpd_package_weight', $data['dpd_package_weight'] );
} else {
$this->delete_meta( '_dpd_package_weight' );
}
if ( isset( $data['dpd_package_width'] ) ) {
$this->set_meta( '_dpd_package_width', $data['dpd_package_width'] );
} else {
$this->delete_meta( '_dpd_package_width' );
}
if ( isset( $data['dpd_package_height'] ) ) {
$this->set_meta( '_dpd_package_height', $data['dpd_package_height'] );
} else {
$this->delete_meta( '_dpd_package_height' );
}
if ( isset( $data['dpd_package_depth'] ) ) {
$this->set_meta( '_dpd_package_depth', $data['dpd_package_depth'] );
} else {
$this->delete_meta( '_dpd_package_depth' );
}
if ( isset( $data['additional_packages'] ) ) {
unset( $data['additional_packages']['_additional_key_'] );
$additional_packages = [];
foreach ( $data['additional_packages'] as $additional_package ) {
$additional_packages[] = $additional_package;
}
$this->set_meta( '_additional_packages', $additional_packages );
} else {
$this->delete_meta( '_additional_packages' );
}
if ( isset( $data['dpd_package_content'] ) ) {
$this->set_meta( '_dpd_package_content', $data['dpd_package_content'] );
} else {
$this->delete_meta( '_dpd_package_content' );
}
if ( isset( $data['dpd_ref'] ) ) {
$this->set_meta( '_dpd_ref', $data['dpd_ref'] );
} else {
$this->delete_meta( '_dpd_ref' );
}
if ( isset( $data['dpd_declared_value'] ) ) {
$this->set_meta( '_dpd_declared_value', $data['dpd_declared_value'] );
} else {
$this->delete_meta( '_dpd_declared_value' );
}
if ( isset( $data['dpd_declared_value_value'] ) ) {
$this->set_meta( '_dpd_declared_value_value', $data['dpd_declared_value_value'] );
} else {
$this->delete_meta( '_dpd_declared_value_value' );
}
if ( isset( $data['dpd_cod'] ) ) {
$this->set_meta( self::META_DPD_COD, $data['dpd_cod'] );
} else {
$this->delete_meta( self::META_DPD_COD );
}
if ( isset( $data['dpd_cod_value'] ) ) {
$this->set_meta( '_dpd_cod_value', $data['dpd_cod_value'] );
} else {
$this->delete_meta( '_dpd_cod_value' );
}
if ( isset( $data['dpd_saturday'] ) ) {
$this->set_meta( self::META_DPD_SATURDAY, $data['dpd_saturday'] );
} else {
$this->delete_meta( self::META_DPD_SATURDAY );
}
if ( isset( $data['dpd_next_day'] ) ) {
$this->set_meta( self::META_DPD_NEXT_DAY, $data['dpd_next_day'] );
} else {
$this->delete_meta( self::META_DPD_NEXT_DAY );
}
$pickup = isset( $data['dpd_pickup'] );
$point_id = isset( $data['dpd_pickup_point_id'] ) ? $data['dpd_pickup_point_id'] : '';
if ( $pickup && $point_id ) {
try {
$this->save_pickup_details( $point_id );
$this->set_meta( self::META_DPD_PICKUP, $data['dpd_pickup'] );
} catch ( HttpClientRequestException $e ) { // phpcs:ignore
// TODO: write exception to log.
}
} else {
$this->delete_meta( self::META_DPD_PICKUP );
$this->delete_meta( self::DELIVERY_FIELD_ID );
$this->delete_meta( self::DELIVERY_FIELD_NAME );
$this->delete_meta( self::DELIVERY_FIELD_ADDRESS );
$this->delete_meta( self::DELIVERY_FIELD_POSTCODE );
$this->delete_meta( self::DELIVERY_FIELD_CITY );
}
}
/**
* @param string $point_id .
*
* @throws HttpClientRequestException
*/
private function save_pickup_details( $point_id ) {
if ( self::TEST_PICKUP_POINT !== $point_id ) {
$point = self::$pickup_points_api->get_point_details( $point_id );
$this->set_meta( self::DELIVERY_FIELD_ID, $point->get_pudo_id() );
$this->set_meta( self::DELIVERY_FIELD_NAME, $point->get_location_hint() );
$this->set_meta( self::DELIVERY_FIELD_ADDRESS, $point->get_address_1() );
$this->set_meta( self::DELIVERY_FIELD_POSTCODE, $point->get_zipcode() );
$this->set_meta( self::DELIVERY_FIELD_CITY, $point->get_city() );
}
$this->set_meta( self::META_DPD_PICKUP_POINT_ID, $point_id );
}
/**
* Get error message.
*
* @return string
*/
public function get_error_message() {
return $this->get_meta( '_dpd_message' );
}
/**
* Get tracking number.
*
* @return string|null
*/
public function get_tracking_number() {
return $this->get_meta( '_dpd_package_number' );
}
/**
* Get tracking url.
*
* @return string|null
*/
public function get_tracking_url() {
$tracking_url = null;
$dpd_package = $this->get_meta( self::DPD_PACKAGE, '' );
if ( '' !== $dpd_package ) {
$tracking_url = 'https://tracktrace.dpd.com.pl/parcelDetails?typ=1';
$count = 1;
if ( ! is_array( $dpd_package->return->Packages->Package->Parcels->Parcel ) ) {
$dpd_package->return->Packages->Package->Parcels->Parcel = [ $dpd_package->return->Packages->Package->Parcels->Parcel ];
}
foreach ( $dpd_package->return->Packages->Package->Parcels->Parcel as $parcel ) {
$tracking_url .= '&p' . $count . '=' . $parcel->Waybill;
$count++;
}
}
return $tracking_url;
}
/**
* Create shipment in API.
*
* @throws Exception .
*/
public function api_create() {
$this->api_create_webapi();
$this->save();
$error_message = $this->get_error_message();
if ( ! empty( $error_message ) ) {
throw new Exception( $error_message );
}
$order = $this->get_order();
$order->add_order_note(
sprintf(
// Translators: package number.
__( 'Do zamówienia została utworzona przesyłka %1$s DPD.', 'woocommerce-dpd' ),
$this->get_meta( '_dpd_package_number' )
),
true
);
}
/**
* Create shipment in WEB API.
*
* @throws Exception .
*/
public function api_create_webapi() {
if ( 'fs-new' !== $this->get_status() && 'fs-failed' !== $this->get_status() ) {
throw new Exception(
sprintf(
// Translators: status.
__( 'Invalid status: %1$s. Package already created?', 'woocommerce-dpd' ),
$this->get_status()
)
);
}
$order = $this->get_order();
$dpd_shipment = new WPDesk_WooCommerce_DPD_Shipment( $this );
$shipment = $dpd_shipment->prepare_shipment();
$api = $this->get_api();
try {
$dpd_package = $api->create_shipment( [ $shipment ] );
if ( ! empty( $dpd_package->return ) && ! empty( $dpd_package->return->Status ) ) {
if ( 'OK' === $dpd_package->return->Status ) {
$this->set_meta( '_dpd_status', 'ok' );
$this->set_meta( self::DPD_PACKAGE, $dpd_package );
$this->set_meta( self::DPD_PACKAGE_ID, $dpd_package->return->Packages->Package->PackageId );
$this->set_meta( '_dpd_session_id', $dpd_package->return->SessionId );
$package_number = '';
if ( ! is_array( $dpd_package->return->Packages->Package->Parcels->Parcel ) ) {
$dpd_package->return->Packages->Package->Parcels->Parcel = [ $dpd_package->return->Packages->Package->Parcels->Parcel ];
}
foreach ( $dpd_package->return->Packages->Package->Parcels->Parcel as $parcel ) {
if ( '' !== $package_number ) {
$package_number .= ', ';
}
$package_number .= $parcel->Waybill;
}
$this->set_meta( '_dpd_package_number', $package_number );
$this->set_meta( '_dpd_session_type', $this->is_domestic() ? self::DOMESTIC : self::INTERNATIONAL );
$this->delete_meta( '_dpd_message' );
$this->update_status( 'fs-confirmed' );
$this->save();
do_action( 'flexible_shipping_shipment_confirmed', $this );
} else {
throw new Exception( $api->parse_shipment_errors( $dpd_package ) );
}
} else {
throw new Exception(
// Translators: api response.
sprintf( __( 'Niepoprawna odpowiedź API DPD: %1$s', 'woocommerce-dpd' ), print_r( $dpd_package, true ) )
);
}
} catch ( Exception $e ) {
$this->set_meta( '_dpd_status', 'error' );
$this->set_meta( '_dpd_message', $e->getMessage() );
$this->update_status( 'fs-failed' );
$this->save();
throw $e;
}
}
/**
* Cancel package in API.
*
* @throws Exception .
*/
public function api_cancel() {
if ( 'fs-confirmed' !== $this->get_status() ) {
throw new Exception(
// Translators: status.
sprintf( __( 'Niepoprawny status przesyłki: %1$s. Przesyłka została anulaowana wcześniej lub protokół został już utworzony?', 'woocommerce-dpd' ), $this->get_status() )
);
}
$dpd_package_number = $this->get_meta( '_dpd_package_number' );
$this->delete_meta( '_dpd_status' );
$this->delete_meta( self::DPD_PACKAGE );
$this->delete_meta( self::DPD_PACKAGE_ID );
$this->delete_meta( '_dpd_session_id' );
$this->delete_meta( '_dpd_package_number' );
$this->delete_meta( '_dpd_message' );
$this->update_status( 'fs-new' );
$this->save();
$order = $this->get_order();
$order->add_order_note(
// Translators: package number.
sprintf( __( 'Przesyłka DPD %s została usunięta.', 'woocommerce-dpd' ), $dpd_package_number ),
true
);
}
/**
* Get label.
*
* @return array
* @throws Exception .
*/
public function get_label() {
if ( ! $this->label_avaliable() ) {
throw new Exception(
// Translators: status.
sprintf( __( 'Etykieta niedostępna dla statusu %1$s.', 'woocommerce-dpd' ), $this->get_status() )
);
}
return $this->api_get_label();
}
/**
* Displays email text: after order table.
*/
public function get_email_after_order_table() {
if ( $this->label_avaliable() ) {
$args = [
'dpd_packages' => [
[
'tracking_url' => $this->get_tracking_url(),
'shipment_id' => $this->get_meta( '_dpd_package_number' ),
],
],
];
echo self::$woocommerce_dpd_plugin->load_template( 'email_after_order_table', 'woocommerce', $args ); // wpcs: XSS ok.
}
}
/**
* Displays after order table on page.
*/
public function get_after_order_table() {
if ( $this->label_avaliable() ) {
$args = [
'dpd_packages' => [
[
'tracking_url' => $this->get_tracking_url(),
'shipment_id' => $this->get_meta( '_dpd_package_number' ),
],
],
];
echo self::$woocommerce_dpd_plugin->load_template( 'order_details_after_order_table', 'woocommerce', $args ); // wpcs: XSS ok.
}
}
/**
* Get label from API.
*
* @return array
*/
public function api_get_label() {
$label_data = [
'label_format' => 'pdf',
'content' => null,
'file_name' => 'dpd_' . $this->get_tracking_number() . '.pdf',
];
$shipping_method = $this->get_shipping_method();
$settings = $shipping_method->settings;
$api = $this->get_api();
$session_type = $this->get_session_type();
$label_settings = $shipping_method->get_label_settings();
$label_format = $label_settings->get_label_format();
$label = $api->get_label(
$this->get_meta( self::DPD_PACKAGE_ID ),
$session_type,
$label_format,
$label_settings->get_label_page_format(),
$label_settings->get_label_type_for_api()
);
$label_data['content'] = $label->return->documentData;
if ( 'ZPL' === $label_format ) {
$label_format = 'zpl';
} elseif ( 'EPL' === $label_format ) {
$label_format = 'epl';
} else {
$label_format = 'pdf';
}
$label_data['label_format'] = $label_format;
$label_data['file_name'] = 'dpd_' . str_replace( ', ', '-', $this->get_tracking_number() ) . '.' . $label_format;
return $label_data;
}
/**
* Get API.
*
* @return mixed
*/
public function get_api() {
$shipping_method = $this->get_shipping_method();
return $shipping_method->get_api();
}
/**
* Get shipping method.
*
* @return WPDesk_WooCommerce_DPD_Shipping_Method
*/
public function get_shipping_method() {
$shipping_methods = WC()->shipping()->shipping_methods;
if ( empty( $shipping_methods ) || ! is_array( $shipping_methods ) || count( $shipping_methods ) === 0 || ! isset( $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ] ) ) {
$shipping_methods = WC()->shipping()->load_shipping_methods();
}
return $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ];
}
/**
* Add shipment from order edit page.
*/
public function admin_add_shipment() {
$order = $this->get_order();
if ( 'cod' === $order->get_payment_method() ) {
$this->set_meta( self::META_DPD_COD, '1' );
}
$this->set_meta( '_dpd_product', 'classic' );
$weight = wc_get_weight( fs_calculate_order_weight( $order ), 'kg' );
$this->set_meta( '_dpd_package_weight', $weight );
// Translators: order number.
$this->set_meta( '_dpd_ref', sprintf( __( 'Zamówienie %s', 'woocommerce-dpd' ), $order->get_order_number() ) );
}
/**
* Is domestic shipment?
*
* @return bool
*/
private function is_domestic() {
if ( 'PL' === $this->get_order()->get_shipping_country() ) {
return true;
}
return false;
}
/**
* Get session type.
* Returns meta _dpd_session_type when shipment already created, or calculated value for shipping country from
* order.
*
* @return string
*/
public function get_session_type() {
return $this->get_meta( '_dpd_session_type', $this->is_domestic() ? self::DOMESTIC : self::INTERNATIONAL );
}
/**
* Get manifest name.
*
* @return string
*/
public function get_manifest_name() {
$manifest_name = $this->is_domestic() ? 'dpd_domestic' : 'dpd_international';
if ( '' !== $this->get_meta( '_dpd_session_type', '' ) ) {
$manifest_name = strtolower( 'dpd_' . $this->get_meta( '_dpd_session_type', '' ) );
}
return $manifest_name;
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* Label settings.
*
* @package WooCommerce DPD
*/
/**
* Can handle label settings.
*/
class WPDesk_WooCommerce_DPD_Label_Settings {
const A4_MULTIPLE = 'A4_MULTIPLE';
const A4 = 'A4';
const LBL_PRINTER = 'LBL_PRINTER';
const BIC3 = 'BIC3';
const BIC3_EXTENDED1 = 'BIC3_EXTENDED1';
const PDF = 'PDF';
const ZPL = 'ZPL';
const EPL = 'EPL';
/**
* @var string
*/
private $label_format;
/**
* @var string
*/
private $label_page_format;
/**
* @var string
*/
private $label_type;
/**
* WPDesk_WooCommerce_DPD_Label_Settings constructor.
*
* @param string $label_format .
* @param string $label_page_format .
* @param string $label_type .
*/
public function __construct( $label_format, $label_page_format, $label_type ) {
$this->label_format = $label_format;
$this->label_page_format = $label_page_format;
$this->label_type = $label_type;
}
/**
* @return string
*/
public function get_label_format() {
return $this->label_format;
}
/**
* @return string
*/
public function get_label_page_format() {
return $this->label_page_format;
}
/**
* @return string
*/
public function get_label_type() {
return $this->label_type;
}
/**
* @return string
*/
public function get_label_type_for_api() {
if ( $this->get_label_type() !== self::LBL_PRINTER ) {
return self::BIC3;
}
return $this->label_type;
}
}

View File

@@ -0,0 +1,124 @@
<?php
/**
* Labels builder.
*
* @package Woocommerce Dpd
*/
/**
* Can build labels.
*/
class WPDesk_Flexible_Shipping_Dpd_Label_Builder extends WPDesk_Flexible_Shipping_Integration_Label_Builder {
/**
* Get labels for shipments.
*
* @return array
*
* @throws Exception .
*/
public function get_labels_for_shipments() {
$shipping_method = $this->get_shipping_method();
if ( WPDesk_WooCommerce_DPD_Label_Settings::A4_MULTIPLE === $shipping_method->get_option( WPDesk_WooCommerce_DPD_Shipping_Method::OPTION_LABEL_PAGE_FORMAT, '' ) ) {
return $this->get_labels_as_single_file_per_session_type();
} else {
return $this->get_labels_as_label_per_file();
}
}
/**
* Get labels as label per file.
*
* @return array
*
* @throws Exception .
*/
private function get_labels_as_single_file_per_session_type() {
$labels = array();
$packages_domestic = array();
$packages_international = array();
/** @var WPDesk_Flexible_Shipping_Shipment_dpd $shipment */ // phpcs:ignore
foreach ( $this->shipments as $shipment ) {
if ( $shipment->label_avaliable() ) {
$package_id = $shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::DPD_PACKAGE_ID );
if ( $shipment->get_session_type() === $shipment::DOMESTIC ) {
$packages_domestic[] = $package_id;
} else {
$packages_international[] = $package_id;
}
}
}
if ( count( $packages_domestic ) ) {
$labels[] = $this->get_labels_from_api_for_parcels( WPDesk_Flexible_Shipping_Shipment_dpd::DOMESTIC, $packages_domestic );
}
if ( count( $packages_international ) ) {
$labels[] = $this->get_labels_from_api_for_parcels( WPDesk_Flexible_Shipping_Shipment_dpd::INTERNATIONAL, $packages_international );
}
return $labels;
}
/**
* Get labels from api for parcels.
*
* @param string $session_type .
* @param array $packages .
*
* @return array
*
* @throws Exception .
*/
private function get_labels_from_api_for_parcels( $session_type, $packages ) {
$api = $this->get_shipping_method()->get_api();
$label_response = $api->get_labels_in_single_file(
$packages,
$session_type,
WPDesk_WooCommerce_DPD_Label_Settings::PDF,
WPDesk_WooCommerce_DPD_Label_Settings::A4,
WPDesk_WooCommerce_DPD_Label_Settings::BIC3
);
$label_format = strtolower( WPDesk_WooCommerce_DPD_Label_Settings::PDF );
return array(
'label_format' => $label_format,
'content' => $label_response->return->documentData,
'file_name' => 'dpd_labels_' . strtolower( $session_type ) . '.' . $label_format,
);
}
/**
* Get labels as label per file.
*
* @return array
*
* @throws Exception .
*/
private function get_labels_as_label_per_file() {
$labels = array();
/** @var WPDesk_Flexible_Shipping_Shipment_dpd $shipment */ // phpcs:ignore
foreach ( $this->shipments as $shipment ) {
if ( $shipment->label_avaliable() ) {
$labels[] = $shipment->get_label();
}
}
return $labels;
}
/**
* Get shipping method.
*
* @return WPDesk_WooCommerce_DPD_Shipping_Method
*/
private function get_shipping_method() {
$shipping_methods = WC()->shipping()->shipping_methods;
if ( empty( $shipping_methods ) || ! is_array( $shipping_methods ) || count( $shipping_methods ) === 0 || ! isset( $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ] ) ) {
$shipping_methods = WC()->shipping()->load_shipping_methods();
}
return $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ];
}
}

View File

@@ -0,0 +1,13 @@
<?php
/**
* Manifest creation exception.
*
* @package WooCommerce DPD
*/
/**
* Thrown by DPD manifest.
*/
class WPDesk_WooCommerce_DPD_Manifest_Creation_Exception extends RuntimeException {
}

View File

@@ -0,0 +1,495 @@
<?php
/**
* Shipping method.
*
* @package WooCommerce DPD
*/
use DPDVendor\Psr\Log\NullLogger;
use DPDVendor\WPDesk\Logger\WPDeskLoggerFactory;
use DPDVendor\WPDesk\PickupPoints\WooCommerceSettings\RefreshPickupPointsTrait;
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\Fields\SenderAddress\SettingsField;
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\Fields\SenderAddress\ShippingMethodRenderSettingsTrait;
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\SenderAddress;
use DPDVendor\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\ApiConnectionRenderer;
use DPDVendor\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\HasApiConnectionStatusField;
/**
* DPD Shipping method.
*/
class WPDesk_WooCommerce_DPD_Shipping_Method extends WC_Shipping_Method implements HasApiConnectionStatusField {
use ApiConnectionRenderer;
use ShippingMethodRenderSettingsTrait;
use RefreshPickupPointsTrait;
const SHIPPING_METHOD_ID = 'dpd';
const ADDITIONAL_SENDER_ADDRESSES = 'additional_sender_addresses';
const ADDITIONAL_SENDER_ADDRESSES_ENABLED = 'additional_sender_addresses_enabled';
const OPTION_LABEL_PAGE_FORMAT = 'label_page_format';
const OPTION_LABEL_FORMAT = 'label_format';
const OPTION_LABEL_TYPE = 'label_type';
const OPTION_SENDER_COMPANY = 'sender_company';
const OPTION_SENDER_NAME = 'sender_name';
const OPTION_SENDER_ADDRESS = 'sender_address';
const OPTION_SENDER_POSTAL_CODE = 'sender_postal_code';
const OPTION_SENDER_CITY = 'sender_city';
const OPTION_SENDER_PHONE = 'sender_phone';
const OPTION_SENDER_EMAIL = 'sender_email';
const OPTION_LOGGING = 'logging';
/**
* API login.
*
* @var string
*/
private $login;
/**
* API password.
*
* @var string
*/
private $password;
/**
* API fid.
*
* @var string
*/
private $fid;
/**
* API test mode setting.
*
* @var string
*/
private $test_mode;
/**
* DPD API.
*
* @var bool|WPDesk_WooCommerce_DPD_API
*/
public $api = false;
/**
* @param int $instance_id .
*/
public function __construct( $instance_id = 0 ) {
$this->instance_id = absint( $instance_id );
$this->id = self::SHIPPING_METHOD_ID;
$this->method_title = __( 'DPD', 'woocommerce-dpd' );
$this->method_description = sprintf(
// Translators: docs link.
__( 'Integracja WooCommerce z DPD. %1$sZapoznaj się z instrukcją obsługi &rarr;%2$s', 'woocommerce-dpd' ),
'<a href="https://www.wpdesk.pl/docs/woocommerce-dpd-docs/" target="_blank">',
'</a>'
);
$this->enabled = 'yes';
$this->title = __( 'DPD', 'woocommerce-dpd' );
$this->settings['enabled'] = 'yes';
$this->init();
add_action( 'woocommerce_update_options_shipping_' . $this->id, [ $this, 'process_admin_options' ] );
}
/**
* .
*/
public function init() {
$this->init_settings();
$this->init_form_fields();
$this->login = $this->get_option( 'login' );
$this->password = $this->get_option( 'password' );
$this->fid = $this->get_option( 'fid' );
$this->test_mode = $this->get_option( 'test_mode', 'no' );
if ( 'yes' === $this->test_mode ) {
$this->login = $this->get_option( 'login_testmode' );
$this->password = $this->get_option( 'password_testmode' );
$this->fid = $this->get_option( 'fid_testmode' );
}
}
/**
* Get DPD API.
*
* @return bool|WPDesk_WooCommerce_DPD_API
*/
public function get_api() {
if ( ! $this->api ) {
$disable_ssl_verification = apply_filters( 'woocommerce_dpd_disable_ssl_verification', false );
$disable_cache_wsdl = apply_filters( 'woocommerce_dpd_disable_cache_wsdl', false );
if ( $this->get_option( self::OPTION_LOGGING, 'no' ) === 'yes' ) {
$logger = ( new WPDeskLoggerFactory() )->createWPDeskLogger();
} else {
$logger = new NullLogger();
}
$this->api = new WPDesk_WooCommerce_DPD_API(
new WPDesk_WooCommerce_DPD_Auth_Data(
$this->login,
$this->password,
$this->fid
),
$logger,
'yes' === $this->test_mode,
$disable_ssl_verification,
$disable_cache_wsdl
);
}
return $this->api;
}
/**
* Initialise Settings Form Fields
*/
public function init_form_fields() {
$order_statuses = wc_get_order_statuses();
$flexible_printing = apply_filters( 'flexible_printing', false );
$auto_print_custom_attributes = [];
if ( $flexible_printing ) {
if ( 'yes' === $this->get_option( 'auto_print', '' ) ) {
$flexible_printing_integration_url = apply_filters( 'flexible_printing_integration_url', self::SHIPPING_METHOD_ID );
// Translators: printing settings.
$auto_print_description = sprintf(
// Translators: link.
__( 'Aby przejść do ustawień wydruku kliknij %1$stutaj%2$s.', 'woocommerce-dpd' ),
'<a target="_blank" href="' . $flexible_printing_integration_url . '">',
'</a>'
);
} else {
$auto_print_description = __( 'Konfiguracja wydruku dostępna po włączeniu opcji i zapisaniu ustawień.', 'woocommerce-dpd' );
}
} else {
$this->settings['auto_print'] = 'no';
$flexible_printing_buy_url = 'https://www.wpdesk.pl/sklep/flexible-printing/';
$auto_print_description = sprintf(
// Translators: buy link.
__( 'Drukuj etykiety bezpośrednio na drukarce (bez pobierania pliku) lub zapisuj automatycznie na dysku. %1$sKup Flexible Printing &rarr;%2$s.', 'woocommerce-dpd' ),
'<a href="' . $flexible_printing_buy_url . '" target="_blank">',
'</a>'
);
$auto_print_custom_attributes = [ 'disabled' => 'disabled' ];
}
$settings = [
[
'title' => __( 'Logowanie', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
'login' => [
'title' => __( 'Login', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'description' => __( 'Login do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
'custom_attributes' => [
'required' => 'required',
],
],
'password' => [
'title' => __( 'Hasło', 'woocommerce-dpd' ) . '*',
'type' => 'password',
'description' => __( 'Hasło do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
'custom_attributes' => [
'required' => 'required',
'autocomplete' => 'new-password',
],
],
'fid' => [
'title' => __( 'Nr klienta', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'desc_tip' => false,
'custom_attributes' => [
'required' => 'required',
],
],
'test_mode' => [
'title' => __( 'Tryb testowy', 'woocommerce-dpd' ),
'label' => __( 'Włącz tryb testowy', 'woocommerce-dpd' ),
'type' => 'checkbox',
'description' => __( 'Jeśli włączasz tryb testowy - wpisz Login, Hasło i Nr. klienta do API testowego, a nie serwisu produkcyjnego.', 'woocommerce-dpd' ),
'default' => 'no',
'desc_tip' => false,
],
'login_testmode' => [
'title' => __( 'Login dla trybu testowego', 'woocommerce-dpd' ),
'type' => 'text',
'description' => __( 'Login do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
],
'password_testmode' => [
'title' => __( 'Hasło dla trybu testowego', 'woocommerce-dpd' ),
'type' => 'password',
'description' => __( 'Hasło do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
],
'fid_testmode' => [
'title' => __( 'Nr klienta dla trybu testowego', 'woocommerce-dpd' ),
'type' => 'text',
'default' => '',
'desc_tip' => false,
'custom_attributes' => [],
],
'api_status' => [
'title' => __( 'Status połączenia', 'woocommerce-dhl' ),
'type' => \DPDVendor\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\ApiConnectionStatus::FIELD_TYPE,
'default' => '',
'desc_tip' => true,
'custom_attributes' => [
'required' => 'required',
'autocomplete' => 'new-password'
]
],
[
'title' => __( 'Tworzenie przesyłek', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
'auto_create' => [
'title' => __( 'Tworzenie przesyłek', 'woocommerce-dpd' ),
'type' => 'select',
'default' => 'manual',
'options' => [
'manual' => __( 'Ręczne', 'woocommerce-dpd' ),
'auto' => __( 'Automatyczne', 'woocommerce-dpd' ),
],
],
'order_status' => [
'title' => __( 'Status zamówienia', 'woocommerce-dpd' ),
'type' => 'select',
'default' => 'wc-completed',
'options' => $order_statuses,
'description' => __( 'Status zamówienia, przy którym do zamówienia zostanie automatycznie utworzona przesyłka.', 'woocommerce-dpd' ),
],
'complete_order' => [
'title' => __( 'Zrealizuj zamówienie', 'woocommerce-dpd' ),
'type' => 'checkbox',
'label' => __( 'Włącz zmianę statusu zamówienia na Zrealizowane', 'woocommerce-dpd' ),
'default' => 'no',
'description' => __( 'Po nadaniu przesyłek status zamówienia zostanie automatycznie zmieniony na Zrealizowane.', 'woocommerce-dpd' ),
],
[
'title' => __( 'Etykiety', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
self::OPTION_LABEL_FORMAT => [
'title' => __( 'Format etykiety', 'woocommerce-dpd' ),
'type' => 'select',
'options' => [
WPDesk_WooCommerce_DPD_Label_Settings::PDF => __( 'PDF', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::ZPL => __( 'ZPL', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::EPL => __( 'EPL', 'woocommerce-dpd' ),
],
'default' => 'PDF',
],
self::OPTION_LABEL_PAGE_FORMAT => [
'title' => __( 'Format strony etykiety', 'woocommerce-dpd' ),
'type' => 'select',
'options' => [
WPDesk_WooCommerce_DPD_Label_Settings::A4 => __( 'A4 - jedna etykieta na stronę', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::A4_MULTIPLE => __( 'A4 - wiele etykiet na stronę', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::LBL_PRINTER => __( 'Etykieciarka', 'woocommerce-dpd' ),
],
'default' => WPDesk_WooCommerce_DPD_Label_Settings::A4_MULTIPLE,
'desc_tip' => true,
'description' => sprintf(
// Translators: strong.
__( 'Dla opcji %1$sA4 - wiele etykiet na stronę%2$s, etykiety przesyłek zagranicznych drukowane są w osobnym pliku.', 'woocommerce-dpd' ),
'<strong>',
'</strong>'
),
],
self::OPTION_LABEL_TYPE => [
'title' => __( 'Typ etykiety', 'woocommerce-dpd' ),
'type' => 'select',
'options' => [
WPDesk_WooCommerce_DPD_Label_Settings::BIC3 => __( '15x10 cm (BIC3)', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::BIC3_EXTENDED1 => __( '17x10 cm (BIC3_EXT)', 'woocommerce-dpd' ),
],
'default' => WPDesk_WooCommerce_DPD_Label_Settings::BIC3,
],
'auto_print' => [
'title' => __( 'Drukowanie', 'woocommerce-dpd' ),
'label' => __( 'Włącz automatyczne drukowanie', 'woocommerce-dpd' ),
'type' => 'checkbox',
'description' => $auto_print_description,
'custom_attributes' => $auto_print_custom_attributes,
'default' => 'no',
'desc_tip' => false,
],
[
'title' => __( 'Dane nadawcy (adres główny)', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
self::OPTION_SENDER_COMPANY => [
'title' => __( 'Firma', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_NAME => [
'title' => __( 'Imię i nazwisko', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_ADDRESS => [
'title' => __( 'Adres', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_POSTAL_CODE => [
'title' => __( 'Kod pocztowy', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_CITY => [
'title' => __( 'Miasto', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_PHONE => [
'title' => __( 'Telefon', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_EMAIL => [
'title' => __( 'E-mail', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => get_option( 'admin_email' ),
'custom_attributes' => [
'required' => 'required',
],
],
self::ADDITIONAL_SENDER_ADDRESSES_ENABLED => [
'title' => __( 'Dodatkowe adresy', 'woocommerce-dpd' ),
'label' => __( 'Włącz konfigurację dodatkowych adresów nadania', 'woocommerce-dpd' ),
'type' => 'checkbox',
'description' => __( 'Adres nadawcy możesz wybrać w metabox DPD w edycji zamówienia.', 'woocommerce-dpd' ),
'default' => 'no',
'desc_tip' => false,
],
self::ADDITIONAL_SENDER_ADDRESSES => [
'title' => '',
'type' => SettingsField::FIELD_TYPE,
'default' => '{}',
],
[
'title' => __( 'Opcje zaawansowane', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
self::OPTION_LOGGING => [
'title' => __( 'Zapis zdarzeń', 'woocommerce-dpd' ),
'label' => __( 'Włącz zapis zdarzeń do logów', 'woocommerce-dpd' ),
'type' => 'checkbox',
// Translators: WC logger URL.
'description' => sprintf( __( 'Zapis zdarzeń należy włączać tylko podczas diagnozowania problemów. Zdarzenia zapisywane są w %1$sdzienniku WooCommerce%2$s.', 'woocommerce-dpd' ), '<a href="' . admin_url( 'admin.php?page=wc-status&tab=logs' ) . '" target="_blank">', '</a>' ),
'custom_attributes' => [],
'default' => 'no',
'desc_tip' => false,
],
'refresh_points' => [
'type' => 'refresh_pickup_points',
'service' => 'dpd',
],
];
$this->form_fields = $settings;
}
/**
* @param array $form_fields .
* @param bool $echo .
*
* @return string|void .
*/
public function generate_settings_html( $form_fields = [], $echo = false ) {
parent::generate_settings_html( $form_fields );
$lbl_printer = WPDesk_WooCommerce_DPD_Label_Settings::LBL_PRINTER;
include __DIR__ . '/views/shipping-method-settings-script.php';
}
/**
* Check API connection.
*/
public function check_connection() {
$api = $this->get_api();
$api->ping();
}
/**
* @param array $package .
*/
public function calculate_shipping( $package = [] ) {
}
/**
* @return bool|void
*/
public function process_admin_options() {
parent::process_admin_options();
$api = $this->get_api();
$api->clear_cache();
}
/**
* Return array of SenderAddress.
*
* @return SenderAddress[]
*/
public function get_additional_sender_addresses() {
return SenderAddress::create_sender_addresses_from_settings( $this->get_option( self::ADDITIONAL_SENDER_ADDRESSES, '{}' ) );
}
/**
* @return WPDesk_WooCommerce_DPD_Label_Settings
*/
public function get_label_settings() {
return new WPDesk_WooCommerce_DPD_Label_Settings(
$this->get_option( self::OPTION_LABEL_FORMAT ),
$this->get_option( self::OPTION_LABEL_PAGE_FORMAT ),
$this->get_option( self::OPTION_LABEL_TYPE )
);
}
}

View File

@@ -0,0 +1,300 @@
<?php
/**
* Plugin.
*
* @package Woocommerce Dpd
*/
use DPDVendor\Octolize\Tracker\TrackerInitializer;
use DPDVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
use DPDVendor\WPDesk\View\Renderer\SimplePhpRenderer;
use DPDVendor\WPDesk\View\Resolver\ChainResolver;
use DPDVendor\WPDesk\View\Resolver\DirResolver;
use DPDVendor\WPDesk\View\Resolver\WPThemeResolver;
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\Fields\SenderAddress\Assets;
use FSVendor\WPDesk\FS\Shipment\Label\LabelsBulkActionHandler;
use WPDesk\DPD\HookableObjects;
/**
* Main class for WordPress plugin
*/
class WPDesk_WooCommerce_DPD_Plugin extends AbstractPlugin {
/**
* Plugin path
*
* @var string
*/
public $plugin_path;
/**
* Template path
*
* @var string
*/
public $template_path;
/**
* Plugin text domain
*
* @var string
*/
public $plugin_text_domain;
/**
* Settings TAB
*
* @var string
*/
public $default_settings_tab;
/**
* Default view arguments
*
* @var array
*/
public $default_view_args;
/**
* Scripts version
*
* @var string
*/
public $scripts_version = WOOCOMMERCE_DPD_VERSION . '.3';
/**
* WPDesk_WooCommerce_DPD instance
*
* @var WPDesk_WooCommerce_DPD
*/
public $dpd;
/**
* Is integrated with Felxible Printing
*
* @var bool
*/
public $flexible_printing_integration = false;
/**
* @var DPDVendor\WPDesk\View\Renderer\Renderer
*/
private $renderer;
/**
* WPDesk_WooCommerce_DPD_Plugin constructor.
*
* @param \DPDVendor\WPDesk_Plugin_Info $plugin_info Plugin data.
*/
public function __construct( \DPDVendor\WPDesk_Plugin_Info $plugin_info ) {
$this->plugin_info = $plugin_info;
parent::__construct( $this->plugin_info );
}
/**
* Init base variables for plugin
*/
public function init_base_variables() {
$this->plugin_url = $this->plugin_info->get_plugin_url();
$this->plugin_path = $this->plugin_info->get_plugin_dir();
$this->template_path = $this->plugin_info->get_text_domain();
$this->plugin_text_domain = $this->plugin_info->get_text_domain();
$this->plugin_namespace = $this->plugin_info->get_text_domain();
$this->template_path = $this->plugin_info->get_text_domain();
$this->settings_url = admin_url( 'admin.php?page=wc-settings&tab=shipping&section=dpd' );
$this->default_settings_tab = 'main';
$this->default_view_args = [
'plugin_url' => $this->get_plugin_url(),
];
}
/**
* Init classes
*/
public function init() {
( new WPDesk_DPD_Tracker() )->hooks();
TrackerInitializer::create_from_plugin_info_for_shipping_method( $this->plugin_info, WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID )->hooks();
add_action(
'plugins_loaded',
function () {
if ( class_exists( 'WPDesk_Flexible_Shipping_Shipment' ) ) {
require_once $this->plugin_path . '/classes/class-flexible-shipping-shipment.php';
WPDesk_Flexible_Shipping_Shipment_dpd::$woocommerce_dpd_plugin = $this;
}
}
);
add_action( 'plugins_loaded', [ $this, 'init_labels_builder' ] );
$this->init_renderer();
parent::init();
}
/**
* Init renderer.
*/
private function init_renderer() {
$resolver = new ChainResolver();
$resolver->appendResolver( new WPThemeResolver( $this->template_path ) );
$resolver->appendResolver( new DirResolver( trailingslashit( $this->plugin_path ) . 'templates' ) );
$this->renderer = new SimplePhpRenderer( $resolver );
}
/**
* Init labels builder.
*/
public function init_labels_builder() {
if ( class_exists( '\FSVendor\WPDesk\FS\Shipment\Label\LabelsBulkActionHandler' ) ) {
$labels_bulk_actions_handler = LabelsBulkActionHandler::get_labels_bulk_actions_handler();
$labels_bulk_actions_handler->add_builder( new WPDesk_Flexible_Shipping_Dpd_Label_Builder( WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ) );
}
}
/**
* Fires hooks
*/
public function hooks() {
parent::hooks();
add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
add_filter( 'flexible_printing_integrations', [ $this, 'flexible_printing_integrations' ] );
add_filter( 'flexible_shipping_has_manifests', '__return_true' );
add_action( 'woocommerce_init', [ $this, 'init_hookable_objects' ] );
}
/**
* @return void
*/
public function init_hookable_objects() {
( new HookableObjects( $this->get_plugin_assets_url(), $this->scripts_version, WC()->cart, $this->get_plugin_url(), WC()->session, $this->renderer ) )->hooks();
}
/**
* Load class
*/
public function plugins_loaded() {
if ( function_exists( 'fs_get_order_shipments' ) ) {
$this->dpd = WPDesk_WooCommerce_DPD::get_instance( $this );
new WPDesk_WooCommerce_DPD_FS_Hooks( $this );
}
}
/**
* Add integration with Flexible Printing
*
* @param array $integrations Integrations list.
*
* @return array
*/
public function flexible_printing_integrations( array $integrations ) {
$this->flexible_printing_integration = new WPDesk_WooCommerce_DPD_Flexible_Printing_Integration( $this );
$integrations[ $this->flexible_printing_integration->id ] = $this->flexible_printing_integration;
return $integrations;
}
/**
* Register/queue frontend scripts.
*/
public function wp_enqueue_scripts() {
parent::wp_enqueue_scripts();
if ( is_checkout() ) {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_style( 'dpd_checkout_css', $this->get_plugin_assets_url() . 'css/checkout' . $suffix . '.css', [], $this->scripts_version );
wp_enqueue_style( 'dpd_checkout_css' );
wp_enqueue_script( 'dpd_checkout_js', $this->get_plugin_assets_url() . 'js/checkout' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
wp_localize_script(
'dpd_checkout_js',
'__jsDpdVars',
[
'map_url' => trailingslashit( $this->plugin_url ) . 'pickup-point-map.php',
'l10n' => [
'close_popup' => __( 'Zamknij mapę', 'woocommerce-dpd' ),
],
]
);
}
}
/**
* Register/queue admin scripts.
*/
public function admin_enqueue_scripts() {
$current_screen = get_current_screen();
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
if ( 'woocommerce_page_wc-settings' === $current_screen->id ) {
$sender_address_assets = new Assets();
$sender_address_assets->enqueue( $this->get_plugin_assets_url() . '/../../vendor_prefixed/wpdesk/wp-settings-field-sender-address', $suffix, $this->scripts_version );
}
if ( in_array( $current_screen->id, [ 'shop_order', 'edit-shop_order', 'woocommerce_page_wc-orders' ], true ) ) {
wp_register_style( 'dpd_admin_css', $this->get_plugin_assets_url() . 'css/admin' . $suffix . '.css', [], $this->scripts_version );
wp_enqueue_style( 'dpd_admin_css' );
wp_enqueue_script( 'dpd_admin_order_js', $this->get_plugin_assets_url() . 'js/admin-order' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
wp_localize_script( 'dpd_admin_order_js', 'dpd_ajax_object', [ 'ajax_url' => admin_url( 'admin-ajax.php' ) ] );
}
}
/**
* Add action links
*
* @param array $links Plugin action links.
*
* @return array
*/
public function links_filter( $links ) {
$plugin_links = [
'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=shipping&section=dpd' ) . '">' . __( 'Ustawienia', 'woocommerce-dpd' ) . '</a>',
'docs' => '<a target="_blank" href="https://www.wpdesk.pl/docs/woocommerce-dpd-docs/">' . __( 'Dokumentacja', 'woocommerce-dpd' ) . '</a>',
'support' => '<a target="_blank" href="https://www.wpdesk.pl/support/">' . __( 'Wsparcie', 'woocommerce-dpd' ) . '</a>',
];
return array_merge( $plugin_links, $links );
}
/**
* Renders and returns selected template
*
* @param string $name name of the template.
* @param string $path additional inner path to the template.
* @param array $args args accesible from template.
*
* @return string
*/
public function load_template( $name, $path = '', $args = [] ) {
$plugin_template_path = trailingslashit( $this->plugin_path ) . 'templates/';
// Look within passed path within the theme - this is priority.
$template = locate_template(
[
trailingslashit( $this->get_template_path() ) . trailingslashit( $path ) . $name . '.php',
]
);
if ( ! $template ) {
$template = $plugin_template_path . trailingslashit( $path ) . $name . '.php';
}
extract( $args ); // phpcs:ignore
ob_start();
include $template;
return ob_get_clean();
}
/**
* Get template path
*
* @return string
*/
public function get_template_path() {
return trailingslashit( $this->template_path );
}
}

View File

@@ -0,0 +1,264 @@
<?php
/**
* DPD Shipment.
*
* @package Woocommerce Dpd
*/
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\SenderAddress;
use WPDesk\DPD\SanitizeTrait;
/**
* Can prepare DPD shipment data from Flexible Shipping shipment.
*/
class WPDesk_WooCommerce_DPD_Shipment {
use SanitizeTrait;
const ADDRESS = 'address';
const CITY = 'city';
const COMPANY = 'company';
const COUNTRY_CODE = 'countryCode';
const EMAIL = 'email';
const FID = 'fid';
const NAME = 'name';
const PHONE = 'phone';
const POSTAL_CODE = 'postalCode';
/**
* @var WPDesk_Flexible_Shipping_Shipment_dpd
*/
private $flexible_shipping_shipment;
/**
* @param WPDesk_Flexible_Shipping_Shipment_dpd $flexible_shipping_shipment .
*/
public function __construct( WPDesk_Flexible_Shipping_Shipment_dpd $flexible_shipping_shipment ) {
$this->flexible_shipping_shipment = $flexible_shipping_shipment;
}
/**
* @param string $postal_code .
*
* @return string
*/
private function format_postal_code( $postal_code ) {
return str_replace( [ '-', ' ' ], '', $postal_code );
}
/**
* Get sender.
*
* @param array $settings .
* @param SenderAddress[] $additional_addresses Additional sender addresses.
*
* @return array
*/
private function get_sender( array $settings, array $additional_addresses ) {
$dpd_sender_address_id = $this->flexible_shipping_shipment->get_meta( '_dpd_sender_address_id', '' );
if ( '' === $dpd_sender_address_id ) {
$sender = [];
$sender[ self::ADDRESS ] = $settings['sender_address'];
$sender[ self::CITY ] = $settings['sender_city'];
$sender[ self::COMPANY ] = $settings['sender_company'];
$sender[ self::COUNTRY_CODE ] = 'PL';
$sender[ self::EMAIL ] = $settings['sender_email'];
$sender[ self::FID ] = $settings[ self::FID ];
if ( 'yes' === $settings['test_mode'] ) {
$sender[ self::FID ] = $settings['fid_testmode'];
}
$sender[ self::NAME ] = $settings['sender_name'];
$sender[ self::PHONE ] = $settings['sender_phone'];
$sender[ self::POSTAL_CODE ] = $this->format_postal_code( $settings['sender_postal_code'] );
} else {
$sender = $this->prepare_sender_from_additional_addresses( $additional_addresses, $dpd_sender_address_id );
}
return $sender;
}
/**
* @param SenderAddress[] $additional_addresses Additional sender addresses.
* @param string $dpd_sender_address_id .
*
* @return array
*/
private function prepare_sender_from_additional_addresses( $additional_addresses, $dpd_sender_address_id ) {
$sender = [];
foreach ( $additional_addresses as $additional_address ) {
if ( $additional_address->get_address_id() === $dpd_sender_address_id ) {
$sender = $this->prepare_sender_from_additional_address( $additional_address );
break;
}
}
return $sender;
}
/**
* @param SenderAddress $additional_address .
*
* @return array
*/
private function prepare_sender_from_additional_address( $additional_address ) {
$sender = [];
$sender[ self::ADDRESS ] = $additional_address->get_address();
$sender[ self::CITY ] = $additional_address->get_city();
$sender[ self::COMPANY ] = $additional_address->get_company();
$sender[ self::COUNTRY_CODE ] = 'PL';
$sender[ self::EMAIL ] = $additional_address->get_email();
$sender[ self::NAME ] = $additional_address->get_name();
$sender[ self::PHONE ] = $additional_address->get_phone();
$sender[ self::POSTAL_CODE ] = $this->format_postal_code( $additional_address->get_postal_code() );
return $sender;
}
/**
* Get receiver.
*
* @param WC_Order $order .
*
* @return array
*/
private function get_receiver( WC_Order $order ) {
$receiver = [];
$address = $order->get_shipping_address_1();
if ( '' !== $order->get_shipping_address_2() ) {
$address .= ' ' . $order->get_shipping_address_2();
}
$receiver[ self::ADDRESS ] = $address;
$receiver[ self::CITY ] = $order->get_shipping_city();
$receiver[ self::COMPANY ] = $order->get_shipping_company();
$receiver[ self::COUNTRY_CODE ] = $order->get_shipping_country();
$receiver[ self::EMAIL ] = $order->get_billing_email();
$receiver[ self::NAME ] = '';
$receiver[ self::NAME ] .= $order->get_shipping_first_name();
$receiver[ self::NAME ] .= ' ';
$receiver[ self::NAME ] .= $order->get_shipping_last_name();
$receiver[ self::PHONE ] = ! empty( $order->get_shipping_phone() ) ? $order->get_shipping_phone() : $order->get_billing_phone();
$receiver[ self::POSTAL_CODE ] = $this->format_postal_code( $order->get_shipping_postcode() );
return $receiver;
}
/**
* Get parcels.
*
* @return array
*/
private function get_parcels() {
$parcels = [];
$parcel = [];
$parcel['content'] = $this->flexible_shipping_shipment->get_meta( '_dpd_package_content' );
$parcel['customerData1'] = $this->flexible_shipping_shipment->get_meta( '_dpd_ref' );
$parcel['sizeX'] = $this->flexible_shipping_shipment->get_meta( '_dpd_package_width' );
$parcel['sizeY'] = $this->flexible_shipping_shipment->get_meta( '_dpd_package_height' );
$parcel['sizeZ'] = $this->flexible_shipping_shipment->get_meta( '_dpd_package_depth' );
$parcel['weight'] = $this->flexible_shipping_shipment->get_meta( '_dpd_package_weight' );
$parcels[] = $parcel;
$additional_packages = $this->flexible_shipping_shipment->get_meta( '_additional_packages', [] );
foreach ( $additional_packages as $additional_key => $additional_package ) {
$parcel = [];
$parcel['content'] = $this->flexible_shipping_shipment->get_meta( '_dpd_package_content' );
$parcel['customerData1'] = $this->flexible_shipping_shipment->get_meta( '_dpd_ref' );
$parcel['sizeX'] = $additional_package['dpd_package_width'];
$parcel['sizeY'] = $additional_package['dpd_package_height'];
$parcel['sizeZ'] = $additional_package['dpd_package_depth'];
$parcel['weight'] = $additional_package['dpd_package_weight'];
$parcels[] = $parcel;
}
return $parcels;
}
/**
* @param array $shipment .
* @param array $settings .
*
* @return array
*/
private function prepare_payer_type( array $shipment, array $settings ) {
$dpd_sender_address_id = $this->flexible_shipping_shipment->get_meta( '_dpd_sender_address_id', '' );
if ( '' === $dpd_sender_address_id ) {
$shipment['payerType'] = 'SENDER';
} else {
$shipment['payerType'] = 'THIRD_PARTY';
$shipment['thirdPartyFID'] = $settings[ self::FID ];
}
return $shipment;
}
/**
* @return array
*/
public function prepare_shipment() {
$shipping_method = $this->flexible_shipping_shipment->get_shipping_method();
$settings = $shipping_method->settings;
$order = $this->flexible_shipping_shipment->get_order();
$shipment = [];
$shipment['sender'] = $this->get_sender( $settings, $shipping_method->get_additional_sender_addresses() );
$shipment['receiver'] = $this->get_receiver( $order );
$shipment['parcels'] = $this->get_parcels();
$shipment = $this->prepare_payer_type( $shipment, $settings );
if ( 'PL' !== $order->get_shipping_country() ) {
$shipment['ref1'] = $this->flexible_shipping_shipment->get_meta( '_dpd_ref' );
$shipment['ref2'] = $this->flexible_shipping_shipment->get_meta( '_dpd_package_content' );
}
$shipment['services'] = [];
if ( intval( $this->flexible_shipping_shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::META_DPD_COD, '0' ) ) === 1 ) {
$shipment['services']['cod'] = [
'amount' => $this->flexible_shipping_shipment->get_meta( '_dpd_cod_value' ),
'currency' => $order->get_currency(),
];
}
if ( intval( $this->flexible_shipping_shipment->get_meta( '_dpd_declared_value', '0' ) ) === 1 ) {
$shipment['services']['declaredValue'] = [
'amount' => $this->flexible_shipping_shipment->get_meta( '_dpd_declared_value_value' ),
'currency' => $order->get_currency(),
];
}
$pickup = $this->get_bool_value( $this->flexible_shipping_shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::META_DPD_PICKUP ) );
$point_id = $this->flexible_shipping_shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::META_DPD_PICKUP_POINT_ID );
if ( $pickup && $point_id ) {
$shipment['services']['dpdPickup'] = [
'pudo' => $point_id,
];
}
if ( (int) $this->flexible_shipping_shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::META_DPD_SATURDAY, '0' ) === 1 ) {
$shipment['services']['guarantee'] = [
'type' => 'SATURDAY',
];
}
if ( (int) $this->flexible_shipping_shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::META_DPD_NEXT_DAY, '0' ) === 1 ) {
$shipment['services']['guarantee'] = [
'type' => 'DPDNEXTDAY',
];
}
return $shipment;
}
}

View File

@@ -0,0 +1,115 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_WooCommerce_DPD' ) ) {
class WPDesk_WooCommerce_DPD {
static $instance = null;
private $plugin = null;
public static function get_instance( WPDesk_WooCommerce_DPD_Plugin $plugin ) {
if ( self::$instance == null ) {
self::$instance = new self( $plugin );
}
return self::$instance;
}
public static function get_products() {
$ret = array(
'classic' => __( 'Classic', 'woocommerce-dpd' ),
);
return $ret;
}
public static function get_package_types() {
return array(
'ENVELOPE' => __( 'Koperta', 'woocommerce-dpd' ),
'PACKAGE' => __( 'Paczka', 'woocommerce-dpd' ),
'PALLET' => __( 'Paleta', 'woocommerce-dpd' ),
);
}
public function __construct( WPDesk_WooCommerce_DPD_Plugin $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
public function hooks() {
add_filter( 'woocommerce_shipping_methods', array( $this, 'woocommerce_shipping_methods' ), 20, 1 );
add_action( 'woocommerce_order_status_changed', array( $this, 'woocommerce_order_status_changed' ), 10, 3 );
add_action( 'flexible_shipping_shipment_confirmed', array( $this, 'flexible_shipping_shipment_confirmed' ), 10, 2 );
add_action( 'flexible_shipping_add_shipping_options', array( $this, 'flexible_shipping_add_shipping_options' ) );
}
public function woocommerce_order_status_changed( $order_id, $old_status, $new_status ) {
$all_shipping_methods = WC()->shipping()->get_shipping_methods();
$dpd = $all_shipping_methods['dpd'];
$settings = $dpd->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 );
$shipments = fs_get_order_shipments( $order_id, 'dpd' );
foreach ( $shipments as $shipment ) {
try {
$shipment->api_create();
}
catch ( Exception $e ) {}
}
}
}
}
public function flexible_shipping_shipment_confirmed( WPDesk_Flexible_Shipping_Shipment $shipment ) {
if ( $shipment->get_meta( '_integration', '' ) != 'dpd' ) {
return;
}
$all_shipping_methods = WC()->shipping()->get_shipping_methods();
$shipping_method = $all_shipping_methods['dpd'];
if ( $shipping_method->get_option( 'complete_order', 'no' ) == 'yes' ) {
$order = $shipment->get_order();
$order->update_status( 'completed', __( 'Status zmieniony automatycznie po utworzeniu przesyłki - wtyczka DPD.', 'woocommerce-dpd' ) );
}
}
public function get_shipping_method() {
$shipping_methods = WC()->shipping()->shipping_methods;
if ( empty( $shipping_methods ) || !is_array( $shipping_methods ) || count( $shipping_methods ) == 0 ) {
$shipping_methods = WC()->shipping()->load_shipping_methods();
}
return $shipping_methods['dpd'];
}
public function get_label( $shipment_id ) {
$shipment = fs_get_shipment( $shipment_id );
return $shipment->get_label();
}
public function get_api() {
$shipping_method = $this->get_shipping_method();
return $shipping_method->get_api();
}
public function woocommerce_shipping_methods( $methods ) {
include_once( 'class-shipping-method.php' );
$methods['dpd'] = 'WPDesk_WooCommerce_DPD_Shipping_Method';
return $methods;
}
public function flexible_shipping_add_shipping_options( $options ) {
$options['dpd'] = 'DPD';
return $options;
}
}
}

View File

@@ -0,0 +1,109 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_DPD_Tracker' ) ) {
class WPDesk_DPD_Tracker {
public function __construct() {
}
public function hooks() {
add_filter( 'wpdesk_tracker_data', array( $this, 'wpdesk_tracker_data_dpd' ), 11 );
}
public function wpdesk_tracker_data_dpd( $data ) {
$shipping_methods = WC()->shipping()->get_shipping_methods();
if ( isset( $shipping_methods['dpd'] ) ) {
$dpd = $shipping_methods['dpd'];
$settings = $dpd->settings;
$plugin_data = array();
if ( isset( $settings['auto_create'] ) ) {
$plugin_data['auto_create'] = $settings['auto_create'];
}
if ( isset( $settings['order_status'] ) ) {
$plugin_data['order_status'] = $settings['order_status'];
}
if ( isset( $settings['complete_order'] ) ) {
$plugin_data['complete_order'] = $settings['complete_order'];
}
if ( isset( $settings['label_format'] ) ) {
$plugin_data['label_format'] = $settings['label_format'];
}
if ( isset( $settings['label_page_format'] ) ) {
$plugin_data['label_page_format'] = $settings['label_page_format'];
}
if ( isset( $settings['label_type'] ) ) {
$plugin_data['label_type'] = $settings['label_type'];
}
if ( isset( $settings['auto_print'] ) ) {
$plugin_data['auto_print'] = $settings['auto_print'];
}
$plugin_data['parcels'] = array();
$all_parcels = 0;
global $wpdb;
$sql = "
SELECT count(p.ID) AS count, p.post_status AS post_status, min(p.post_date) AS min, max(p.post_date) AS max
FROM {$wpdb->posts} p, {$wpdb->postmeta} m
WHERE p.post_type = 'shipment'
AND p.ID = m.post_id
AND m.meta_key = '_integration'
AND m.meta_value = 'dpd'
GROUP BY p.post_status
";
$query = $wpdb->get_results( $sql );
if ( $query ) {
foreach ( $query as $row ) {
$plugin_data['parcels'][$row->post_status] = $row->count;
$all_parcels = $all_parcels + $row->count;
}
}
$plugin_data['all_parcels'] = $all_parcels;
$plugin_data['dpd_product'] = array();
$sql = "
SELECT count(p.ID) AS count, m2.meta_value AS dpd_product
FROM {$wpdb->posts} p, {$wpdb->postmeta} m1, {$wpdb->postmeta} m2
WHERE p.post_type = 'shipment'
AND p.ID = m2.post_id
AND m1.meta_key = '_integration'
AND m1.meta_value = 'dpd'
AND p.ID = m1.post_id
AND m2.meta_key = '_dpd_product'
GROUP BY m2.meta_value
";
$query = $wpdb->get_results( $sql );
if ( $query ) {
foreach ( $query as $row ) {
$plugin_data['dpd_product'][$row->dpd_product] = $row->count;
}
}
$sql = "
SELECT TIMESTAMPDIFF(MONTH, min(p.post_date), max(p.post_date) )+1 AS months
FROM {$wpdb->posts} p, {$wpdb->postmeta} m
WHERE p.post_type = 'shipment'
AND p.ID = m.post_id
AND m.meta_key = '_integration'
AND m.meta_value = 'dpd'
GROUP BY p.post_status
";
$query = $wpdb->get_results( $sql );
if ( $query ) {
foreach ( $query as $row ) {
if ( $row->months != 0 ) {
$plugin_data['avg_parcels_per_month'] = floatval( $all_parcels )/floatval( $row->months );
}
}
}
$data['dpd'] = $plugin_data;
}
return $data;
}
}
}

View File

@@ -0,0 +1,88 @@
<div class="dpd_additional_package" id="dpd_additional_package_<?php echo $id; ?>" data-key="<?php echo $additional_key; ?>" data-id="<?php echo $id; ?>">
<h4><?php echo $additional_package_label; ?></h4>
<div class="dpd-weight">
<?php
$key = 'additional_packages[' . $additional_key . '][dpd_package_weight]';
$args = array(
'label' => __( 'Waga [kg]', 'woocommerce-dpd' ),
'id' => 'dpd_package_weight_' . $additional_key . '_' . $id,
'type' => 'number',
);
$value = '';
if ( isset( $additional_package['dpd_package_weight'] ) ) {
$value = $additional_package['dpd_package_weight'];
}
else {
$value = $dpd_package_weight;
}
if ( $disabled ) {
$args['custom_attributes'] = array( 'disabled' => 'disabled' );
}
woocommerce_form_field( $key, $args, $value );
?>
</div>
<div class="dpd-dimensions">
<label><?php _e( 'Wymiary (dł / sz / gł) [cm]' ); ?></label>
<?php
$key = 'additional_packages[' . $additional_key . '][dpd_package_width]';
$args = array(
'type' => 'number',
'id' => 'dpd_package_width_' . $additional_key . '_' . $id,
);
$value = '';
if ( isset( $additional_package['dpd_package_width'] ) ) {
$value = $additional_package['dpd_package_width'];
}
else {
$value = $dpd_package_width;
}
if ( $disabled ) {
$args['custom_attributes'] = array( 'disabled' => 'disabled' );
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'additional_packages[' . $additional_key . '][dpd_package_height]';
$args = array(
'label' => '/',
'type' => 'number',
'id' => 'dpd_package_height_' . $additional_key . '_' . $id,
);
$value = '';
if ( isset( $additional_package['dpd_package_height'] ) ) {
$value = $additional_package['dpd_package_height'];
}
else {
$value = $dpd_package_height;
}
if ( $disabled ) {
$args['custom_attributes'] = array( 'disabled' => 'disabled' );
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'additional_packages[' . $additional_key . '][dpd_package_depth]';
$args = array(
'label' => '/',
'type' => 'number',
'id' => 'dpd_package_depth_' . $additional_key . '_' . $id,
);
$value = '';
if ( isset( $additional_package['dpd_package_depth'] ) ) {
$value = $additional_package['dpd_package_depth'];
}
else {
$value = $dpd_package_depth;
}
if ( $disabled ) {
$args['custom_attributes'] = array( 'disabled' => 'disabled' );
}
woocommerce_form_field( $key, $args, $value );
?>
</div>
<?php if ( !$disabled ) : ?>
<a data-id="<?php echo $id; ?>" data-key="<?php echo $additional_key; ?>" class="dpd-button dpd-button-delete-package"><?php _e( 'Usuń', 'woocommerce-dpd' ); ?></a>
<?php endif; ?>
<div style="clear: both;"></div>
<hr/>
</div>

View File

@@ -0,0 +1,430 @@
<?php
/**
* Order metabox content.
*
* @var $order \WC_Order
* @var $disabled bool
* @var $dpd_sender_address_id string
* @var $sender_address_options array
* @var $tracking_url string
* @var $dpd_package_number string
* @var $label_url string
* @var $label_avaliable bool
* @var $dpd_pickup bool
* @var $pickup_points array
* @var $dpd_pickup_point_id string
* @package WooCommerce DPD
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; /* Exit if accessed directly */
}
?>
<div class="dpd-package">
<?php
$key = 'dpd_product';
$args = [
'label' => __( 'Usługa DPD', 'woocommerce-dpd' ),
'id' => 'dpd_product_' . $id,
'type' => 'select',
'options' => WPDesk_WooCommerce_DPD::get_products(),
'class' => [ 'dpd-product', 'dpd-hidden' ],
'input_class' => [ 'dpd-product' ],
];
$value = '';
if ( isset( $dpd_product ) ) {
$value = $dpd_product;
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
<hr class="dpd-hidden"/>
<?php
$key = 'dpd_sender_address_id';
$args = [
'label' => __( 'Adres nadawcy', 'woocommerce-dpd' ),
'id' => 'dpd_sender_address_id' . $id,
'type' => 'select',
'options' => $sender_address_options,
'class' => [ 'dpd-sender-address-id' ],
'input_class' => [ 'dpd-sender-address-id' ],
];
$value = '';
if ( isset( $dpd_sender_address_id ) ) {
$value = $dpd_sender_address_id;
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
<div class="dpd_package_data">
<h4>
<?php
// Translators: package number.
echo esc_html( sprintf( __( 'Paczka %1$s', 'woocommerce-dpd' ), 1 ) );
?>
</h4>
<div class="dpd-weight">
<?php
$key = 'dpd_package_weight';
$args = [
'label' => __( 'Waga [kg]', 'woocommerce-dpd' ),
'id' => 'dpd_package_weight_' . $id,
'type' => 'number',
];
$value = '';
if ( isset( $dpd_package_weight ) ) {
$value = $dpd_package_weight;
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
</div>
<div class="dpd-dimensions">
<label><?php echo esc_html( __( 'Wymiary (dł / wys / gł) [cm]', 'woocommerce-dpd' ) ); ?></label>
<?php
$key = 'dpd_package_width';
$args = [
'type' => 'number',
'id' => 'dpd_package_width_' . $id,
];
$value = '';
if ( isset( $dpd_package_width ) ) {
$value = $dpd_package_width;
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_package_height';
$args = [
'label' => '/',
'type' => 'number',
'id' => 'dpd_package_height_' . $id,
];
$value = '';
if ( isset( $dpd_package_height ) ) {
$value = $dpd_package_height;
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_package_depth';
$args = [
'label' => '/',
'type' => 'number',
'id' => 'dpd_package_depth_' . $id,
];
$value = '';
if ( isset( $dpd_package_depth ) ) {
$value = $dpd_package_depth;
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
</div>
</div>
<hr/>
<?php $count_additional = 1; ?>
<?php if ( isset( $additional_packages ) ) : ?>
<?php foreach ( $additional_packages as $additional_key => $additional_package ) : ?>
<?php $count_additional++; ?>
<?php // Translators: package number. ?>
<?php $additional_package_label = sprintf( __( 'Paczka %s', 'woocommerce-dpd' ), $count_additional ); ?>
<?php include __DIR__ . '/order-metabox-additional-package.php'; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php if ( ! $disabled ) : ?>
<p id="dpd_add_package_<?php echo esc_attr( $id ); ?>">
<a data-id="<?php echo esc_attr( $id ); ?>"
data-count_additional="<?php echo esc_attr( $count_additional ); ?>"
class="button dpd-button dpd-button-add-package"><?php echo esc_html( __( 'Dodaj paczkę', 'woocommerce-dpd' ) ); ?>
</a>
<span class="dpd-spinner spinner shipping-spinner"></span>
</p>
<hr/>
<div class="dpd_additional_package_template" id="dpd_additional_package_template_<?php echo esc_attr( $id ); ?>"
style="display: none;">
<?php $additional_key = '_additional_key_'; ?>
<?php $count_additional = '_count_additional_'; ?>
<?php $additional_package_label = __( 'Nowa paczka', 'woocommerce-dpd' ); ?>
<?php include __DIR__ . '/order-metabox-additional-package.php'; ?>
</div>
<?php endif; ?>
<?php
$key = 'dpd_declared_value';
$args = [
'label' => __( 'Zadeklarowana wartość', 'woocommerce-dpd' ),
'id' => 'dpd_declared_value_' . $id,
'type' => 'checkbox',
'input_class' => [ 'dpd-declared_value' ],
];
$value = '0';
if ( isset( $dpd_declared_value ) ) {
$value = $dpd_declared_value;
}
if ( $disabled ) {
$args['input_class'][] = 'dpd-disabled';
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_declared_value_value';
$args = [
'label' => __( 'Zadeklarowana kwota', 'woocommerce-dpd' ),
'id' => 'dpd_declared_value_value_' . $id,
'type' => 'number',
'class' => [ 'dpd-declared_value-value' ],
'custom_attributes' => [
'min' => 0,
'step' => '0.01',
],
];
$value = '';
if ( isset( $dpd_declared_value_value ) ) {
$value = $dpd_declared_value_value;
} else {
$value = $order->get_total();
}
if ( $disabled ) {
$args['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_cod';
$args = [
'label' => __( 'Pobranie', 'woocommerce-dpd' ),
'id' => 'dpd_cod_' . $id,
'type' => 'checkbox',
'input_class' => [ 'dpd-cod' ],
];
$value = '0';
if ( isset( $dpd_cod ) ) {
$value = $dpd_cod;
}
if ( $disabled ) {
$args['input_class'][] = 'dpd-disabled';
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_cod_value';
$args = [
'label' => __( 'Kwota pobrania', 'woocommerce-dpd' ),
'id' => 'dpd_cod_value_' . $id,
'type' => 'number',
'class' => [ 'dpd-cod-value' ],
'custom_attributes' => [
'min' => 0,
'step' => '0.01',
],
];
$value = '';
if ( isset( $dpd_cod_value ) ) {
$value = $dpd_cod_value;
} else {
$value = $order->get_total();
}
if ( $disabled ) {
$args['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_saturday';
$args = [
'label' => __( 'Doręczenie w sobotę', 'woocommerce-dpd' ),
'id' => 'dpd_saturday_' . $id,
'type' => 'checkbox',
'input_class' => [ 'dpd-saturday' ],
];
$value = '0';
if ( isset( $dpd_saturday ) ) {
$value = $dpd_saturday;
}
if ( $disabled ) {
$args['input_class'][] = 'dpd-disabled';
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_next_day';
$args = [
'label' => __( 'Dostawa następnego dnia (Next Day)', 'woocommerce-dpd' ),
'id' => $key . '_' . $id,
'type' => 'checkbox',
'input_class' => [ $key ],
];
$value = '0';
if ( isset( $dpd_next_day ) ) {
$value = $dpd_next_day;
}
if ( $disabled ) {
$args['input_class'][] = 'dpd-disabled';
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_pickup';
$args = [
'label' => __( 'Doręczenie do punktu odbioru', 'woocommerce-dpd' ),
'id' => 'dpd_pickup_' . $id,
'type' => 'checkbox',
'input_class' => [ 'dpd-pickup' ],
];
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, (int) $dpd_pickup );
?>
<?php
$key = 'dpd_pickup_point_id';
$args = [
'id' => 'dpd_pickup_point_id_' . $id,
'type' => 'select',
'input_class' => [ 'dpd-pickup-value' ],
'options' => $pickup_points,
];
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $dpd_pickup_point_id );
?>
<?php
$key = 'dpd_package_content';
$args = [
'label' => __( 'Zawartość', 'woocommerce-dpd' ),
'id' => 'dpd_package_content_' . $id,
'type' => 'text',
];
$value = '';
if ( isset( $dpd_package_content ) ) {
$value = $dpd_package_content;
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
<?php
$key = 'dpd_ref';
$args = [
'label' => __( 'Numer referencyjny', 'woocommerce-dpd' ),
'id' => 'dpd_ref_' . $id,
'type' => 'text',
];
$value = '';
if ( isset( $dpd_ref ) ) {
$value = $dpd_ref;
} else {
$value = str_replace( '[order_number]', $order->get_order_number(), $value );
$value = str_replace( '[shop_name]', get_bloginfo( 'name' ), $value );
$value = str_replace( '[shop_url]', home_url(), $value );
}
if ( $disabled ) {
$args['custom_attributes'] = [ 'disabled' => 'disabled' ];
}
woocommerce_form_field( $key, $args, $value );
?>
<?php if ( isset( $dpd_status ) && 'ok' === $dpd_status ) : ?>
<p>
<?php echo esc_html( __( 'Przesyłka: ', 'woocommerce-dpd' ) ); ?> <a target="_blank"
href="<?php echo esc_attr( $tracking_url ); ?>"><?php echo esc_html( $dpd_package_number ); ?></a>
</p>
<p>
<a
target="_blank" href='<?php echo esc_attr( $label_url ); ?>' data-id="<?php echo esc_attr( $id ); ?>"
class="button button-primary dpd-button"><?php echo esc_html( __( 'Pobierz etykietę', 'woocommerce-dpd' ) ); ?></a>
<span class="spinner dpd-spinner shipping-spinner"></span>
<a
data-id="<?php echo esc_attr( $id ); ?>"
class="dpd-button dpd-button-delete-created button-shipping"><?php echo esc_html( __( 'Anuluj', 'woocommerce-dpd' ) ); ?></a>
</p>
<?php endif; ?>
<?php if ( $label_avaliable ) : ?>
<?php if ( apply_filters( 'flexible_printing', false ) && isset( $settings['flexible_printing_integration'] ) && 'yes' === $settings['flexible_printing_integration'] ) : ?>
<p>
<?php
echo apply_filters( // phpcs: XSS OK.
'flexible_printing_print_button',
'',
'dpd',
[
'content' => 'print',
'id' => $dpd_package_number,
'icon' => true,
// Translators: printer.
'label' => __( 'Drukuj na: %1$s', 'woocommerce-dpd' ),
'data' => [
'shippment_id' => $id,
'dpd_package_number' => $dpd_package_number,
],
]
);
?>
</p>
<?php endif; ?>
<?php endif; ?>
<?php if ( $label_avaliable ) : ?>
<?php if ( apply_filters( 'flexible_printing', false ) ) : ?>
<p>
<?php
echo apply_filters( // phpcs: XSS OK.
'flexible_printing_print_button',
'',
'dpd',
[
'content' => 'print',
'id' => str_replace( ', ', '-', $dpd_package_number ),
'icon' => true,
// Translators: printer.
'label' => __( 'Drukuj na: %1$s', 'woocommerce-dpd' ),
'data' => [
'shippment_id' => $id,
'dpd_package_number' => $dpd_package_number,
],
]
);
?>
</p>
<?php endif; ?>
<?php endif; ?>
<?php if ( ! $disabled ) : ?>
<p>
<button data-id="<?php echo esc_attr( $id ); ?>"
class="button button-primary dpd-button dpd-button-create button-shipping"><?php echo esc_html( __( 'Utwórz', 'woocommerce-dpd' ) ); ?></button>
<button data-id="<?php echo esc_attr( $id ); ?>"
class="button dpd-button dpd-button-save button-shipping"><?php echo esc_html( __( 'Zapisz', 'woocommerce-dpd' ) ); ?></button>
<span class="spinner dpd-spinner shipping-spinner"></span>
</p>
<?php endif; ?>
</div>
<script type="text/javascript">
jQuery(document).ready(function () {
dpd_init();
})
</script>

View File

@@ -0,0 +1,57 @@
<?php
/**
* @var $lbl_printer string
*
* @package WooCommerce DPD
*/
?><script type="text/javascript">
jQuery(document).ready(function () {
function dpd_order_status() {
if (jQuery('#woocommerce_dpd_auto_create').val() === 'auto') {
jQuery('#woocommerce_dpd_order_status').closest('tr').show();
} else {
jQuery('#woocommerce_dpd_order_status').closest('tr').hide();
}
}
function dpd_test_mode() {
if (jQuery('#woocommerce_dpd_test_mode').is(':checked')) {
jQuery('#woocommerce_dpd_login_testmode').closest('tr').show();
jQuery('#woocommerce_dpd_password_testmode').closest('tr').show();
jQuery('#woocommerce_dpd_fid_testmode').closest('tr').show();
} else {
jQuery('#woocommerce_dpd_login_testmode').closest('tr').hide();
jQuery('#woocommerce_dpd_password_testmode').closest('tr').hide();
jQuery('#woocommerce_dpd_fid_testmode').closest('tr').hide();
}
}
function dpd_sender_addresses() {
let sender_addresses_enabled = jQuery('#woocommerce_dpd_additional_sender_addresses_enabled').is(':checked');
jQuery('.settings-field-sender-address').closest('tr').toggle(sender_addresses_enabled);
}
function dpd_label_page_format() {
let toggle = jQuery('#woocommerce_dpd_label_page_format').val() === '<?php echo esc_attr( $lbl_printer ); ?>';
jQuery('#woocommerce_dpd_label_type').closest('tr').toggle(toggle);
}
dpd_order_status();
dpd_test_mode();
dpd_sender_addresses();
dpd_label_page_format();
jQuery('#woocommerce_dpd_auto_create').change(function () {
dpd_order_status();
});
jQuery('#woocommerce_dpd_test_mode').change(function () {
dpd_test_mode();
});
jQuery('#woocommerce_dpd_label_page_format').change(function () {
dpd_label_page_format();
});
jQuery('#woocommerce_dpd_additional_sender_addresses_enabled').change(function () {
dpd_sender_addresses();
});
})
</script>