Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Domain/Services
File: CheckoutFields.php
* Returns an array of fields for a given location.
[1000] Fix | Delete
*
[1001] Fix | Delete
* @param string $location The location to get fields for (address|contact|order).
[1002] Fix | Delete
* @return array An array of fields definitions.
[1003] Fix | Delete
*/
[1004] Fix | Delete
public function get_fields_for_location( $location ) {
[1005] Fix | Delete
$location = $this->prepare_location_name( $location );
[1006] Fix | Delete
[1007] Fix | Delete
if ( in_array( $location, array_keys( $this->fields_locations ), true ) ) {
[1008] Fix | Delete
$order_fields_keys = $this->fields_locations[ $location ];
[1009] Fix | Delete
[1010] Fix | Delete
return array_filter(
[1011] Fix | Delete
$this->get_additional_fields(),
[1012] Fix | Delete
function ( $key ) use ( $order_fields_keys ) {
[1013] Fix | Delete
return in_array( $key, $order_fields_keys, true );
[1014] Fix | Delete
},
[1015] Fix | Delete
ARRAY_FILTER_USE_KEY
[1016] Fix | Delete
);
[1017] Fix | Delete
}
[1018] Fix | Delete
return [];
[1019] Fix | Delete
}
[1020] Fix | Delete
[1021] Fix | Delete
/**
[1022] Fix | Delete
* Returns an array of fields for a given location and uses context to evaluate hidden and required fields.
[1023] Fix | Delete
*
[1024] Fix | Delete
* @param string $location The location to get fields for (address|contact|order).
[1025] Fix | Delete
* @param DocumentObject|null $document_object The document object.
[1026] Fix | Delete
* @return array An array of fields definitions.
[1027] Fix | Delete
*/
[1028] Fix | Delete
public function get_contextual_fields_for_location( $location, $document_object = null ) {
[1029] Fix | Delete
$location_fields = $this->get_fields_for_location( $location );
[1030] Fix | Delete
$fields = [];
[1031] Fix | Delete
foreach ( $location_fields as $key => $field ) {
[1032] Fix | Delete
if ( $this->is_hidden_field( $key, $document_object ) ) {
[1033] Fix | Delete
continue;
[1034] Fix | Delete
}
[1035] Fix | Delete
$field['required'] = $this->is_required_field( $field, $document_object );
[1036] Fix | Delete
$field['validate_callback'] = $this->get_validate_callback( $field, $document_object );
[1037] Fix | Delete
$fields[ $key ] = $field;
[1038] Fix | Delete
}
[1039] Fix | Delete
[1040] Fix | Delete
return $fields;
[1041] Fix | Delete
}
[1042] Fix | Delete
[1043] Fix | Delete
/**
[1044] Fix | Delete
* Validates a set of fields for a given location against custom validation rules.
[1045] Fix | Delete
*
[1046] Fix | Delete
* @param array $fields Array of key value pairs of field values to validate.
[1047] Fix | Delete
* @param string $location The location being validated (address|contact|order).
[1048] Fix | Delete
* @param string $group The group to get the field value for (shipping|billing|other).
[1049] Fix | Delete
* @return WP_Error
[1050] Fix | Delete
*/
[1051] Fix | Delete
public function validate_fields_for_location( $fields, $location, $group = 'other' ) {
[1052] Fix | Delete
$errors = new WP_Error();
[1053] Fix | Delete
$location = $this->prepare_location_name( $location );
[1054] Fix | Delete
$group = $this->prepare_group_name( $group );
[1055] Fix | Delete
[1056] Fix | Delete
try {
[1057] Fix | Delete
wc_do_deprecated_action( '__experimental_woocommerce_blocks_validate_location_' . $location . '_fields', array( $errors, $fields, $group ), '8.9.0', 'woocommerce_blocks_validate_location_' . $location . '_fields', 'This action has been graduated, use woocommerce_blocks_validate_location_' . $location . '_fields instead.' );
[1058] Fix | Delete
[1059] Fix | Delete
/**
[1060] Fix | Delete
* Pass an error object to allow validation of an additional field.
[1061] Fix | Delete
*
[1062] Fix | Delete
* @param WP_Error $errors A WP_Error object that extensions may add errors to.
[1063] Fix | Delete
* @param mixed $fields List of fields (key value pairs) in this location.
[1064] Fix | Delete
* @param string $group The group of this location (shipping|billing|other).
[1065] Fix | Delete
*
[1066] Fix | Delete
* @since 8.7.0
[1067] Fix | Delete
*/
[1068] Fix | Delete
do_action( 'woocommerce_blocks_validate_location_' . $location . '_fields', $errors, $fields, $group );
[1069] Fix | Delete
[1070] Fix | Delete
} catch ( \Throwable $e ) {
[1071] Fix | Delete
[1072] Fix | Delete
// One of the filters errored so skip them. This allows the checkout process to continue.
[1073] Fix | Delete
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
[1074] Fix | Delete
trigger_error(
[1075] Fix | Delete
sprintf(
[1076] Fix | Delete
'The action %s encountered an error. The field location %s may not have any custom validation applied to it. %s',
[1077] Fix | Delete
esc_html( 'woocommerce_blocks_validate_' . $location . '_fields' ),
[1078] Fix | Delete
esc_html( $location ),
[1079] Fix | Delete
esc_html( $e->getMessage() )
[1080] Fix | Delete
),
[1081] Fix | Delete
E_USER_WARNING
[1082] Fix | Delete
);
[1083] Fix | Delete
}
[1084] Fix | Delete
[1085] Fix | Delete
return $errors;
[1086] Fix | Delete
}
[1087] Fix | Delete
[1088] Fix | Delete
/**
[1089] Fix | Delete
* Validates a field to check it belongs to the given location and is valid according to its registration.
[1090] Fix | Delete
*
[1091] Fix | Delete
* This does not apply any custom validation rules on the value.
[1092] Fix | Delete
*
[1093] Fix | Delete
* @param string $key The field key.
[1094] Fix | Delete
* @param mixed $value The field value.
[1095] Fix | Delete
* @param string $location The location to validate the field for (address|contact|order).
[1096] Fix | Delete
*
[1097] Fix | Delete
* @return true|WP_Error True if the field is valid, a WP_Error otherwise.
[1098] Fix | Delete
*/
[1099] Fix | Delete
public function validate_field_for_location( $key, $value, $location ) {
[1100] Fix | Delete
$location = $this->prepare_location_name( $location );
[1101] Fix | Delete
[1102] Fix | Delete
if ( ! $this->is_field( $key ) ) {
[1103] Fix | Delete
return new WP_Error(
[1104] Fix | Delete
'woocommerce_invalid_checkout_field',
[1105] Fix | Delete
\sprintf(
[1106] Fix | Delete
// translators: % is field key.
[1107] Fix | Delete
__( 'The field %s is invalid.', 'woocommerce' ),
[1108] Fix | Delete
$key
[1109] Fix | Delete
)
[1110] Fix | Delete
);
[1111] Fix | Delete
}
[1112] Fix | Delete
[1113] Fix | Delete
if ( ! in_array( $key, $this->fields_locations[ $location ], true ) ) {
[1114] Fix | Delete
return new WP_Error(
[1115] Fix | Delete
'woocommerce_invalid_checkout_field_location',
[1116] Fix | Delete
\sprintf(
[1117] Fix | Delete
// translators: %1$s is field key, %2$s location.
[1118] Fix | Delete
__( 'The field %1$s is invalid for the location %2$s.', 'woocommerce' ),
[1119] Fix | Delete
$key,
[1120] Fix | Delete
$location
[1121] Fix | Delete
)
[1122] Fix | Delete
);
[1123] Fix | Delete
}
[1124] Fix | Delete
[1125] Fix | Delete
return true;
[1126] Fix | Delete
}
[1127] Fix | Delete
[1128] Fix | Delete
/**
[1129] Fix | Delete
* Returns all fields key for a given group.
[1130] Fix | Delete
*
[1131] Fix | Delete
* @param string $group The group to get the key for (shipping|billing|other).
[1132] Fix | Delete
*
[1133] Fix | Delete
* @return string[] Field keys.
[1134] Fix | Delete
*/
[1135] Fix | Delete
public function get_fields_for_group( $group = 'other' ) {
[1136] Fix | Delete
$group = $this->prepare_group_name( $group );
[1137] Fix | Delete
if ( 'shipping' === $group || 'billing' === $group ) {
[1138] Fix | Delete
return $this->get_fields_for_location( 'address' );
[1139] Fix | Delete
}
[1140] Fix | Delete
return \array_merge(
[1141] Fix | Delete
$this->get_fields_for_location( 'contact' ),
[1142] Fix | Delete
$this->get_fields_for_location( 'order' )
[1143] Fix | Delete
);
[1144] Fix | Delete
}
[1145] Fix | Delete
[1146] Fix | Delete
/**
[1147] Fix | Delete
* Returns true if the given key is a valid field.
[1148] Fix | Delete
*
[1149] Fix | Delete
* @param string $key The field key.
[1150] Fix | Delete
*
[1151] Fix | Delete
* @return bool True if the field is valid, false otherwise.
[1152] Fix | Delete
*/
[1153] Fix | Delete
public function is_field( $key ) {
[1154] Fix | Delete
return array_key_exists( $key, $this->additional_fields );
[1155] Fix | Delete
}
[1156] Fix | Delete
[1157] Fix | Delete
/**
[1158] Fix | Delete
* Returns true if the given key is a valid customer field.
[1159] Fix | Delete
*
[1160] Fix | Delete
* Customer fields are fields saved to the customer data, like address and contact fields.
[1161] Fix | Delete
*
[1162] Fix | Delete
* @param string $key The field key.
[1163] Fix | Delete
*
[1164] Fix | Delete
* @return bool True if the field is valid, false otherwise.
[1165] Fix | Delete
*/
[1166] Fix | Delete
public function is_customer_field( $key ) {
[1167] Fix | Delete
return in_array( $key, array_intersect( array_merge( $this->get_address_fields_keys(), $this->get_contact_fields_keys() ), array_keys( $this->additional_fields ) ), true );
[1168] Fix | Delete
}
[1169] Fix | Delete
[1170] Fix | Delete
/**
[1171] Fix | Delete
* Persists a field value for a given order. This would also optionally set the field value on the customer object if the order is linked to a registered customer.
[1172] Fix | Delete
*
[1173] Fix | Delete
* @param string $key The field key.
[1174] Fix | Delete
* @param mixed $value The field value.
[1175] Fix | Delete
* @param WC_Order $order The order to persist the field for.
[1176] Fix | Delete
* @param string $group The group to persist the field for (shipping|billing|other).
[1177] Fix | Delete
* @param bool $set_customer Whether to set the field value on the customer or not.
[1178] Fix | Delete
*
[1179] Fix | Delete
* @return void
[1180] Fix | Delete
*/
[1181] Fix | Delete
public function persist_field_for_order( string $key, $value, WC_Order $order, string $group = 'other', bool $set_customer = true ) {
[1182] Fix | Delete
$group = $this->prepare_group_name( $group );
[1183] Fix | Delete
$this->set_array_meta( $key, $value, $order, $group );
[1184] Fix | Delete
if ( $set_customer && $order->get_customer_id() ) {
[1185] Fix | Delete
$customer = new WC_Customer( $order->get_customer_id() );
[1186] Fix | Delete
$this->persist_field_for_customer( $key, $value, $customer, $group );
[1187] Fix | Delete
}
[1188] Fix | Delete
}
[1189] Fix | Delete
[1190] Fix | Delete
/**
[1191] Fix | Delete
* Persists a field value for a given customer.
[1192] Fix | Delete
*
[1193] Fix | Delete
* @param string $key The field key.
[1194] Fix | Delete
* @param mixed $value The field value.
[1195] Fix | Delete
* @param WC_Customer $customer The customer to persist the field for.
[1196] Fix | Delete
* @param string $group The group to persist the field for (shipping|billing|other).
[1197] Fix | Delete
*
[1198] Fix | Delete
* @return void
[1199] Fix | Delete
*/
[1200] Fix | Delete
public function persist_field_for_customer( string $key, $value, WC_Customer $customer, string $group = 'other' ) {
[1201] Fix | Delete
$group = $this->prepare_group_name( $group );
[1202] Fix | Delete
$this->set_array_meta( $key, $value, $customer, $group );
[1203] Fix | Delete
}
[1204] Fix | Delete
[1205] Fix | Delete
/**
[1206] Fix | Delete
* Sets a field value in an array meta, supporting routing things to billing, shipping, or additional fields, based on a prefix for the key.
[1207] Fix | Delete
*
[1208] Fix | Delete
* @param string $key The field key.
[1209] Fix | Delete
* @param mixed $value The field value.
[1210] Fix | Delete
* @param WC_Customer|WC_Order $wc_object The object to set the field value for.
[1211] Fix | Delete
* @param string $group The group to set the field value for (shipping|billing|other).
[1212] Fix | Delete
*
[1213] Fix | Delete
* @return void
[1214] Fix | Delete
*/
[1215] Fix | Delete
private function set_array_meta( string $key, $value, WC_Data $wc_object, string $group ) {
[1216] Fix | Delete
$meta_key = self::get_group_key( $group ) . $key;
[1217] Fix | Delete
[1218] Fix | Delete
/**
[1219] Fix | Delete
* Allow reacting for saving an additional field value.
[1220] Fix | Delete
*
[1221] Fix | Delete
* @param string $key The key of the field being saved.
[1222] Fix | Delete
* @param mixed $value The value of the field being saved.
[1223] Fix | Delete
* @param string $group The group of this location (shipping|billing|other).
[1224] Fix | Delete
* @param WC_Customer|WC_Order $wc_object The object to set the field value for.
[1225] Fix | Delete
*
[1226] Fix | Delete
* @since 8.9.0
[1227] Fix | Delete
*/
[1228] Fix | Delete
do_action( 'woocommerce_set_additional_field_value', $key, $value, $group, $wc_object );
[1229] Fix | Delete
// Convert boolean values to strings because Data Stores will skip false values.
[1230] Fix | Delete
if ( is_bool( $value ) ) {
[1231] Fix | Delete
$value = $value ? '1' : '0';
[1232] Fix | Delete
}
[1233] Fix | Delete
$wc_object->update_meta_data( $meta_key, $value );
[1234] Fix | Delete
}
[1235] Fix | Delete
[1236] Fix | Delete
/**
[1237] Fix | Delete
* Returns a field value for a given object.
[1238] Fix | Delete
*
[1239] Fix | Delete
* @param string $key The field key.
[1240] Fix | Delete
* @param WC_Customer|WC_Order $wc_object The customer or order to get the field value for.
[1241] Fix | Delete
* @param string $group The group to get the field value for (shipping|billing|other).
[1242] Fix | Delete
*
[1243] Fix | Delete
* @return mixed The field value.
[1244] Fix | Delete
*/
[1245] Fix | Delete
public function get_field_from_object( string $key, WC_Data $wc_object, string $group = 'other' ) {
[1246] Fix | Delete
$group = $this->prepare_group_name( $group );
[1247] Fix | Delete
$meta_key = self::get_group_key( $group ) . $key;
[1248] Fix | Delete
$value = $wc_object->get_meta( $meta_key, true );
[1249] Fix | Delete
[1250] Fix | Delete
if ( ! $value && '0' !== $value ) {
[1251] Fix | Delete
/**
[1252] Fix | Delete
* Allow providing a default value for additional fields if no value is already set.
[1253] Fix | Delete
*
[1254] Fix | Delete
* @param null $value The default value for the filter, always null.
[1255] Fix | Delete
* @param string $group The group of this key (shipping|billing|other).
[1256] Fix | Delete
* @param WC_Data $wc_object The object to get the field value for.
[1257] Fix | Delete
*
[1258] Fix | Delete
* @since 8.9.0
[1259] Fix | Delete
*/
[1260] Fix | Delete
$value = apply_filters( "woocommerce_get_default_value_for_{$key}", null, $group, $wc_object );
[1261] Fix | Delete
}
[1262] Fix | Delete
[1263] Fix | Delete
// We cast the value to a boolean if the field is a checkbox.
[1264] Fix | Delete
if ( $this->is_field( $key ) && 'checkbox' === $this->additional_fields[ $key ]['type'] ) {
[1265] Fix | Delete
return '1' === $value;
[1266] Fix | Delete
}
[1267] Fix | Delete
[1268] Fix | Delete
if ( null === $value ) {
[1269] Fix | Delete
return '';
[1270] Fix | Delete
}
[1271] Fix | Delete
[1272] Fix | Delete
return $value;
[1273] Fix | Delete
}
[1274] Fix | Delete
[1275] Fix | Delete
/**
[1276] Fix | Delete
* Returns an array of all fields values for a given object in a group.
[1277] Fix | Delete
*
[1278] Fix | Delete
* @param WC_Data $wc_object The object or order to get the fields for.
[1279] Fix | Delete
* @param string $group The group to get the fields for (shipping|billing|other).
[1280] Fix | Delete
* @param bool $all Whether to return all fields or only the ones that are still registered. Default false.
[1281] Fix | Delete
* @return array An array of fields.
[1282] Fix | Delete
*/
[1283] Fix | Delete
public function get_all_fields_from_object( WC_Data $wc_object, string $group = 'other', bool $all = false ) {
[1284] Fix | Delete
$meta_data = [];
[1285] Fix | Delete
$group = $this->prepare_group_name( $group );
[1286] Fix | Delete
$prefix = self::get_group_key( $group );
[1287] Fix | Delete
[1288] Fix | Delete
if ( $wc_object instanceof WC_Data ) {
[1289] Fix | Delete
$meta = $wc_object->get_meta_data();
[1290] Fix | Delete
foreach ( $meta as $meta_data_object ) {
[1291] Fix | Delete
if ( 0 === \strpos( $meta_data_object->key, $prefix ) ) {
[1292] Fix | Delete
$key = \str_replace( $prefix, '', $meta_data_object->key );
[1293] Fix | Delete
if ( $all || $this->is_field( $key ) ) {
[1294] Fix | Delete
$meta_data[ $key ] = $meta_data_object->value;
[1295] Fix | Delete
}
[1296] Fix | Delete
}
[1297] Fix | Delete
}
[1298] Fix | Delete
}
[1299] Fix | Delete
[1300] Fix | Delete
$missing_fields = array_diff( array_keys( $this->get_fields_for_group( $group ) ), array_keys( $meta_data ) );
[1301] Fix | Delete
[1302] Fix | Delete
foreach ( $missing_fields as $missing_field ) {
[1303] Fix | Delete
/**
[1304] Fix | Delete
* Allow providing a default value for additional fields if no value is already set.
[1305] Fix | Delete
*
[1306] Fix | Delete
* @param null $value The default value for the filter, always null.
[1307] Fix | Delete
* @param string $group The group of this key (shipping|billing|other).
[1308] Fix | Delete
* @param WC_Data $wc_object The object to get the field value for.
[1309] Fix | Delete
*
[1310] Fix | Delete
* @since 8.9.0
[1311] Fix | Delete
*/
[1312] Fix | Delete
$value = apply_filters( "woocommerce_get_default_value_for_{$missing_field}", null, $group, $wc_object );
[1313] Fix | Delete
[1314] Fix | Delete
if ( isset( $value ) ) {
[1315] Fix | Delete
$meta_data[ $missing_field ] = $value;
[1316] Fix | Delete
}
[1317] Fix | Delete
}
[1318] Fix | Delete
[1319] Fix | Delete
return $meta_data;
[1320] Fix | Delete
}
[1321] Fix | Delete
[1322] Fix | Delete
/**
[1323] Fix | Delete
* Copies additional fields from an order to a customer.
[1324] Fix | Delete
*
[1325] Fix | Delete
* @param WC_Order $order The order to sync the fields for.
[1326] Fix | Delete
* @param WC_Customer $customer The customer to sync the fields for.
[1327] Fix | Delete
*/
[1328] Fix | Delete
public function sync_customer_additional_fields_with_order( WC_Order $order, WC_Customer $customer ) {
[1329] Fix | Delete
foreach ( $this->groups as $group ) {
[1330] Fix | Delete
$order_additional_fields = $this->get_all_fields_from_object( $order, $group, true );
[1331] Fix | Delete
[1332] Fix | Delete
// Sync customer additional fields with order additional fields.
[1333] Fix | Delete
foreach ( $order_additional_fields as $key => $value ) {
[1334] Fix | Delete
if ( $this->is_customer_field( $key ) ) {
[1335] Fix | Delete
$this->persist_field_for_customer( $key, $value, $customer, $group );
[1336] Fix | Delete
}
[1337] Fix | Delete
}
[1338] Fix | Delete
}
[1339] Fix | Delete
}
[1340] Fix | Delete
[1341] Fix | Delete
/**
[1342] Fix | Delete
* Copies additional fields from a customer to an order.
[1343] Fix | Delete
*
[1344] Fix | Delete
* @param WC_Order $order The order to sync the fields for.
[1345] Fix | Delete
* @param WC_Customer $customer The customer to sync the fields for.
[1346] Fix | Delete
*/
[1347] Fix | Delete
public function sync_order_additional_fields_with_customer( WC_Order $order, WC_Customer $customer ) {
[1348] Fix | Delete
foreach ( $this->groups as $group ) {
[1349] Fix | Delete
$customer_additional_fields = $this->get_all_fields_from_object( $customer, $group, true );
[1350] Fix | Delete
[1351] Fix | Delete
// Sync order additional fields with customer additional fields.
[1352] Fix | Delete
foreach ( $customer_additional_fields as $key => $value ) {
[1353] Fix | Delete
if ( $this->is_field( $key ) ) {
[1354] Fix | Delete
$this->persist_field_for_order( $key, $value, $order, $group, false );
[1355] Fix | Delete
}
[1356] Fix | Delete
}
[1357] Fix | Delete
}
[1358] Fix | Delete
}
[1359] Fix | Delete
[1360] Fix | Delete
/**
[1361] Fix | Delete
* From a set of fields, returns only the ones for a given location.
[1362] Fix | Delete
*
[1363] Fix | Delete
* @param array $fields The fields to filter.
[1364] Fix | Delete
* @param string $location The location to validate the field for (address|contact|order).
[1365] Fix | Delete
* @return array The filtered fields.
[1366] Fix | Delete
*/
[1367] Fix | Delete
public function filter_fields_for_location( array $fields, string $location ) {
[1368] Fix | Delete
$location = $this->prepare_location_name( $location );
[1369] Fix | Delete
[1370] Fix | Delete
return array_filter(
[1371] Fix | Delete
$fields,
[1372] Fix | Delete
function ( $key ) use ( $location ) {
[1373] Fix | Delete
return $this->get_field_location( $key ) === $location;
[1374] Fix | Delete
},
[1375] Fix | Delete
ARRAY_FILTER_USE_KEY
[1376] Fix | Delete
);
[1377] Fix | Delete
}
[1378] Fix | Delete
[1379] Fix | Delete
/**
[1380] Fix | Delete
* Filter fields for order confirmation.
[1381] Fix | Delete
*
[1382] Fix | Delete
* @param array $fields The fields to filter.
[1383] Fix | Delete
* @param array $context Additional context for the filter.
[1384] Fix | Delete
* @return array The filtered fields.
[1385] Fix | Delete
*/
[1386] Fix | Delete
public function filter_fields_for_order_confirmation( $fields, $context = array() ) {
[1387] Fix | Delete
return array_filter(
[1388] Fix | Delete
$fields,
[1389] Fix | Delete
function ( $field ) use ( $fields, $context ) {
[1390] Fix | Delete
/**
[1391] Fix | Delete
* Filter fields for order confirmation (thank you page, email).
[1392] Fix | Delete
*
[1393] Fix | Delete
* Used in methods:
[1394] Fix | Delete
* WC_Email::additional_checkout_fields
[1395] Fix | Delete
* WC_Email::additional_address_fields
[1396] Fix | Delete
* CheckoutFieldsFrontend::render_order_other_fields
[1397] Fix | Delete
* AdditionalFields::render_content
[1398] Fix | Delete
*
[1399] Fix | Delete
* @param bool Whether the field should be shown.
[1400] Fix | Delete
* @param array $field Field data.
[1401] Fix | Delete
* @param array $fields All fields for better context when field should be shown or hidden based on other fields values.
[1402] Fix | Delete
* @param array $context Additional context for the filter. Data depends in which method filter_fields_for_order_confirmation is called.
[1403] Fix | Delete
* @param CheckoutFields $this The CheckoutFields instance.
[1404] Fix | Delete
* @since 10.1.0
[1405] Fix | Delete
*/
[1406] Fix | Delete
return apply_filters( 'woocommerce_filter_fields_for_order_confirmation', ! empty( $field['show_in_order_confirmation'] ), $field, $fields, $context, $this );
[1407] Fix | Delete
}
[1408] Fix | Delete
);
[1409] Fix | Delete
}
[1410] Fix | Delete
[1411] Fix | Delete
/**
[1412] Fix | Delete
* Get additional fields for an order.
[1413] Fix | Delete
*
[1414] Fix | Delete
* @param WC_Order $order Order object.
[1415] Fix | Delete
* @param string $location The location to get fields for (address|contact|order).
[1416] Fix | Delete
* @param string $group The group to get the field value for (shipping|billing|other).
[1417] Fix | Delete
* @param string $context The context to get the field value for (edit|view).
[1418] Fix | Delete
* @return array An array of fields definitions as well as their values formatted for display.
[1419] Fix | Delete
*/
[1420] Fix | Delete
public function get_order_additional_fields_with_values( WC_Order $order, string $location, string $group = 'other', string $context = 'edit' ) {
[1421] Fix | Delete
[1422] Fix | Delete
// Because the Additional Checkout Fields API only applies to orders created with Store API, we should not
[1423] Fix | Delete
// return any values unless it was created using Store API. This is mainly to prevent "empty" checkbox values
[1424] Fix | Delete
// from being shown on the order confirmation page for orders placed using the shortcode. It's rare that this
[1425] Fix | Delete
// will happen but not impossible.
[1426] Fix | Delete
if ( 'store-api' !== $order->get_created_via() ) {
[1427] Fix | Delete
return [];
[1428] Fix | Delete
}
[1429] Fix | Delete
[1430] Fix | Delete
$location = $this->prepare_location_name( $location );
[1431] Fix | Delete
$group = $this->prepare_group_name( $group );
[1432] Fix | Delete
$fields = $this->get_fields_for_location( $location );
[1433] Fix | Delete
$fields_with_values = [];
[1434] Fix | Delete
[1435] Fix | Delete
foreach ( $fields as $field_key => $field ) {
[1436] Fix | Delete
$value = $this->get_field_from_object( $field_key, $order, $group );
[1437] Fix | Delete
[1438] Fix | Delete
if ( '' === $value || null === $value ) {
[1439] Fix | Delete
continue;
[1440] Fix | Delete
}
[1441] Fix | Delete
[1442] Fix | Delete
if ( 'view' === $context ) {
[1443] Fix | Delete
$value = $this->format_additional_field_value( $value, $field );
[1444] Fix | Delete
}
[1445] Fix | Delete
[1446] Fix | Delete
$field['value'] = $value;
[1447] Fix | Delete
$fields_with_values[ $field_key ] = $field;
[1448] Fix | Delete
}
[1449] Fix | Delete
[1450] Fix | Delete
return $fields_with_values;
[1451] Fix | Delete
}
[1452] Fix | Delete
[1453] Fix | Delete
/**
[1454] Fix | Delete
* Formats a raw field value for display based on its type definition.
[1455] Fix | Delete
*
[1456] Fix | Delete
* @param string $value Value to format.
[1457] Fix | Delete
* @param array $field Additional field definition.
[1458] Fix | Delete
* @return string
[1459] Fix | Delete
*/
[1460] Fix | Delete
public function format_additional_field_value( $value, $field ) {
[1461] Fix | Delete
if ( 'checkbox' === $field['type'] ) {
[1462] Fix | Delete
$value = $value ? __( 'Yes', 'woocommerce' ) : __( 'No', 'woocommerce' );
[1463] Fix | Delete
}
[1464] Fix | Delete
[1465] Fix | Delete
if ( 'select' === $field['type'] ) {
[1466] Fix | Delete
$options = array_column( $field['options'], 'label', 'value' );
[1467] Fix | Delete
$value = isset( $options[ $value ] ) ? $options[ $value ] : $value;
[1468] Fix | Delete
}
[1469] Fix | Delete
[1470] Fix | Delete
return $value;
[1471] Fix | Delete
}
[1472] Fix | Delete
[1473] Fix | Delete
/**
[1474] Fix | Delete
* Prepares a group name for use.
[1475] Fix | Delete
*
[1476] Fix | Delete
* @param string $group The group name to prepare.
[1477] Fix | Delete
* @return string The prepared group name.
[1478] Fix | Delete
*/
[1479] Fix | Delete
private function prepare_group_name( $group ) {
[1480] Fix | Delete
if ( ! in_array( $group, $this->groups, true ) ) {
[1481] Fix | Delete
$group = 'other';
[1482] Fix | Delete
}
[1483] Fix | Delete
return $group;
[1484] Fix | Delete
}
[1485] Fix | Delete
[1486] Fix | Delete
/**
[1487] Fix | Delete
* Prepares a location name for use.
[1488] Fix | Delete
*
[1489] Fix | Delete
* @param string $location The location name to prepare.
[1490] Fix | Delete
* @return string The prepared location name.
[1491] Fix | Delete
*/
[1492] Fix | Delete
private function prepare_location_name( $location ) {
[1493] Fix | Delete
if ( 'additional' === $location ) {
[1494] Fix | Delete
$location = 'order';
[1495] Fix | Delete
}
[1496] Fix | Delete
return $location;
[1497] Fix | Delete
}
[1498] Fix | Delete
[1499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function