Gan*_*nly 7 php wordpress woocommerce
所以我有一个使用woocommerce的电子商务,我使用自定义运费来跟踪运费.我已经添加了新的输入数据(选择).就像你在下面的图片中看到的那样:

// 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;
}
Run Code Online (Sandbox Code Playgroud)
Woocommerce默认数据有address_1,address_2,country,state,city但我还需要一个名为subdistrict的数据.我不需要保存该数据(subdistrict).但我需要使用该值作为跟踪运费的参数.
我已经创建了新的class-custom-shipping-delivery.php.我已经确保该函数完美运行,因为我已经尝试手动设置$ subdistrict数据.
//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
));
Run Code Online (Sandbox Code Playgroud)
摘要:
如何从Subdistrict输入类型选择值(在结帐页面上)?
对不起,我只是从另一个人的工作编辑,所以我根本不理解该代码.但我认为他们忘记了这个价值,因为他们只是硬编码而且我是wordpress的新手所以我不知道如何在结帐表单上传递数据.
经过一段时间的寻找答案后,我得到了答案。
因此,总结一下:上面的答案使用会话从运输自定义字段(ajax)获取数据。所以当AJAX运行时。我将“分区”字段的值发送给函数并将其保存到会话中。
function ajax_process($subdistrict){
//some code
session_start();
$_SESSION['subdistrict'] = $subdistrict;
}
Run Code Online (Sandbox Code Playgroud)
然后为了从其他函数获取会话数据,我运行以下代码:
@session_start();
$subdistrict = $_SESSION['subdistrict'];
Run Code Online (Sandbox Code Playgroud)