简体   繁体   English

在结帐页面上保存选择字段woocommerce

[英]save select field on checkout page woocommerce

I'm new to WooCommerce.I'm unable to figure out where is problem here is my code 我是WooCommerce的新手,我无法弄清楚问题出在哪里,这是我的代码

I have added a select field in billing form of checkout page. 我已经在结帐页面的结算表单中添加了一个选择字段。

Problem 问题

records are not saving or updating on submitting. 记录在提交时不会保存或更新。 Problem is in Update the order meta with field value.value is not updating in database 问题是在更新具有字段value.value的订单meta.value在数据库中未更新

// checkout page customization start

global $post, $woocommerce; 
// Account select field
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields($fields) {

     $fields['billing']['Account'] = array(
'type'          => 'select',
'class'         => array('billing form-row-wide'),
'label'         => __('Choose an Account'),
'placeholder'   => _x('Account', 'placeholder', 'woocommerce'),
'options'     => array(
                    '' => __( 'Select Account','' ),              
                    ),
'required'    => true,
 );
  return $fields;

}



add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;

if (!$_POST['Account'])
$woocommerce->add_error( __('Please enter your Account.'.$_POST['Account']));
}
///**

Problem area value is not updating in database 问题区域值未在数据库中更新

//* Update the order meta with field value
//**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['Account']) update_post_meta( $order_id, 'Account',      esc_attr($_POST['Account']));
}

/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['Account']) update_user_meta( $user_id, 'Account', esc_attr($_POST['Account']) );
}

The functions look pretty accurate to me. 这些功能对我来说看起来很准确。 Are you sure they aren't updating? 您确定他们没有更新吗? Or perhaps you are having difficulty is displaying them. 也许您在显示它们时遇到困难。

I've tweaked them a bit. 我对它们进行了一些调整。 First, to use the $posted variabled that WooCommerce sends to the function, though this is trivial as $_POST should be the same. 首先,使用WooCommerce发送给函数的$posted变量,尽管这很简单,因为$_POST应该相同。 And secondly, you are using esc_attr() when you should be using sanitize_text_field() . 其次,当您应该使用sanitize_text_field()时,您正在使用esc_attr() sanitize_text_field() The former is for displaying the data in an HTML attribute while the latter is for sanitizing before saving. 前者用于在HTML属性中显示数据,而后者用于在保存之前进行清理。

//* Update the order meta with field value
//**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 2 );

function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
    if ( isset( $posted['Account'] ) ){
        update_post_meta( $order_id, 'Account', sanitize_text_field( $posted['Account'] ) );
    }
}

/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta', 10, 2 );
function my_custom_checkout_field_update_user_meta( $user_id, $posted ) {
    if ( $user_id && isset( $posted['Account'] ) ){
        update_user_meta( $user_id, 'Account', sanitize_text_field( $posted['Account'] ) );
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM