date-time-location
1 year ago
class-date-time-location-block-integration.php
1 year ago
class-date-time-location-block.php
1 year ago
class-date-time-location-storage.php
1 year ago
class-date-time-location-storage.php
63 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 | //error_log(print_r($request['body'],true)); |
| 24 | $extensions = $request->get_param( 'extensions' ); |
| 25 | $params = $extensions['pisol-dtt/date-time-location'] ?? array(); |
| 26 | |
| 27 | if(empty($params['pi_delivery_type'])){ |
| 28 | $params['pi_delivery_type'] = 'non-deliverable'; |
| 29 | } |
| 30 | |
| 31 | if(isset($params['pickup_location_id'])){ |
| 32 | $params['pickup_location'] = $params['pickup_location_id']; |
| 33 | } |
| 34 | |
| 35 | if ( empty( $params ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $errors = new \WP_Error(); |
| 40 | |
| 41 | // Perform validation checks and add errors to $errors object |
| 42 | \pi_dtt_validate::validateCheckout($params, $errors); |
| 43 | |
| 44 | // Check if there are any errors in the $errors object |
| 45 | if ($errors->has_errors()) { |
| 46 | // Extract all error messages from WP_Error |
| 47 | $error_messages = $errors->get_error_messages(); |
| 48 | |
| 49 | // Option 1: Throw an exception for each error individually (not typically ideal) |
| 50 | $combined_error_message = implode("<br>", $error_messages); |
| 51 | throw new \WC_Data_Exception('invalid_checkout_data', $combined_error_message); |
| 52 | } |
| 53 | |
| 54 | $order_id = $order->get_id(); |
| 55 | |
| 56 | \pi_dtt_order::storeDetailInOrder($order_id, $params); |
| 57 | |
| 58 | $order->save(); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | DateTimeLocationStorage::get_instance(); |