date-time-location
10 months ago
class-date-time-location-block-integration.php
10 months ago
class-date-time-location-block.php
10 months ago
class-date-time-location-storage.php
10 months ago
class-date-time-location-storage.php
73 lines
| 1 | <?php |
| 2 | namespace PISOL\DTT\BLOCK; |
| 3 | |
| 4 | class DateTimeLocationStorage{ |
| 5 | |
| 6 | protected static $instance = null; |
| 7 | |
| 8 | public static function get_instance( ) { |
| 9 | if ( is_null( self::$instance ) ) { |
| 10 | self::$instance = new self(); |
| 11 | } |
| 12 | return self::$instance; |
| 13 | } |
| 14 | |
| 15 | function __construct() |
| 16 | { |
| 17 | add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $this, 'update_block_order_meta' ), 10, 2 ); |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
| 22 | function update_block_order_meta($order, $request){ |
| 23 | |
| 24 | /** |
| 25 | * In latest woocommerce 6.8 update this function is getting called even when payment method is changed, but in that case other data is not being send so we need to check if that is the case or not |
| 26 | */ |
| 27 | $json_params = $request->get_json_params(); |
| 28 | |
| 29 | if(count($json_params) == 1 && isset($json_params['payment_method'])){ |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | //error_log(print_r($request['body'],true)); |
| 34 | $extensions = $request->get_param( 'extensions' ); |
| 35 | $params = $extensions['pisol-dtt/date-time-location'] ?? array(); |
| 36 | |
| 37 | if(empty($params['pi_delivery_type'])){ |
| 38 | $params['pi_delivery_type'] = 'non-deliverable'; |
| 39 | } |
| 40 | |
| 41 | if(isset($params['pickup_location_id'])){ |
| 42 | $params['pickup_location'] = $params['pickup_location_id']; |
| 43 | } |
| 44 | |
| 45 | if ( empty( $params ) ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | $errors = new \WP_Error(); |
| 50 | |
| 51 | // Perform validation checks and add errors to $errors object |
| 52 | \pi_dtt_validate::validateCheckout($params, $errors); |
| 53 | |
| 54 | // Check if there are any errors in the $errors object |
| 55 | if ($errors->has_errors()) { |
| 56 | // Extract all error messages from WP_Error |
| 57 | $error_messages = $errors->get_error_messages(); |
| 58 | |
| 59 | // Option 1: Throw an exception for each error individually (not typically ideal) |
| 60 | $combined_error_message = implode("<br>", $error_messages); |
| 61 | throw new \WC_Data_Exception('invalid_checkout_data', $combined_error_message); |
| 62 | } |
| 63 | |
| 64 | $order_id = $order->get_id(); |
| 65 | |
| 66 | \pi_dtt_order::storeDetailInOrder($order_id, $params); |
| 67 | |
| 68 | $order->save(); |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | DateTimeLocationStorage::get_instance(); |