I'm modifying this code and need to place the "optionals" div in the product summary hook, if I replace the but if I replace woocommerce_before_add_to_cart_button with woocommerce_single_product_summary `the wk_add_to_cart_validation function fails for each check.
/**
* Add a custom input field to the product page.
*/
function wk_add_text_field() { ?>
<div class="custom-field-1-wrap">
<label for="vuoiMontare">Vuoi montare gli attacchi?</label>
<select id="vuoiMontare" name="vuoiMontare">
<option value="no"><?php esc_html_e( 'No', 'theme-domain' ); ?></option>
<option value="si"><?php esc_html_e( 'Sì', 'theme-domain' ); ?></option>
</select>
<br>
<div class="opzioni-attacchi">
<label for="altezza">
<?php esc_html_e( 'Altezza (cm):', 'theme-domain' ); ?>
</label>
<input type="text" name='altezza' id='altezza' value=''>
<br>
<label for="peso">
<?php esc_html_e( 'Peso (kg):', 'theme-domain' ); ?>
</label>
<input type="text" name='peso' id='peso' value=''>
<br>
<label for="lunghezzaScarpone">
<?php esc_html_e( 'Lung. Scarpone (mm):', 'theme-domain' ); ?>
</label>
<input type="text" name='lunghezzaScarpone' id='lunghezzaScarpone' value=''>
<br>
<label for="eta">
<?php esc_html_e( 'Età:', 'webkul' ); ?>
</label>
<input type="text" name='eta' id='eta' value=''>
</div>
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'wk_add_text_field' );
/**
* Validate custom input field value
*/
function wk_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null ) {
if ( empty( $_POST['vuoiMontare'] ) || $_POST['vuoiMontare'] === 'si' ) {
// Check if any of the custom fields is empty
$custom_fields = array( 'altezza', 'peso', 'lunghezzaScarpone', 'eta' );
foreach ( $custom_fields as $field ) {
if ( empty( $_POST[ $field ] ) ) {
$passed = false;
wc_add_notice( sprintf( __( '%s is a required field.', 'theme-domain' ), ucfirst( $field ) ), 'error' );
}
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'wk_add_to_cart_validation', 10, 4 );
/**
* Add custom cart item data
*/
function wk_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
// Get form data
$vuoiMontare = sanitize_text_field($_POST['vuoiMontare']);
$altezza = sanitize_text_field($_POST['altezza']);
$peso = sanitize_text_field($_POST['peso']);
$lunghezzaScarpone = sanitize_text_field($_POST['lunghezzaScarpone']);
$eta = sanitize_text_field($_POST['eta']);
$cart_item_data['pr_field'] = array(
'vuoiMontare' => $vuoiMontare,
'altezza' => $altezza,
'peso' => $peso,
'lunghezzaScarpone' => $lunghezzaScarpone,
'eta' => $eta,
);
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'wk_add_cart_item_data', 10, 3 );
/**
* Display custom item data in the cart
*/
function wk_get_item_data( $item_data, $cart_item_data ) {
if ( isset( $cart_item_data['pr_field'] ) ) {
$item_data[] = array(
'key' => __( 'Montare attacchi?', 'theme-domain' ),
'value' => wc_clean( $cart_item_data['pr_field']['vuoiMontare'] ),
);
$item_data[] = array(
'key' => __( 'Altezza', 'theme-domain' ),
'value' => wc_clean( $cart_item_data['pr_field']['altezza'] ) . 'cm',
);
$item_data[] = array(
'key' => __( 'Peso', 'theme-domain' ),
'value' => wc_clean( $cart_item_data['pr_field']['peso'] ) . 'Kg',
);
$item_data[] = array(
'key' => __( 'Lunghezza scarpone', 'theme-domain' ),
'value' => wc_clean( $cart_item_data['pr_field']['lunghezzaScarpone'] ) . 'mm',
);
$item_data[] = array(
'key' => __( 'Età', 'theme-domain' ),
'value' => wc_clean( $cart_item_data['pr_field']['eta'] ),
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'wk_get_item_data', 10, 2 );
For single product frontend custom form field, it requires to be inside the add-to-cart form.
To use
woocommerce_single_product_summaryhook for those custom form fields, you need additional corresponding custom hidden input fields inside the add-to-cart form and some JavaScript.Now there are some mistakes in your code, so I have revised everything. All your field settings are now in a custom function.
When loading the product page, only "Vuoi montare?" select field is visible. If the customer select "Si", then the other fields are displayed.
Everything works as JavaScript populate the hidden fields with the customer inputted data.
The complete code:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.