Last active
August 31, 2022 09:47
-
-
Save bekarice/fa52c810b3ba71a53ade to your computer and use it in GitHub Desktop.
Add custom attributes to the WooCommerce Checkout Add-ons form elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
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 ! :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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_filter( 'woocommerce_checkout_fields', 'wc_add_checkout_add_ons_attributes', 20 );