Woocommerce自定义产品文本 [英] Woocommerce Custom Product Text
问题描述
我正在尝试使用woocommerce创建可变产品,该产品将包括特定于每种产品的用户输入文本。为此,我正在检测 custom_text类型并显示文本输入字段,而不是正常的选择选项下拉列表。
I'm trying to create a variable product using woocommerce which will include user input text which is specific to each product. To do so I am detecting for 'custom_text' type and displaying a text input field instead of the normal select-option dropdown.
这在variable.php中:
This is in variable.php:
<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 is working except when you get to the cart page it is showing the input as all lower case with white spaces removed (changed to hyphens) etc.
有人知道您可以在哪里挂入和/或覆盖此行为?我一直在尝试一切都无济于事。
Does anyone know where you could hook in and/or override this behavior? I have been trying everything to no avail.
谢谢
推荐答案
// Get value from post data
// Don't use woocommerce_clean as it destroys sanitized characters
$value = sanitize_title( trim( stripslashes( $_REQUEST[ $taxonomy ] ) ) );
上一个位于woocommerce-functions.php中的339行。
需要更改为:
The previous is located on line 339 in woocommerce-functions.php. It needs to be changed to:
$value = trim( stripslashes( $_REQUEST[ $taxonomy ] ) )
现在只需要适当地覆盖此文件即可。我从woocommerce-functions.php复制了原始函数,并将其添加到主题的functions.php中。然后,我对其进行了更改,以免影响用户输入。
Now it's just a matter of overriding this file properly. I copied the original function from the woocommerce-functions.php and added it to my theme's functions.php. I then changed it so that it does not sanitize the user input.
这是我添加到主题功能中的内容。php:
This is what I added to my theme's functions.php:
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 ] ) );
// ...
}
通过这种方式我们可以无需更改任何核心文件,使我们可以在需要时更新插件。 :)
By doing it this way we do not have to change any core files allowing us to update the plugin when needed. :)
这篇关于Woocommerce自定义产品文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!