-
-
Save bekarice/fa52c810b3ba71a53ade to your computer and use it in GitHub Desktop.
<?php // only copy this line if needed! | |
/** | |
* Add attributes to our checkout fields. | |
* | |
* @param array $checkout_fields the checkout field data | |
*/ | |
function wc_add_checkout_add_ons_attributes( $checkout_fields ) { | |
$add_on_id = 'abc123'; // change to your add-on id | |
if ( isset( $checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ] ) ) { | |
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['maxlength'] = "300"; | |
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['description'] = "Please add no more than 300 characters."; | |
// Adds a maximum character length + description to this add on | |
} | |
// repeat for each add-on | |
$add_on_id = 'abc124'; // change to your add-on id | |
if ( isset( $checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ] ) ) { | |
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['maxlength'] = "140"; | |
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['placeholder'] = "My custom placeholder"; | |
// Add a maximum length and placeholder | |
} | |
return $checkout_fields; | |
} | |
add_filter( 'woocommerce_checkout_fields', 'wc_add_checkout_add_ons_attributes', 20 ); |
This code works (note change to the add_on_id):
// Add attributes to our checkout fields
function wc_add_checkout_add_ons_attributes( $checkout_fields ) {
$add_on_id = 3; // change 3 to your add-on id
if ( isset( $checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ] ) ) {
$checkout_fields['add_ons'][ $add_on_id ]['maxlength'] = "300";
$checkout_fields['add_ons'][ $add_on_id ]['description'] = "Please add no more than 300 characters.";
//Adds a maximum character length + description to this add on
}
// repeat for each add-on
$add_on_id = 7; // change 7 to your add-on id
if ( isset( $checkout_fields['add_ons'][ $add_on_id ] ) ) {
$checkout_fields['add_ons'][ $add_on_id ]['maxlength'] = "140";
$checkout_fields['add_ons'][ $add_on_id ]['placeholder'] = "My custom placeholder";
//Add a maximum length and placeholder
}
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_add_checkout_add_ons_attributes', 20 );
Hey,
I used the code as follow, but it is not working and it returns an error on checkout page :
ERROR :
Warning: Use of undefined constant f5472d2 - assumed 'f5472d2' (this will throw an Error in a future version of PHP) in /homepages/2/d660419233/htdocs/clickandbuilds/PremierMoment/wp-content/themes/flatsome-child/functions.php on line 25
OUR CODE, in functions.php :
function wc_add_checkout_add_ons_attributes( $checkout_fields ) {
$add_on_id = f5472d2; // I also tried with "f5472d2", the error message disapear, but the code is still not working
if ( isset( $checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ] ) ) {
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['maxlength'] = "250";
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['placeholder'] = "250 characters max";
//Add a maximum length and placeholder
}
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_add_checkout_add_ons_attributes', 20 );
Any help ?
Many thanks ! :)
Hey Beka, is there any documentation on adding validation to a field or is the "validate" attribute something other than a bool or a range, etc?
For example:
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['validate'] = $validate;
Can the string be called and validate if field was "valid"? I am mostly looking for a guide to the attributes but I will inevitably end of reverse engineering the plugin functions.