|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // GP Limit Choices // GSPC compatibility |
| 4 | + * |
| 5 | + * This is snippet will ensure that validation is checked again before an order is created. |
| 6 | + * https://gravitywiz.com/documentation/gravity-forms-limit-choices/ |
| 7 | + */ |
| 8 | + |
| 9 | +add_action( 'woocommerce_checkout_create_order', function( $order ) { |
| 10 | + if ( ! function_exists( 'gp_limit_choices' ) || ! function_exists( 'gs_product_configurator' ) ) { |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + $items = $order->get_items(); |
| 15 | + |
| 16 | + foreach ( $items as $item ) { |
| 17 | + $gspc_order_item = new GS_Product_Configurator\WC_Order_Item( $item ); |
| 18 | + $entries = $gspc_order_item->get_entries(); |
| 19 | + |
| 20 | + foreach ( $entries as $entry ) { |
| 21 | + add_filter( 'gwlc_selected_values', function( $value, $field ) use ( $entry ) { |
| 22 | + return GFFormsModel::get_lead_field_value( $entry, $field ); |
| 23 | + }, 10, 2 ); |
| 24 | + add_filter( 'gplc_requested_count', function( $count, $field ) use ( $entry ) { |
| 25 | + return rgar( $entry, gp_limit_choices()->get_product_quantity_input_id( $field ) ); |
| 26 | + }, 10, 2 ); |
| 27 | + |
| 28 | + $form = GFAPI::get_form( $entry['form_id'] ); |
| 29 | + |
| 30 | + if ( gp_limit_choices()->has_validation_error( $form ) ) { |
| 31 | + $message = field_validation_message( $form ); |
| 32 | + throw new \Exception( $message ); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | +}, 11 ); |
| 38 | + |
| 39 | +public function field_validation_message( $form ) { |
| 40 | + foreach ( $form['fields'] as $field ) { |
| 41 | + if ( rgar( $field, 'validation_message' ) ) { |
| 42 | + return $field['validation_message']; |
| 43 | + } |
| 44 | + } |
| 45 | + return null; |
| 46 | +} |
0 commit comments