Woocommerce Custom Product Text

I am trying to create a variable product using woocommerce that will include user input text that is specific to each product. To do this, I detect the type "custom_text" and display a text input field instead of the standard drop-down menu.

This is in the .php variable:

<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>"> <?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; <?php if(is_array($options) && $options[0] == "custom_text") : //Name to be added to product ?> <label for="<?php echo sanitize_title($name); ?>"><?php echo $woocommerce->attribute_label($name); ?></label></td> <input type="text" class="fullwidth req" id="<?php echo esc_attr( sanitize_title($name) ); ?>" name="attribute_<?php echo sanitize_title($name); ?>"/> <?php else : ?> ... 

This works , except when you get to the cart page, which displays the input, since the entire lower case with spaces removed (changed to hyphens), etc.

Does anyone know where you can connect and / or override this behavior? I tried everything to no avail.

thanks

+6
source share
1 answer

I found the answer if someone was wondering.

  // Get value from post data // Don't use woocommerce_clean as it destroys sanitized characters $value = sanitize_title( trim( stripslashes( $_REQUEST[ $taxonomy ] ) ) ); 

The previous one is on line 339 in woocommerce-functions.php. It must be changed to:

  $value = trim( stripslashes( $_REQUEST[ $taxonomy ] ) ) 

Now it’s just a matter of overriding this file correctly. I copied the original function from woocommerce-functions.php and added it to my functions.php theme. Then I changed it so that it does not sanitize user input.

This is what I added functions.php to my theme:

 add_action( 'init', 'override_add_to_cart_action' ); function override_add_to_cart_action( $url = false ) { // Original function is woocommerce_add_to_cart_action() // ... full function above and below $value = trim( stripslashes( $_REQUEST[ $taxonomy ] ) ); // ... } 

Thus, we do not need to change the kernel files, allowing us to update the plugin when necessary. :)

+4
source

All Articles