Files
krolewskie-miody.pl/wp-content/plugins/woocommerce-paczkomaty-inpost/class/class-flexible-shipping-shipment-checkout.php
2026-04-28 15:13:50 +02:00

329 lines
12 KiB
PHP

<?php
/**
* Checkout.
*
* @package PaczkomatyInpost
*/
use FS\InPost\PickupPoints\PickupPointsManager;
/**
* Handles shipment metadata creation on checkout.
*/
class WPDesk_Flexible_Shipping_Shipment_Paczkomaty_Checkout {
use WPDesk_Flexible_Shipping_Shipment_Paczkomaty_Filtered_Meta;
/**
* Shipment.
*
* @var WPDesk_Flexible_Shipping_Shipment_Paczkomaty
*/
private $shipment;
/**
* Global Paczkomaty shipping method.
*
* @var WC_Paczkomaty_Shipping_Method.
*/
private $paczkomaty_shipping_method;
/**
* Shipment options.
*
* @var WPDesk_Paczkomaty_Additional_Service[]
*/
private $shipment_options;
/**
* Flexible shipping method settings.
*
* @var array
*/
private $flexible_shipping_method;
/**
* WooCommerce shipping package.
*
* @var array
*/
private $package;
/**
* @var PickupPointsManager
*/
private $pickup_points_manager;
/**
* WPDesk_Flexible_Shipping_Shipment_Paczkomaty_Checkout constructor.
*
* @param WPDesk_Flexible_Shipping_Shipment_Paczkomaty $shipment .
* @param WC_Paczkomaty_Shipping_Method $paczkomaty_shipping_method .
* @param WPDesk_Paczkomaty_Additional_Service[] $shipment_options .
* @param array $flexible_shipping_method .
* @param array $package .
*/
public function __construct(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty $shipment,
WC_Paczkomaty_Shipping_Method $paczkomaty_shipping_method,
array $shipment_options,
array $flexible_shipping_method,
array $package,
PickupPointsManager $pickup_points_manager
) {
$this->shipment = $shipment;
$this->paczkomaty_shipping_method = $paczkomaty_shipping_method;
$this->shipment_options = $shipment_options;
$this->flexible_shipping_method = $flexible_shipping_method;
$this->package = $package;
$this->pickup_points_manager = $pickup_points_manager;
}
/**
* Set meta on shipment.
*
* @param string $meta_key .
* @param string|int $meta_value .
*/
private function set_meta_filtered( $meta_key, $meta_value ) {
$this->shipment->set_meta(
$meta_key,
$this->get_meta_filtered_value(
$meta_key,
$meta_value,
$this->flexible_shipping_method,
$this->package,
$this->shipment->get_order()
)
);
}
/**
* Set common meta.
*
* @param array $fs_method Flexible Shipping Method Settings.
*/
private function set_meta_common( array $fs_method ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_DROPOFF_PARCEL_MACHINE,
$this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_DEFAULT_PARCEL_LOCKER )
);
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_DROPOFF_POP,
$this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_DEFAULT_POP )
);
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_DROPOFF_DISPATCH_POINT,
$this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_DEFAULT_DISPATCH_POINT )
);
if ( isset( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_UBEZPIECZENIE ] ) ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_UBEZPIECZENIE,
$fs_method[ WPDesk_Paczkomaty_FS::SETTING_UBEZPIECZENIE ]
);
} else {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_UBEZPIECZENIE,
'0'
);
}
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_UBEZPIECZENIE_AMOUNT,
$this->shipment->get_order()->get_total()
);
if ( isset( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_COD ] ) ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_COD,
$fs_method[ WPDesk_Paczkomaty_FS::SETTING_COD ]
);
} else {
$this->set_meta_filtered( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_COD, '0' );
}
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_COD_AMOUNT,
$this->shipment->get_order()->get_total()
);
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_CUSTOMER_REF,
// Translators: order number.
sprintf( __( 'Zamówienie %s', 'woocommerce-paczkomaty-inpost' ), $this->shipment->get_order()->get_order_number() )
);
$default_weight = (float) ( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_DEFAULT_WEIGHT ] ?? 0.0 );
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_WEIGHT,
( new WooCommerce_Order_Weight_Calculator( $this->shipment->get_order() ) )->get_order_weight_kg( $default_weight )
);
$this->shipment->set_meta( '_flexible_shipping_status', 'new' );
foreach ( $this->shipment_options as $shipment_option ) {
$shipment_option->handle_checkout( $fs_method, $this->package );
}
}
/**
* Set shipment meta for service paczkomaty.
*
* @param array $fs_method Flexible Shipping method settings.
* @param array $posted_data Posted data.
*/
private function set_meta_for_service_paczkomaty( $fs_method, $posted_data ) {
if ( isset( $posted_data[ WPDesk_Paczkomaty_Checkout::FIELD_PACZKOMAT_ID ] ) ) {
$paczkomat_id = $posted_data[ WPDesk_Paczkomaty_Checkout::FIELD_PACZKOMAT_ID ];
} else {
$paczkomat_id = $this->shipment->get_order()->get_meta( WPDesk_Paczkomaty_Checkout::FIELD_PACZKOMAT_ID );
}
if ( ! empty( $paczkomat_id ) && $paczkomat_id !== '' ) {
$this->shipment->set_meta(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_ID,
sanitize_text_field( $paczkomat_id )
);
try {
$point = $this->pickup_points_manager->get_point_details( $paczkomat_id );
$this->shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::DELIVERY_FIELD_ID, $point->get_point_id() );
$this->shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::DELIVERY_FIELD_NAME, $point->get_name() );
$this->shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::DELIVERY_FIELD_ADDRESS, $point->get_address_1() );
$this->shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::DELIVERY_FIELD_POSTCODE, $point->get_zipcode() );
$this->shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::DELIVERY_FIELD_CITY, $point->get_city() );
} catch ( Exception $e ) {
$this->shipment->get_logger()->error( $e->getMessage() );
}
} else {
throw new Exception( __( 'Proszę wybrać paczkomat', 'woocommerce-paczkomaty-inpost' ) );
}
if ( isset( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_SIZE ] ) && $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_SIZE ] !== '0' ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SIZE,
$fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_SIZE ]
);
} else {
$default_package_size = $this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_DEFAULT_PACKAGE_SIZE, '' );
if ( $default_package_size ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SIZE,
$default_package_size
);
}
}
if ( WC_Paczkomaty_Shipping_Method::API_TYPE_XML === $this->paczkomaty_shipping_method->get_parcel_lockers_api() ) {
$self_send = $this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SELF_SEND, '' );
if ( $self_send ) {
if ( WC_Paczkomaty_Shipping_Method::SENDING_METHOD_PARCEL_LOCKER_XML_API === $self_send ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD,
WC_Paczkomaty_Shipping_Method::SENDING_METHOD_PARCEL_LOCKER
);
}
if ( WC_Paczkomaty_Shipping_Method::SENDING_METHOD_DISPATCH_ORDER_XML_API === $self_send ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD,
WC_Paczkomaty_Shipping_Method::SENDING_METHOD_DISPATCH_ORDER
);
}
}
} else {
$sending_method = $this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SENDING_METHOD, '' );
if ( $sending_method ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD,
$sending_method
);
}
}
}
/**
* Set shipment meta for service polecony.
*/
private function set_meta_for_service_polecony() {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_PUNKT_NADANIA,
$this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_DOMYSLNY_PUNKT_NADANIA, '' )
);
}
/**
* Set shipment meta for service kurier.
*
* @param array $fs_method Flexble Shipping method settings.
*/
private function set_meta_for_service_kurier( array $fs_method ) {
if ( isset( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_WIDTH ] ) ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_WIDTH,
$fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_WIDTH ]
);
}
if ( isset( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_HEIGHT ] ) ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_HEIGHT,
$fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_HEIGHT ]
);
}
if ( isset( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_LENGTH ] ) ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_LENGTH,
$fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_PACKAGE_LENGTH ]
);
}
if ( isset( $fs_method[ WPDesk_Paczkomaty_FS::SETTING_SATURDAY ] ) ) {
$this->set_meta_filtered(
WPDesk_Paczkomaty_Additional_Service_Saturday::META_SATURDAY,
$fs_method[ WPDesk_Paczkomaty_FS::SETTING_SATURDAY ]
);
}
$courier_service = $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_KURIER_USLUGA ] === WPDesk_Paczkomaty_FS::STANDARD
? WPDesk_Flexible_Shipping_Shipment_Paczkomaty::INPOST_COURIER_STANDARD
: WPDesk_Flexible_Shipping_Shipment_Paczkomaty::INPOST_COURIER_C2C;
$this->shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::COURIER_SERVICE, $courier_service );
$sending_method = $this->paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SENDING_METHOD, '' );
if ( $sending_method ) {
$this->set_meta_filtered(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD,
$sending_method
);
}
}
/**
* Process checkout.
*
* @param array $posted_data Posted data ($_POST global variable).
*/
public function process_checkout( array $posted_data ) {
$fs_method = $this->flexible_shipping_method;
$service = $fs_method[ WPDesk_Paczkomaty_FS::SETTING_PACZKOMATY_USLUGA ];
$this->shipment->set_meta(
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_KEY_PACZKOMATY_USLUGA,
$service
);
if ( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::SERVICE_PACZKOMATY === $service ) {
$this->set_meta_for_service_paczkomaty( $fs_method, $posted_data );
} elseif ( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::SERVICE_POLECONY === $service ) {
$this->set_meta_for_service_polecony();
} else {
$this->set_meta_for_service_kurier( $fs_method );
}
$this->set_meta_common( $fs_method );
}
}