如何从自定义文本字段调用值并将其动态放置在表单属性中?
How can I call a value from a Custom Text Field and dynamically place it in Form Attribute?
我在 WordPress 电子商务网站上工作,我在其中使用 WooCommerce 平台来支持购物功能。
我创建了一个自定义文本框,它显示在产品页面上。此文本框允许站点访问者输入他们希望在其产品上显示的任何文本。为了创建这个自定义文本框,我在 functions.php
文件中输入了以下代码:
<div>
<label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="2" maxlength="3" />
</label>
</div>
如您所见,我 manually/statically 输入了 minlength="2" maxlength="3"
属性。我不想手动输入上述属性,而是想在 WooCommerce 产品仪表板中动态调用自定义字段中的值。
WooCommerce 产品仪表板:自定义字段
要在 WooCommerce 产品仪表板中创建自定义字段,我将以下代码输入到 functions.php
文件中:
function product_custom_lettering(){
echo '<div class="product_custom_field">';
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'product_custom_lettering');
有人知道如何实现吗?
这个函数有custom_attributes个参数,可以传递你需要的参数。我已将 "name" 属性添加到您的函数中:
function product_custom_lettering(){
echo '<div class="product_custom_field">';
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'name' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'product_custom_lettering');
接下来需要添加保存自定义字段功能,你好像没有添加:
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save' );
function woocommerce_product_custom_fields_save($post_id)
{
$woocommerce_custom_product_text_field = $_POST['_minimum_engrave_text_option'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr($woocommerce_custom_product_text_field));
}
所以,现在我们已经有了一个可以通过 wp-admin 设置的自定义字段。
最后我们需要在自定义代码中调用它:
<div>
<label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_engrave_text_option',true);?>" maxlength="3" />
</label>
</div>
我在 WordPress 电子商务网站上工作,我在其中使用 WooCommerce 平台来支持购物功能。
我创建了一个自定义文本框,它显示在产品页面上。此文本框允许站点访问者输入他们希望在其产品上显示的任何文本。为了创建这个自定义文本框,我在 functions.php
文件中输入了以下代码:
<div>
<label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="2" maxlength="3" />
</label>
</div>
如您所见,我 manually/statically 输入了 minlength="2" maxlength="3"
属性。我不想手动输入上述属性,而是想在 WooCommerce 产品仪表板中动态调用自定义字段中的值。
WooCommerce 产品仪表板:自定义字段
要在 WooCommerce 产品仪表板中创建自定义字段,我将以下代码输入到 functions.php
文件中:
function product_custom_lettering(){
echo '<div class="product_custom_field">';
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'product_custom_lettering');
有人知道如何实现吗?
这个函数有custom_attributes个参数,可以传递你需要的参数。我已将 "name" 属性添加到您的函数中:
function product_custom_lettering(){
echo '<div class="product_custom_field">';
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'name' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'product_custom_lettering');
接下来需要添加保存自定义字段功能,你好像没有添加:
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save' );
function woocommerce_product_custom_fields_save($post_id)
{
$woocommerce_custom_product_text_field = $_POST['_minimum_engrave_text_option'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr($woocommerce_custom_product_text_field));
}
所以,现在我们已经有了一个可以通过 wp-admin 设置的自定义字段。
最后我们需要在自定义代码中调用它:
<div>
<label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_engrave_text_option',true);?>" maxlength="3" />
</label>
</div>