|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Shop // GS Product Configurator // Add order metadata to GF entries on export. |
| 4 | + * |
| 5 | + * Instructions: |
| 6 | + * 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
| 7 | + * 2. Update the returned value in `gspc_add_order_meta_to_gf_entry_metas()` accordingly. |
| 8 | + */ |
| 9 | + |
| 10 | +if ( is_callable( 'gs_product_configurator' ) ) : |
| 11 | + |
| 12 | + function gspc_add_order_meta_to_gf_entry_metas() { |
| 13 | + /** |
| 14 | + * Update this with the WC metadata keys you want to add. You |
| 15 | + * may optionally include a label to use during export like so: |
| 16 | + * '_billing_phone' => 'Phone', |
| 17 | + * If a label is not included, labels are generated thus: |
| 18 | + * e.g. 'Billing Phone' is used for '_billing_phone'. |
| 19 | + * |
| 20 | + * Please see class WC_Order_Data_Store_CPT in the WC plugin. |
| 21 | + * You may also check your DB records or the extension files |
| 22 | + * to see other order metadata keys that your installed WC |
| 23 | + * extensions might be storing. |
| 24 | + */ |
| 25 | + return array( |
| 26 | + '_order_key', |
| 27 | + '_billing_first_name', |
| 28 | + '_billing_last_name', |
| 29 | + '_billing_email' => 'Email', |
| 30 | + '_billing_phone' => 'Phone', |
| 31 | + '_shipping_first_name', |
| 32 | + '_shipping_last_name', |
| 33 | + '_cart_discount', |
| 34 | + '_created_via', |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + function gspc_add_order_meta_to_gf_entry_metas_keys() { |
| 39 | + $meta_keys = array(); |
| 40 | + |
| 41 | + foreach ( gspc_add_order_meta_to_gf_entry_metas() as $key => $value ) { |
| 42 | + if ( is_int( $key ) ) { |
| 43 | + $meta_keys[] = $value; |
| 44 | + } else { |
| 45 | + $meta_keys[] = $key; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return $meta_keys; |
| 50 | + } |
| 51 | + |
| 52 | + add_filter( 'gform_export_fields', function( $form ) { |
| 53 | + $metas = gspc_add_order_meta_to_gf_entry_metas(); |
| 54 | + |
| 55 | + foreach ( $metas as $key => $value ) { |
| 56 | + if ( is_int( $key ) ) { |
| 57 | + $new_key = $value; |
| 58 | + $new_value = trim( $value, " \n\r\t\v\x00_" ); |
| 59 | + $new_value = preg_replace( '/[_]+/', ' ', $new_value ); |
| 60 | + $new_value = ucwords( $new_value ); |
| 61 | + } else { |
| 62 | + $new_key = $key; |
| 63 | + $new_value = $value; |
| 64 | + } |
| 65 | + |
| 66 | + array_push( |
| 67 | + $form['fields'], |
| 68 | + array( |
| 69 | + 'id' => $new_key, |
| 70 | + 'label' => $new_value, |
| 71 | + ) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return $form; |
| 76 | + }, 20 ); // we use 20 so that it comes after the one in GSPC core. |
| 77 | + |
| 78 | + add_filter( 'gform_export_field_value', function( $value, $form_id, $field_id, $entry ) { |
| 79 | + $meta_keys = gspc_add_order_meta_to_gf_entry_metas_keys(); |
| 80 | + |
| 81 | + if ( ! in_array( $field_id, $meta_keys, true ) ) { |
| 82 | + return $value; |
| 83 | + } |
| 84 | + |
| 85 | + // custom field keys could clash, so confirm if this |
| 86 | + // entry is from a form embedded in a product page. |
| 87 | + $order_ids = gform_get_meta( $entry['id'], GS_Product_Configurator::ENTRY_WC_ORDER_IDS ); |
| 88 | + $order_ids = maybe_unserialize( $order_ids ); |
| 89 | + |
| 90 | + if ( ! is_array( $order_ids ) || ! count( $order_ids ) ) { |
| 91 | + return $value; |
| 92 | + } |
| 93 | + |
| 94 | + $orders = array_map( function( $id ) { |
| 95 | + $order = wc_get_order( $id ); |
| 96 | + |
| 97 | + return $order ? $order : null; |
| 98 | + }, $order_ids ); |
| 99 | + |
| 100 | + // There are situations in which an entry can be associated with multiple |
| 101 | + // order IDs (e.g. WooCommerce Subscriptions), so get the main order. |
| 102 | + $orders = array_filter( $orders, function( $order ) { |
| 103 | + return $order && ! $order->get_parent_id(); |
| 104 | + } ); |
| 105 | + $order = $orders[0]; |
| 106 | + |
| 107 | + $val = $order->get_meta( $field_id ); |
| 108 | + $val = $val ? $val : ''; |
| 109 | + |
| 110 | + if ( is_object( $val ) ) { |
| 111 | + $val = (array) $val; |
| 112 | + } |
| 113 | + |
| 114 | + if ( is_array( $val ) ) { |
| 115 | + array_walk( $val, function( &$v, $k ) { |
| 116 | + $v = "{$k} ({$v})"; |
| 117 | + }); |
| 118 | + $val = implode( ', ', $val ); |
| 119 | + } |
| 120 | + |
| 121 | + $value = str_replace( array( "\n", "\t", "\r" ), ' ', $val ); |
| 122 | + $value = htmlspecialchars_decode( $value ); |
| 123 | + |
| 124 | + return $value; |
| 125 | + }, 10, 4 ); |
| 126 | + |
| 127 | +endif; |
0 commit comments