我有這個插件可以完成訂單前上傳圖片,但對於我的生活,我無法獲取要上傳的圖片。 $ _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');
?>
您通過ajax提交結帳表單嗎? –
結帳完成只需使用JS來檢查圖像的輸入字段是否爲空。如果它是空的,則在完整的結帳按鈕上放置一個「禁用」,以便您無法完成結帳。然後,一旦你上傳了圖片(我假設JS必須以某種方式參與),只需從結賬按鈕中刪除'disabled'即可。 至於爲什麼它不上傳,沒有線索... ... –
@Maha Dev結帳正常提交,無論任何過程woocommerce使用 –