Issue after adding to the cart with custom input field in woocommerce
-
I have an issue trying to ad this code in child functions.php to get the text input field in a single product page and this error was returned after adding to the cart:
WARNING: CALL_USER_FUNC_ARRAY() EXPECTS PARAMETER 1 TO BE A VALID CALLBACK, FUNCTION ‘ NJENGAH_PRODUCT_ADD_ON_VALIDATION’ NOT FOUND OR INVALID FUNCTION NAME IN /USERS/LUISRODRIGUES/LOCAL SITES/QSH/APP/PUBLIC/WP-INCLUDES/CLASS-WP-HOOK.PHP ON LINE 289
Code added in functions.php to get tex input fields in single products page:
/**
* @snippet Add an input field to products – WooCommerce
*/
// —————————————–
// 1. Show custom input field above Add to Cart
add_action( ‘woocommerce_before_add_to_cart_button’, ‘njengah_product_add_on’, 9 );
function njengah_product_add_on() {
$terms = [‘desenhos’];
if ( has_term( $terms, ‘product_cat’, get_the_ID() ) ) {$value = isset( $_POST[‘custom_text_add_on’] ) ? sanitize_text_field( $_POST[‘_custom_text_add_on’] ) : ”;
echo ‘<div><label>Custom Text Add-On <abbr class=”required” title=”required”>*</abbr></label><p><input name=”custom_text_add_on” value=”‘ . $value . ‘”></p></div>’;
}
}// —————————————–
// 2. Throw error if custom input field empty
add_filter( ‘woocommerce_add_to_cart_validation’, ‘ njengah_product_add_on_validation’, 10, 3 );
function njengah_product_add_on_validation( $passed, $product_id, $qty ){
if( isset( $_POST[‘custom_text_add_on’] ) && sanitize_text_field( $_POST[‘custom_text_add_on’] ) == ” ) {
wc_add_notice( ‘Custom Text Add-On is a required field’, ‘error’ );
$passed = false;
}
return $passed;
}
// —————————————–
// 3. Save custom input field value into cart item data
add_filter( ‘woocommerce_add_cart_item_data’, ‘ njengah_product_add_on_cart_item_data’, 10, 2 );
function njengah_product_add_on_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST[‘custom_text_add_on’] ) ) {
$cart_item[‘custom_text_add_on’] = sanitize_text_field( $_POST[‘custom_text_add_on’] );
}
return $cart_item;
}
// —————————————–
// 4. Display custom input field value @ Cart
add_filter( ‘woocommerce_get_item_data’, ‘ njengah_product_add_on_display_cart’, 10, 2 );
function njengah_product_add_on_display_cart( $data, $cart_item ) {
if ( isset( $cart_item[‘custom_text_add_on’] ) ){
$data[] = array(
‘name’ => ‘Custom Text Add-On’,
‘value’ => sanitize_text_field( $cart_item[‘custom_text_add_on’] )
);
}
return $data;
}
// —————————————–
// 5. Save custom input field value into order item meta
add_action( ‘woocommerce_add_order_item_meta’, ‘ njengah_product_add_on_order_item_meta’, 10, 2 );
function njengah_product_add_on_order_item_meta( $item_id, $values ) {
if ( ! empty( $values[‘custom_text_add_on’] ) ) {
wc_add_order_item_meta( $item_id, ‘Custom Text Add-On’, $values[‘custom_text_add_on’], true );
}
}
// —————————————–
// 6. Display custom input field value into order table
add_filter( ‘woocommerce_order_item_product’, ‘ njengah_product_add_on_display_order’, 10, 2 );
function njengah_product_add_on_display_order( $cart_item, $order_item ){
if( isset( $order_item[‘custom_text_add_on’] ) ){
$cart_item[‘custom_text_add_on’] = $order_item[‘custom_text_add_on’];
}
return $cart_item;
}
// —————————————–
// 7. Display custom input field value into order emails
add_filter( ‘woocommerce_email_order_meta_fields’, ‘ njengah_product_add_on_display_emails’ );
function njengah_product_add_on_display_emails( $fields ) {
$fields[‘custom_text_add_on’] = ‘Custom Text Add-On’;
return $fields;
}
add_filter( ‘something’, ‘regis_options’ );`
The page I need help with: [log in to see the link]
- The topic ‘Issue after adding to the cart with custom input field in woocommerce’ is closed to new replies.