【发布时间】:2017-05-04 00:28:44
【问题描述】:
我正在开发一个具有 WooCommerce 功能的 WordPress 网站。
我使用以下代码为产品页面后端的产品数据框创建了 2 个自定义字段:
<?php
function theme_name_woocommerce_custom_fields() {
// Price Per Character
woocommerce_wp_text_input(
array(
'id' => '_character_price',
'label' => 'Price Per Character',
'description' => 'This is the amount a customer pays per letter entered.',
'desc_tip' => 'true',
'placeholder' => 'Enter Amount per Letter (Exclude the £)'
)
);
// Custom Text Box
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_box',
'label' => 'Show Custom Text Box',
'description' => 'Select this box, if you would like a Custom Text Box to appear on the Product's page.',
'desc_tip' => 'true',
)
);
}
add_action( 'woocommerce_product_options_general_product_data', 'theme_name_woocommerce_custom_fields' );
function save_theme_name_woocommerce_custom_field( $post_id ) {
if ( ! empty( $_POST['_custom_text_field'] ) ) {
update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
}
}
add_action( 'woocommerce_process_product_meta', 'save_theme_name_woocommerce_custom_fields' );
?>
参考woocommerce_wp_checkbox,我想创建一个函数,在选中此复选框时,它会在关联的产品页面上创建自定义文本框。然后,潜在客户可以使用此自定义文本框输入一段文本,他们希望将其打印到页面的产品中。
是否有人知道我需要输入哪些额外的代码才能实现上述目标?
【问题讨论】:
标签: php wordpress woocommerce