Type caché n'existe pas par défaut d'achat pour les champs de formulaire WooCommerce ... Mais vous pouvez le créer.
- La première fonction créera ce type de champ masqué.
- La 2ème fonction ajoutera ce champ caché personnalisé (où vous définissez la valeur)
- La troisième fonction affiche cette valeur pour modifier la page
Voici le code:
// Create hidden checkout field type
add_filter('woocommerce_form_field_hidden', 'create_checkout_hidden_field_type', 5, 4);
function create_checkout_hidden_field_type($field, $key, $args, $value){
return '<input type="hidden" name="'.esc_attr($key).'" id="'.esc_attr($args['id']).'" value="'.esc_attr($args['default']).'" />';
}
// Add custom hidden billing checkout field
add_filter('woocommerce_checkout_fields', 'custom_billing_fields');
function custom_billing_fields($fields){
## HERE set the value (for this hidden checkout field)
$value = "The value";
$fields['billing']['billing_quickbook'] = array(
'type' => 'hidden',
'label' => __('Purchase Order Number', 'woocommerce'),
'placeholder' => _x('Purchase Order Number', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'default' => $value, // The custom field value
);
return $fields;
}
// Display the field value on the admin order edit page (after billing address)
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_field_in_admin_order_meta', 10, 1);
function display_custom_field_in_admin_order_meta($order){
echo '<p><strong>'.__('Quickbook').':</strong> ' . get_post_meta($order->get_id(), '_billing_quickbook', true) . '</p>';
}
Le code va dans le fichier function.php de votre thème enfant actif (ou thème) ou dans n'importe quel fichier plugin.
USAGES(RÉCUPÉRATION DE LA VALEUR):
Pour obtenir la valeur de l'ID de commande $order_id
, vous utiliserez (si nécessaire):
$value = get_post_meta($order_id, '_billing_quickbook', true);
Ce code a été testé et fonctionne dans WooCommerce 3+.
documentation développeur officiel: Customizing checkout fields using actions and filters