【发布时间】:2016-08-29 17:42:46
【问题描述】:
我有这个插件,用于在完成订单之前上传图片,但在我的一生中,我无法上传图片。 $_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检查图像的输入字段是否为空。如果它是空的,请在完成结帐按钮上放置一个
disabled,这样您就无法完成结帐。然后,一旦您上传了图像(我假设这里必须以某种方式涉及 JS),只需从结帐按钮中删除disabled。至于为什么不上传,不知道... -
@Maha Dev 结帐正常提交,无论 woocommerce 使用什么流程
-
@dingo_d 输入字段绝对不是空的,但是 $_FILES 是出于某种原因
-
您可能会更新问题以包括 woo-commerce 使用 ajax 来处理购物车。否则你不会得到正确的答案
标签: php ajax wordpress file woocommerce