Wordpress Woocommerce 从自定义运输字段 (AJAX) 中获取价值 [英] Wordpress Woocommerce get value from custom shipping field (AJAX)
问题描述
所以我有一个使用 woocommerce 的电子商务,我使用自定义运输来跟踪运输费用.我已经添加了新的输入数据(选择).如下图所示:
So I have an ecommerce using woocommerce and I use custom shipping for track shipping fee. And I already add new input data (select). like you can see on below picture:
// Hook in
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_city'] = array(
'type' => 'select',
'label' => __('Kota / Kabupaten', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kota / Kabupaten'
)
);
$fields['shipping']['shipping_city'] = array(
'type' => 'select',
'label' => __('Kota / Kabupaten', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kota / Kabupaten'
)
);
$fields['billing']['billing_subdistrict'] = array(
'type' => 'select',
'label' => __('Kecamatan', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kecamatan'
)
);
$fields['shipping']['shipping_subdistrict'] = array(
'type' => 'select',
'label' => __('Kecamatan', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kecamatan'
)
);
return $fields;
}
Woocommerce 默认数据有 address_1、address_2、country、state、city,但我还需要一个名为 subdistrict 的数据.我不需要保存该数据(分区).但我需要使用该值作为跟踪运费的参数.
Woocommerce default data had address_1,address_2,country,state,city but I need one more data called subdistrict. I don't need to save that data (subdistrict). But I need to use that value as parameter for track shipping fee.
我已经创建了新的 class-custom-shipping-delivery.php.我已经确保该函数完美运行,因为我已经尝试手动设置 $subdistrict 数据.
I already create new class-custom-shipping-delivery.php. and I already make sure that function work perfectly because I already try to set $subdistrict data manually.
//custom-shipping.php
$province = $package['destination']['state'];
$city = $package['destination']['city'];
$subdistrict= 'something';
//How to get the data from custom field (ajax)
//because I need to see the shipping fee result before Checkout (and update it to add rate)
$destination_code = $this->getDestinationCode($province,$city,$subdistrict);
$ongkir = $this->cek_ongkir($origin,$destination_code,$weight);
//print_r();
// send the final rate to the user.
$this->add_rate(array(
'id' => $this->id,
'label' => $this->title,
'cost' => $ongkir
));
总结:
如何从分区输入类型选择中获取值(在结帐页面上)?
How to get Value from Subdistrict Input type select (on checkout page)?
对不起,我只是从另一个人的作品中编辑,所以我根本不理解该代码.但我认为他们忘记获取该值,因为他们只是对其进行了硬编码,而且我是 wordpress 的新手,所以我不知道如何在结帐表单上传递数据.
Sorry I just edit from another person work so I'm not understand that code at all. But I think they forgot to get that value because they just hardcode it and I'm a newbie on wordpress so I don't know how to pass data on checkout form.
推荐答案
搜索了一段时间后,我得到了答案.
After searching the answer for a while then I get the answer.
所以总结一下:以上答案使用会话从运输自定义字段(ajax)中获取数据.所以当 AJAX 运行时.我将subdistrict"字段的值发送到函数并将其保存到会话中.
So for Summary it: Above answer use session to get the data from shipping custom field (ajax). So when AJAX run. I send value of 'subdistrict' field to function and save it to session.
function ajax_process($subdistrict){
//some code
session_start();
$_SESSION['subdistrict'] = $subdistrict;
}
然后为了从其他函数中获取会话数据,我运行此代码:
then for get the session data from other function I run this code :
@session_start();
$subdistrict = $_SESSION['subdistrict'];
这篇关于Wordpress Woocommerce 从自定义运输字段 (AJAX) 中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!