试图添加Woo Commerce Shipping国家和州下拉到自定义帐户页面
我是与PHP合作的新手。
我正在尝试将Woo Commerce Country和State Drows添加到自定义用户帐户页面上。
- 我需要从usermeta获得用户国家 /地区(结帐 页面运输详细信息)
- 我需要在帐户页面上显示这些信息, 因此,用户可以根据需要编辑它们 - 按更新按钮保存 更新的国家和声明为同一usermeta密钥。
- 另外,如果用户 在下拉列表中更改国家,我需要州选项 更改与选择的国家相对应。
只是想知道我需要什么代码?
我认为我可以通过以下方式出现该国下拉列表:
<?php
global $woocommerce;
woocommerce_form_field( 'billing_country', array( 'type' => 'country' ) );
?>
但是我不确定如何获取用于计费国家的现有usermeta数据,
不确定如何使其更新以更新
不确定如何使状态出现以及如何获取状态下拉列表选择哪个国家 /地区更改的选项。
预先感谢
Jaydev-我回顾了页面链接,但这似乎正在使用特定于Woo Commerce帐户页面的挂钩。我正在建立一个自定义页面。
从您发送的链接和我们已经尝试过的自定义页面的链接中,但似乎没有用。有什么想法吗?
我尝试了以下内容,但似乎没有用。有什么想法吗?
在function.php中
add_action( 'wp_ajax_account_details_information', 'account_details_information' );
add_action( 'wp_ajax_nopriv_account_details_information', 'account_details_information' );
function account_details_information(){
$user_id=$_POST['user_id'];
$first_name=get_user_meta($user_id,'first_name',true);
$last_name=get_user_meta($user_id,'last_name',true);
$dob=get_user_meta($user_id,'birthday',true);
$gender=get_user_meta($user_id,'gender',true);
if($gender=="unknown"){
$gender= "Prefer not to say";
}
$phone=get_user_meta($user_id,'shipping_phone_number',true);
$user_info = get_userdata($user_id);
$email=$user_info->user_email;
$address1=get_user_meta($user_id,'shipping_address_1',true);
$address2=get_user_meta($user_id,'shipping_address_2',true);
$country=get_user_meta($user_id,'shipping_country',true);
$state=get_user_meta($user_id,'shipping_region',true);
$city=get_user_meta($user_id,'shipping_city',true);
$shipping_zip_code=get_user_meta($user_id,'shipping_zip_code',true);
$add=$address1.' '.$address2.' '.$city.' '.$state.' '.$shipping_zip_code;
$result=array('first_name'=>$first_name,'last_name'=>$last_name,'dob'=>$dob,'gender'=>$gender,
'phone'=>$phone,'address'=>$add,'email'=>$email,'address_1'=>$address1,'address_2'=>$address2,
'country'=>$country,'state'=>$state,'city'=>$city,'zipcode'=>$shipping_zip_code);
echo json_encode($result);
die();
}
add_action( 'wp_ajax_account_details_information_save', 'account_details_information_save' );
add_action( 'wp_ajax_nopriv_account_details_information_save', 'account_details_information_save' );
function account_details_information_save(){
$user_id=$_POST['user_id'];
update_user_meta($user_id,'first_name',$_POST['first_name']);
update_user_meta($user_id,'last_name',$_POST['last_name']);
update_user_meta($user_id,'birthday',$_POST['dob']);
update_user_meta($user_id,'gender',$_POST['gender']);
update_user_meta($user_id,'shipping_address_1',$_POST['address_1']);
update_user_meta($user_id,'shipping_address_2',$_POST['address_2']);
update_user_meta($user_id,'shipping_country',$_POST['country']);
update_user_meta($user_id,'shipping_region',$_POST['state']);
update_user_meta($user_id,'shipping_city',$_POST['city']);
update_user_meta($user_id,'shipping_zip_code',$_POST['zipcode']);
update_user_meta($user_id,'shipping_phone_number',$_POST['phone']);
$result=array('code'=>200);
echo json_encode($result);
die();
}
in Custom Page.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $woocommerce;
?>
<div class="col-md-6">
<div class="form-group">
<label for="shipping_country">Country</label>
<label for="shipping_country"><?php woocommerce_form_field( 'shipping_country', array( 'type' => 'country' ) ); ?></label>
</div>
</div>
I am quite new to working with PHP.
I am trying to add woo commerce country and state drop down to custom user account page.
- I need to get the users country and state from usermeta (checkout
page shipping details) - I need to display these on the account page,
so user can edit them if they want - press update button to save the
updated country and state to the same usermeta key. - Also if the user
changes the country in the drop down I need the state options to
change corresponding to the country choosen.
Just wondering what code I would need for this?
I think I can get the country dropdown to appear by:
<?php
global $woocommerce;
woocommerce_form_field( 'billing_country', array( 'type' => 'country' ) );
?>
But I am not sure how to get the existing usermeta data for billing country
Not sure how to get it to update
Not sure how to get state to appear and how to get the state dropdown options to change with what country is chosen.
Thanks in advance
Thanks JayDev - I reviewed the page link but that seems to be using hooks specific to the woo commerce account page. I am building a custom page.
From the link you sent and the custom page we already have I tried this but it did not seem to work. Any Ideas?
I tried the following but it did not seem to work .. any ideas?
in function.php
add_action( 'wp_ajax_account_details_information', 'account_details_information' );
add_action( 'wp_ajax_nopriv_account_details_information', 'account_details_information' );
function account_details_information(){
$user_id=$_POST['user_id'];
$first_name=get_user_meta($user_id,'first_name',true);
$last_name=get_user_meta($user_id,'last_name',true);
$dob=get_user_meta($user_id,'birthday',true);
$gender=get_user_meta($user_id,'gender',true);
if($gender=="unknown"){
$gender= "Prefer not to say";
}
$phone=get_user_meta($user_id,'shipping_phone_number',true);
$user_info = get_userdata($user_id);
$email=$user_info->user_email;
$address1=get_user_meta($user_id,'shipping_address_1',true);
$address2=get_user_meta($user_id,'shipping_address_2',true);
$country=get_user_meta($user_id,'shipping_country',true);
$state=get_user_meta($user_id,'shipping_region',true);
$city=get_user_meta($user_id,'shipping_city',true);
$shipping_zip_code=get_user_meta($user_id,'shipping_zip_code',true);
$add=$address1.' '.$address2.' '.$city.' '.$state.' '.$shipping_zip_code;
$result=array('first_name'=>$first_name,'last_name'=>$last_name,'dob'=>$dob,'gender'=>$gender,
'phone'=>$phone,'address'=>$add,'email'=>$email,'address_1'=>$address1,'address_2'=>$address2,
'country'=>$country,'state'=>$state,'city'=>$city,'zipcode'=>$shipping_zip_code);
echo json_encode($result);
die();
}
add_action( 'wp_ajax_account_details_information_save', 'account_details_information_save' );
add_action( 'wp_ajax_nopriv_account_details_information_save', 'account_details_information_save' );
function account_details_information_save(){
$user_id=$_POST['user_id'];
update_user_meta($user_id,'first_name',$_POST['first_name']);
update_user_meta($user_id,'last_name',$_POST['last_name']);
update_user_meta($user_id,'birthday',$_POST['dob']);
update_user_meta($user_id,'gender',$_POST['gender']);
update_user_meta($user_id,'shipping_address_1',$_POST['address_1']);
update_user_meta($user_id,'shipping_address_2',$_POST['address_2']);
update_user_meta($user_id,'shipping_country',$_POST['country']);
update_user_meta($user_id,'shipping_region',$_POST['state']);
update_user_meta($user_id,'shipping_city',$_POST['city']);
update_user_meta($user_id,'shipping_zip_code',$_POST['zipcode']);
update_user_meta($user_id,'shipping_phone_number',$_POST['phone']);
$result=array('code'=>200);
echo json_encode($result);
die();
}
in custom page.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $woocommerce;
?>
<div class="col-md-6">
<div class="form-group">
<label for="shipping_country">Country</label>
<label for="shipping_country"><?php woocommerce_form_field( 'shipping_country', array( 'type' => 'country' ) ); ?></label>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论