dongpochi9741 2016-11-01 03:26 采纳率: 100%
浏览 40
已采纳

Wordpress Woocommerce从定制运输领域(AJAX)获得价值

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: enter image description here

// 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 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.

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
));

Summary:

How to get Value from Subdistrict Input type select (on checkout page)?

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.

  • 写回答

2条回答 默认 最新

  • dq23171 2016-11-07 22:00
    关注

    After searching the answer for a while then I get the answer.

    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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?