Ich müsste in den Rechnungsdetails auf Woocommerce ein Pflichtfeld für das Geburtsdatum hinzufügen und prüfen, ob sie 18 Jahre alt sind.
Wie könnte ich das machen?
Jede Hilfe wird geschätzt.
Maurizio Sellitti
Ich müsste in den Rechnungsdetails auf Woocommerce ein Pflichtfeld für das Geburtsdatum hinzufügen und prüfen, ob sie 18 Jahre alt sind.
Wie könnte ich das machen?
Jede Hilfe wird geschätzt.
LoicTheAztec
Der folgende Code fügt ein Feld für das Rechnungsgeburtsdatum hinzu und überprüft das Alter des Kunden, sodass der Kauf vermieden wird, wenn der Kunde nicht mindestens 18 Jahre alt ist:
// Adding a custom checkout date field
add_filter( 'woocommerce_billing_fields', 'add_birth_date_billing_field', 20, 1 );
function add_birth_date_billing_field($billing_fields) {
$billing_fields['billing_birth_date'] = array(
'type' => 'date',
'label' => __('Birth date'),
'class' => array('form-row-wide'),
'priority' => 25,
'required' => true,
'clear' => true,
);
return $billing_fields;
}
// Check customer age
add_action('woocommerce_checkout_process', 'check_birth_date');
function check_birth_date() {
// Check billing city 2 field
if( isset($_POST['billing_birth_date']) && ! empty($_POST['billing_birth_date']) ){
// Get customer age from birthdate
$age = date_diff(date_create($_POST['billing_birth_date']), date_create('now'))->y;
// Checking age and display an error notice avoiding checkout (and emptying cart)
if( $age < 18 ){
wc_add_notice( __( "You need at least to be 18 years old, to be able to checkout." ), "error" );
WC()->cart->empty_cart(); // <== Empty cart (optional)
}
}
}
Der Code befindet sich in der Datei „functions.php“ Ihres aktiven untergeordneten Themas (oder aktiven Themas). Getestet und funktioniert.
Wo wird dieser Code konkret hinzugefügt? dh welche Datei?
– Alex Stewart
16. Mai 2019 um 7:40 Uhr
@AlexStewart-Code geht in die Datei „functiond.php“ Ihres aktiven untergeordneten Themas (oder aktiven Themas)
– LoicTheAztec
16. Mai 2019 um 7:43 Uhr
@LoicTheAztec zeigt dieser Code neben den regulären Rechnungsfeldern auch das Feld im Benutzerprofil im Admin-Bereich an? Ist das Feld validiert?
– Zach Nicodemous
21. Januar 2022 um 21:42 Uhr