向 show/hide 结帐字段添加复选框并在 WooCommerce 后端显示值
Add a checkbox to show/hide checkout fields and display the values in WooCommerce backend
我坚持以下。基于 答案代码,我添加了一个复选框,show/hides 在结帐时输入三个。
为此,我使用了 Jquery 并连接到 WooCommerce 结帐字段,我让那部分工作了。我无法实现的是在 WooCommerce 后端显示这些字段。
我已将它们添加到试图连接到 woocommerce_admin_order_data_after_billing_address
的订单仪表板,但我只得到 <p>
没有内容的元素字符串。
示例:Ettevõtte 地址:(空)
我阅读了很多帖子和 WooCommerce 文档,但我似乎找不到这方面的错误。
有什么建议吗?
我的代码尝试:
add_action( 'woocommerce_before_checkout_form', 'conditionally_show_hide_checkout_field' );
function conditionally_show_hide_checkout_field() {
?>
<style>
p#new_billing_field_field.on { display:none !important; }
p#new_billing_field2_field.on { display:none !important; }
p#new_billing_field3_field.on { display:none !important; }
</style>
<script type="text/javascript">
jQuery(function($){
var a = 'input#checkbox_trigger', b = '#new_billing_field_field,#new_billing_field2_field,#new_billing_field3_field '
$(a).change(function(){
if ( $(this).prop('checked') === true && $(b).hasClass('on') ) {
$(b).show(function(){
$(b).css({'display':'none'}).removeClass('on').show();
});
}
else if ( $(this).prop('checked') !== true && ! $(b).hasClass('on') ) {
$(b).fadeOut(function(){
$(b).addClass('on')
});
$(b+' input').val('');
}
});
});
</script>
<?php
}
add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields' );
function add_custom_checkout_fields( $fields ) {
$fields['billing']['checkbox_trigger'] = array(
'type' => 'checkbox',
'label' => __('Ostan ettevõttena', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['new_billing_field'] = array(
'label' => __('Ettevõtte nimi', 'woocommerce'),
'placeholder' => _x('Ettevõtte nimi', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide on'),
'clear' => true
);
$fields['billing']['new_billing_field2'] = array(
'label' => __('Ettevõtte registrikood ', 'woocommerce'),
'placeholder' => _x('Ettevõtte registrikood ', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide on'),
'clear' => true
);
$fields['billing']['new_billing_field3'] = array(
'label' => __('Ettevõtte aadress', 'woocommerce'),
'placeholder' => _x('Ettevõtte aadress', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide on'),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Ostan ettevõttena').':</strong> ' . get_post_meta( $order->get_id(), '_new_billing_field', true ) . '</p>';
echo '<p><strong>'.__('Ettevõtte aadress').':</strong> ' . get_post_meta( $order->get_id(), '_new_billing_field3', true ) . '</p>';
echo '<p><strong>'.__('Ettevõtte registrikood').':</strong> ' . get_post_meta( $order->get_id(), '_new_billing_field2', true ) . '</p>';
}
除了你交替使用new_billing_field
和_new_billing_field
(注意下划线)之外,你是否没有代码来有效地保存字段中的值,然后才能显示它们在其他位置。
所以你得到:
function action_woocommerce_before_checkout_form() {
?>
<style>
p#new_billing_field_field.on { display:none !important; }
p#new_billing_field2_field.on { display:none !important; }
p#new_billing_field3_field.on { display:none !important; }
</style>
<script type="text/javascript">
jQuery(function($){
var a = 'input#checkbox_trigger';
var b = '#new_billing_field_field,#new_billing_field2_field,#new_billing_field3_field';
$(a).change(function() {
if ( $(this).prop('checked') === true && $(b).hasClass('on') ) {
$(b).show(function(){
$(b).css({'display':'none'}).removeClass('on').show();
});
}
else if ( $(this).prop('checked') !== true && ! $(b).hasClass('on') ) {
$(b).fadeOut(function(){
$(b).addClass('on')
});
$(b+' input').val('');
}
});
});
</script>
<?php
}
add_action( 'woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form' );
function filter_woocommerce_checkout_fields( $fields ) {
$fields['billing']['checkbox_trigger'] = array(
'type' => 'checkbox',
'label' => __( 'Ostan ettevõttena', 'woocommerce' ),
'class' => array( 'form-row-wide' ),
'clear' => true
);
$fields['billing']['new_billing_field'] = array(
'label' => __( 'Ettevõtte nimi', 'woocommerce' ),
'placeholder' => _x( 'Ettevõtte nimi', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-wide on' ),
'clear' => true
);
$fields['billing']['new_billing_field2'] = array(
'label' => __( 'Ettevõtte registrikood ', 'woocommerce' ),
'placeholder' => _x( 'Ettevõtte registrikood ', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-wide on' ),
'clear' => true
);
$fields['billing']['new_billing_field3'] = array(
'label' => __( 'Ettevõtte aadress', 'woocommerce' ),
'placeholder' => _x( 'Ettevõtte aadress', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-wide on' ),
'clear' => true
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'filter_woocommerce_checkout_fields', 10, 1 );
// Save fields
function action_woocommerce_checkout_create_order( $order, $data ) {
// Isset and NOT empty, save
if ( isset( $_POST['new_billing_field'] ) && ! empty( $_POST['new_billing_field'] ) ) {
// Update meta data
$order->update_meta_data( '_new_billing_field', sanitize_text_field( $_POST['new_billing_field'] ) );
}
if ( isset( $_POST['new_billing_field2'] ) && ! empty( $_POST['new_billing_field2'] ) ) {
// Update meta data
$order->update_meta_data( '_new_billing_field2', sanitize_text_field( $_POST['new_billing_field2'] ) );
}
if ( isset( $_POST['new_billing_field3'] ) && ! empty( $_POST['new_billing_field3'] ) ) {
// Update meta data
$order->update_meta_data( '_new_billing_field3', sanitize_text_field( $_POST['new_billing_field3'] ) );
}
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
// Display field values on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Get meta
$new_billing_field = $order->get_meta( '_new_billing_field' );
// NOT empty
if ( ! empty ( $new_billing_field ) ) {
echo '<p><strong>' . __( 'Ostan ettevõttena', 'woocommerce' ) . ':</strong> ' . $new_billing_field . '</p>';
}
// Get meta
$new_billing_field2 = $order->get_meta( '_new_billing_field2' );
// NOT empty
if ( ! empty ( $new_billing_field2 ) ) {
echo '<p><strong>' . __( 'Ettevõtte aadress', 'woocommerce' ) . ':</strong> ' . $new_billing_field2 . '</p>';
}
// Get meta
$new_billing_field3 = $order->get_meta( '_new_billing_field3' );
// NOT empty
if ( ! empty ( $new_billing_field3 ) ) {
echo '<p><strong>' . __( 'Competitor dob day', 'woocommerce' ) . ':</strong> ' . $new_billing_field3 . '</p>';
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
我坚持以下。基于
为此,我使用了 Jquery 并连接到 WooCommerce 结帐字段,我让那部分工作了。我无法实现的是在 WooCommerce 后端显示这些字段。
我已将它们添加到试图连接到 woocommerce_admin_order_data_after_billing_address
的订单仪表板,但我只得到 <p>
没有内容的元素字符串。
示例:Ettevõtte 地址:(空)
我阅读了很多帖子和 WooCommerce 文档,但我似乎找不到这方面的错误。
有什么建议吗?
我的代码尝试:
add_action( 'woocommerce_before_checkout_form', 'conditionally_show_hide_checkout_field' );
function conditionally_show_hide_checkout_field() {
?>
<style>
p#new_billing_field_field.on { display:none !important; }
p#new_billing_field2_field.on { display:none !important; }
p#new_billing_field3_field.on { display:none !important; }
</style>
<script type="text/javascript">
jQuery(function($){
var a = 'input#checkbox_trigger', b = '#new_billing_field_field,#new_billing_field2_field,#new_billing_field3_field '
$(a).change(function(){
if ( $(this).prop('checked') === true && $(b).hasClass('on') ) {
$(b).show(function(){
$(b).css({'display':'none'}).removeClass('on').show();
});
}
else if ( $(this).prop('checked') !== true && ! $(b).hasClass('on') ) {
$(b).fadeOut(function(){
$(b).addClass('on')
});
$(b+' input').val('');
}
});
});
</script>
<?php
}
add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields' );
function add_custom_checkout_fields( $fields ) {
$fields['billing']['checkbox_trigger'] = array(
'type' => 'checkbox',
'label' => __('Ostan ettevõttena', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['new_billing_field'] = array(
'label' => __('Ettevõtte nimi', 'woocommerce'),
'placeholder' => _x('Ettevõtte nimi', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide on'),
'clear' => true
);
$fields['billing']['new_billing_field2'] = array(
'label' => __('Ettevõtte registrikood ', 'woocommerce'),
'placeholder' => _x('Ettevõtte registrikood ', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide on'),
'clear' => true
);
$fields['billing']['new_billing_field3'] = array(
'label' => __('Ettevõtte aadress', 'woocommerce'),
'placeholder' => _x('Ettevõtte aadress', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide on'),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Ostan ettevõttena').':</strong> ' . get_post_meta( $order->get_id(), '_new_billing_field', true ) . '</p>';
echo '<p><strong>'.__('Ettevõtte aadress').':</strong> ' . get_post_meta( $order->get_id(), '_new_billing_field3', true ) . '</p>';
echo '<p><strong>'.__('Ettevõtte registrikood').':</strong> ' . get_post_meta( $order->get_id(), '_new_billing_field2', true ) . '</p>';
}
除了你交替使用new_billing_field
和_new_billing_field
(注意下划线)之外,你是否没有代码来有效地保存字段中的值,然后才能显示它们在其他位置。
所以你得到:
function action_woocommerce_before_checkout_form() {
?>
<style>
p#new_billing_field_field.on { display:none !important; }
p#new_billing_field2_field.on { display:none !important; }
p#new_billing_field3_field.on { display:none !important; }
</style>
<script type="text/javascript">
jQuery(function($){
var a = 'input#checkbox_trigger';
var b = '#new_billing_field_field,#new_billing_field2_field,#new_billing_field3_field';
$(a).change(function() {
if ( $(this).prop('checked') === true && $(b).hasClass('on') ) {
$(b).show(function(){
$(b).css({'display':'none'}).removeClass('on').show();
});
}
else if ( $(this).prop('checked') !== true && ! $(b).hasClass('on') ) {
$(b).fadeOut(function(){
$(b).addClass('on')
});
$(b+' input').val('');
}
});
});
</script>
<?php
}
add_action( 'woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form' );
function filter_woocommerce_checkout_fields( $fields ) {
$fields['billing']['checkbox_trigger'] = array(
'type' => 'checkbox',
'label' => __( 'Ostan ettevõttena', 'woocommerce' ),
'class' => array( 'form-row-wide' ),
'clear' => true
);
$fields['billing']['new_billing_field'] = array(
'label' => __( 'Ettevõtte nimi', 'woocommerce' ),
'placeholder' => _x( 'Ettevõtte nimi', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-wide on' ),
'clear' => true
);
$fields['billing']['new_billing_field2'] = array(
'label' => __( 'Ettevõtte registrikood ', 'woocommerce' ),
'placeholder' => _x( 'Ettevõtte registrikood ', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-wide on' ),
'clear' => true
);
$fields['billing']['new_billing_field3'] = array(
'label' => __( 'Ettevõtte aadress', 'woocommerce' ),
'placeholder' => _x( 'Ettevõtte aadress', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-wide on' ),
'clear' => true
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'filter_woocommerce_checkout_fields', 10, 1 );
// Save fields
function action_woocommerce_checkout_create_order( $order, $data ) {
// Isset and NOT empty, save
if ( isset( $_POST['new_billing_field'] ) && ! empty( $_POST['new_billing_field'] ) ) {
// Update meta data
$order->update_meta_data( '_new_billing_field', sanitize_text_field( $_POST['new_billing_field'] ) );
}
if ( isset( $_POST['new_billing_field2'] ) && ! empty( $_POST['new_billing_field2'] ) ) {
// Update meta data
$order->update_meta_data( '_new_billing_field2', sanitize_text_field( $_POST['new_billing_field2'] ) );
}
if ( isset( $_POST['new_billing_field3'] ) && ! empty( $_POST['new_billing_field3'] ) ) {
// Update meta data
$order->update_meta_data( '_new_billing_field3', sanitize_text_field( $_POST['new_billing_field3'] ) );
}
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
// Display field values on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Get meta
$new_billing_field = $order->get_meta( '_new_billing_field' );
// NOT empty
if ( ! empty ( $new_billing_field ) ) {
echo '<p><strong>' . __( 'Ostan ettevõttena', 'woocommerce' ) . ':</strong> ' . $new_billing_field . '</p>';
}
// Get meta
$new_billing_field2 = $order->get_meta( '_new_billing_field2' );
// NOT empty
if ( ! empty ( $new_billing_field2 ) ) {
echo '<p><strong>' . __( 'Ettevõtte aadress', 'woocommerce' ) . ':</strong> ' . $new_billing_field2 . '</p>';
}
// Get meta
$new_billing_field3 = $order->get_meta( '_new_billing_field3' );
// NOT empty
if ( ! empty ( $new_billing_field3 ) ) {
echo '<p><strong>' . __( 'Competitor dob day', 'woocommerce' ) . ':</strong> ' . $new_billing_field3 . '</p>';
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );