Php 在WooCommerce中将签出自定义字段数据添加到装运包中以进行计算
我正在创建一个插件,该插件将使用API计算自定义装运变量。我有一个jQuery脚本,它根据输入的地址计算邮政编码和fias编码Php 在WooCommerce中将签出自定义字段数据添加到装运包中以进行计算,php,jquery,wordpress,woocommerce,shipping,Php,Jquery,Wordpress,Woocommerce,Shipping,我正在创建一个插件,该插件将使用API计算自定义装运变量。我有一个jQuery脚本,它根据输入的地址计算邮政编码和fias编码 $("#billing_address_1").suggestions({ serviceUrl: "https://suggestions.dadata.ru/suggestions/api/4_1/rs", token: php_vars.dadata_suggest_token,
$("#billing_address_1").suggestions({
serviceUrl: "https://suggestions.dadata.ru/suggestions/api/4_1/rs",
token: php_vars.dadata_suggest_token,
type: "ADDRESS",
count: 5,
onSelect: function (suggestion) {
$("#billing_city").val(suggestion.data.city);
$("#billing_state").val(suggestion.data.region);
$("#billing_postcode").val(suggestion.data.postal_code);
if (suggestion.data.settlement_fias_id)
$("#billing_fias_code").val(suggestion.data.settlement_fias_id);
else if (suggestion.data.city_fias_id)
$("#billing_fias_code").val(suggestion.data.city_fias_id);
else
$("#billing_fias_code").val('');
}
});
为了存储fias代码,我创建了自定义字段
add_filter( 'woocommerce_checkout_fields' , array( $this, 'custom_checkout_fields' ));
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'shipping_apartment_update_order_meta') );
function custom_checkout_fields( $fields ) {
$fields['shipping']['shipping_fias_code'] = array(
'type' => 'text',
'label' => __('FIAS', 'woocommerce'),
'placeholder' => _x('Код', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_fias_code'] = array(
'type' => 'text',
'label' => __('FIAS', 'woocommerce'),
'placeholder' => _x('Код', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
function shipping_apartment_update_order_meta( $order_id ) {
if ( ! empty( $_POST['shipping_fias_code'] ) ) {
update_post_meta( $order_id, 'shipping_fias_code', sanitize_text_field( $_POST['shipping_fias_code'] ) );
}
if ( ! empty( $_POST['billing_fias_code'] ) ) {
update_post_meta( $order_id, 'billing_fias_code', sanitize_text_field( $_POST['billing_fias_code'] ) );
}
}
woocommerce中WC_shipping_方法中的calculate_shipping()方法使用$package变量计算配送选项,该变量有一个包含配送地址信息的“destination”数组字段。默认情况下,邮政编码将传递到此数组中。但是我还需要在$package中传递我的自定义字段
据我所知,我创建的字段将保存通过jQuery脚本添加的信息,只有在表单发布之后。但是$package['destination']中包含的其他字段在添加信息后会立即保存
如何将签出表单中的自定义字段中的数据添加到$package['destination']
变量中?我无法测试(或修改)您的jQuery代码,因此您必须自己处理,可能需要对其进行一些更改。我已经完全重温了您的所有代码(jQuery代码除外),一切都按照您的预期运行
因此,$package['destination']
将为'fias\u code'
添加一个条目
注释代码:
//添加账单和发货字段
添加_过滤器('woocommerce_billing_fields'、'custom_billing_fields');
添加_过滤器('woocommerce_shipping_fields'、'custom_shipping_fields');
函数自定义装运字段($fields){
$fields['shipping\u fias\u code']=数组(
'类型'=>'文本',
“标签”=>“‘FIAS’、‘woocommerce’”,
“占位符”=>x('kerköС','placeholder','woocommerce'),
“必需”=>false,
'class'=>array('form-row-wide'),
“清除”=>true
);
返回$fields;
}
功能自定义\账单\字段($fields){
$fields['billing\u fias\u code']=数组(
'类型'=>'文本',
“标签”=>“‘FIAS’、‘woocommerce’”,
“占位符”=>x('kerköС','placeholder','woocommerce'),
“必需”=>false,
'class'=>array('form-row-wide'),
“清除”=>true
);
返回$fields;
}
//Ajax发送器
添加动作('wp_footer'、'checkout_发送_fias_code_通过_ajax_js');
函数签出\u发送\u fias\u代码\u通过\u ajax\u js(){
如果(is_checkout()&&!is_wc_endpoint_url()):
?>
jQuery(函数($){
if(wc\u checkout\u参数的类型==“未定义”)
返回false;
//发送Ajax请求的函数
函数sendAjaxRequest(值,字段集='billing'){
$.ajax({
键入:“POST”,
url:wc_checkout_params.ajax_url,
数据:{
“行动”:“fias_代码”,
“fias_代码”:值,
“字段集”:字段集
},
成功:功能(结果){
$(document.body).trigger('update_checkout');//更新签出过程
console.log(结果);//用于测试(发送输出数据)
}
});
}
//计费fias代码更改和输入事件
$(document.body).on('change input','input[name=billing\u fias\u code]',function(){
sendAjaxRequest($(this.val());
});
//装运fias代码更改和输入事件
$(document.body).on('change input','input[name=shipping\u fias\u code]',function(){
sendAjaxRequest($(this.val(),'shipping');
});
});
谢谢,你的回答对我很有帮助!:)