From 81df57d215dbfcb06daba620b288d01e1ed56357 Mon Sep 17 00:00:00 2001 From: Bartholomew Date: Fri, 27 Sep 2024 08:23:09 +0100 Subject: [PATCH] `gplc-gspc-compatibility.php`: Added new snippet to check validation before an order is created. --- gp-limit-choices/gplc-gspc-compatibility.php | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 gp-limit-choices/gplc-gspc-compatibility.php diff --git a/gp-limit-choices/gplc-gspc-compatibility.php b/gp-limit-choices/gplc-gspc-compatibility.php new file mode 100644 index 00000000..977c2fd4 --- /dev/null +++ b/gp-limit-choices/gplc-gspc-compatibility.php @@ -0,0 +1,46 @@ +get_items(); + + foreach ( $items as $item ) { + $gspc_order_item = new GS_Product_Configurator\WC_Order_Item( $item ); + $entries = $gspc_order_item->get_entries(); + + foreach ( $entries as $entry ) { + add_filter( 'gwlc_selected_values', function( $value, $field ) use ( $entry ) { + return GFFormsModel::get_lead_field_value( $entry, $field ); + }, 10, 2 ); + add_filter( 'gplc_requested_count', function( $count, $field ) use ( $entry ) { + return rgar( $entry, gp_limit_choices()->get_product_quantity_input_id( $field ) ); + }, 10, 2 ); + + $form = GFAPI::get_form( $entry['form_id'] ); + + if ( gp_limit_choices()->has_validation_error( $form ) ) { + $message = field_validation_message( $form ); + throw new \Exception( $message ); + } + } + } + +}, 11 ); + +public function field_validation_message( $form ) { + foreach ( $form['fields'] as $field ) { + if ( rgar( $field, 'validation_message' ) ) { + return $field['validation_message']; + } + } + return null; +}