2016-05-04 20 views
8

我有這個插件可以完成訂單前上傳圖片,但對於我的生活,我無法獲取要上傳的圖片。 $ _FILES將永遠不會返回任何內容,但我不確定爲什麼。我希望它是結賬將不會完成,直到圖像上傳,這是甚至可能嗎? *我已經告訴woocommerce使用Ajax的有沒有辦法在沒有插件的情況下在結賬完成之前上傳圖片?

<?php 
    /* 
     @package   UploadTest 
     @wordpress_plugin 
     Plugin Name:   UploadTest 
     Plugin URI:    null 
     Description:    
     Author:     Goodship 
     Version:    0.0.2 
     Author URI:    www.Goodship.co.za 
    */ 

    function add_image(){ 
     $testLog = fopen(plugin_dir_path(__FILE__)."testicle.txt","w") or exit ("Unable to open file!"); 
     //if they DID upload a file... 
     if($_FILES['testUpload']['name']){ 
      //if no errors... 
      if(!$_FILES['testUpload']['error']){ 
       //now is the time to modify the future file name and validate the file 
       $new_file_name = strtolower($_FILES['testUpload']['tmp_name']); //rename file 
       if($_FILES['testUpload']['size'] > (1024000)) //can't be larger than 1 MB 
       { 
        $valid_file = false; 
        $message = 'Oops! Your file\'s size is to large.'; 
       } 

       //if the file has passed the test 
       if($valid_file){ 
        //move it to where we want it to be 
        move_uploaded_file($_FILES['testUpload']['tmp_name'], plugin_dir_path(__FILE__).'uploads/'.$new_file_name); 
        $message = 'Congratulations! Your file was accepted.'; 
       } 
      } 
      //if there is an error... 
      else 
      { 
       //set that to be the returned message 
       $message = 'Ooops! Your upload triggered the following error: '.$_FILES['testUpload']['error']; 
      } 
     } 
     fwrite ($testLog ,$message); 
     fwrite ($testLog ,var_dump($_FILES)); 
     fclose ($testLog); 
    } 

    add_action('woocommerce_checkout_update_order_meta', 'add_image'); 


    function add_checkout_notice() { 
     echo '<input type="file" name="testUpload" />'; 
    } 
    add_action('woocommerce_checkout_before_customer_details', 'add_checkout_notice'); 

    ?> 
+1

您通過ajax提交結帳表單嗎? –

+0

結帳完成只需使用JS來檢查圖像的輸入字段是否爲空。如果它是空的,則在完整的結帳按鈕上放置一個「禁用」,以便您無法完成結帳。然後,一旦你上傳了圖片(我假設JS必須以某種方式參與),只需從結賬按鈕中刪除'disabled'即可。 至於爲什麼它不上傳,沒有線索... ... –

+0

@Maha Dev結帳正常提交,無論任何過程woocommerce使用 –

回答

4

你需要在你的子主題如下函數調用。

function add_image($order_id){ 
    // Do your code of upload image. 
} 
add_action('woocommerce_checkout_order_processed', 'add_image', 1, 1 ); 
+0

不幸的是,沒有任何效果,並且測試文件沒有記錄 –

2

Woocommerce Checkout將始終通過Ajax發生(我不知道從它一直是哪個版本是這樣)

PLUGIN-DIR/woocommerce/assets/frontend/checkout.js這是負責結帳操作的文件。

因此,從結帳頁面上傳文件是不可能的,除非您打算自己修改checkout.js文件。

如果你還喜歡從結賬頁面上傳文件,你可以參考這個answer

+0

有趣,謝謝 –

相關問題