$ _FILES在woocommerce checkout中不使用ajax提交

时间:2015-08-14 11:42:05

标签: ajax wordpress woocommerce

我尝试在woocommerce结帐页面上传文件,但我无法上传文件。我也尝试在class-wc-checkout.php文件中的create_order函数中使用print_r($_FILES)获取值,但它将返回空白数组 当我从结帐表单中删除课程结账时,它将正常工作(没有ajax提交)

我想提交没有ajax的表单,但我需要在结帐页面上保留ajax,因为我使用ajax添加了一些额外的价格(更新价格没有刷新)

我的ajax电话要求增加额外费用。

   jQuery('#PageCount').change(function () {
            var state = jQuery('#PageCount').val();
            var current_price = jQuery('#carttot').val();
        var data = {
            action: 'woocommerce_apply_state',
            security: wc_checkout_params.apply_state_nonce,
            state: state,
            current_price: current_price
          };
        jQuery.ajax({
            type: 'POST',
            url: wc_checkout_params.ajax_url,
            data: data,
            success: function (code) {
           console.log(code);
                if (code === '0') {
                 jQuery('body').trigger('update_checkout');
                }
            },
            dataType: 'html'
        });
        return false;
    });

和php代码或woocommerce挂钩增加额外费用

add_action('wp_ajax_woocommerce_apply_state', 'calculate_per_page', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'calculate_per_page', 10);

function calculate_per_page() {
    if (isset($_POST['state'])) {
        global $woocommerce;
        $weight = WC()->cart->cart_contents_weight;
        $state = $_POST['state'];
        $current_price = $_POST['current_price'];
               $val =  $current_price * $state -$current_price;
        session_start();
        $_SESSION['val'] = $val;
    }
}

add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');

function woo_add_cart_fee() {
    session_start();
    $extracost = $_SESSION['val'];
    WC()->cart->add_fee('Per page charges:', $extracost);
}

我上传文件的功能..

add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
function custom_checkout_field_update_order_meta( $order_id ) {
if ($_FILES['avatar']['name']) {
 if ( ! function_exists( 'wp_handle_upload' ) ) {
                  require_once( ABSPATH . 'wp-admin/includes/file.php' );
               }
               $upload_dir = wp_upload_dir(); 

               $path = $upload_dir['path']; 

              $fileName = $_FILES["avatar"]["name"]; 
              $fileTmpLoc = $_FILES["avatar"]["tmp_name"];
              $pathAndName = $path."/".$fileName;
              $moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
              update_post_meta( $order_id, 'uploaded_file_to_translate', $_FILES['avatar']['name']);

  if ($_POST['billing_output_to_send']) 
    update_post_meta( $order_id, 'How do you want your translation sent to you?', esc_attr(htmlspecialchars($_POST['billing_output_to_send'])));
  if ($_POST['PageCount']) 
    update_post_meta( $order_id, 'How many pages are in your documents?', esc_attr(htmlspecialchars($_POST['PageCount'])));


         } 
}

怎么可能?

0 个答案:

没有答案