I have this plugin I've made for uploading an image before an order can be completed, but for the life of me I can't get the image to upload. $_FILES will always return nothing, I'm not sure why though. I'd like it to be that the checkout won't be completed until the image is uploaded, is this even possible? *I've been told that woocommerce uses ajax for the cart
我有这个插件,我已经为完成订单上传图像,但对于我的生活,我无法上传图像。 $ _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');
?>
3 个解决方案
#1
4
You need to call below function in your child theme.
您需要在您的子主题中调用以下函数。
function add_image($order_id){
// Do your code of upload image.
}
add_action( 'woocommerce_checkout_order_processed', 'add_image', 1, 1 );
#2
2
Woocommerce checkout will always happens via Ajax ( I am not sure from what version it's been like this )
Woocommerce结帐将始终通过Ajax发生(我不确定它是什么版本)
PLUGIN-DIR/woocommerce/assets/frontend/checkout.js
this is the file which is responsible for the checkout action.
PLUGIN-DIR / woocommerce / assets / frontend / checkout.js这是负责结账操作的文件。
So uploading files from checkout page is not possible unless you intended to modify the checkout.js
file by yourself.
因此,除非您打算自行修改checkout.js文件,否则无法从结帐页面上传文件。
If you still prefer uploading file from checkout page, you may refer this answer.
如果您仍然希望从结帐页面上传文件,可以参考此答案。
#3
1
Have you given a look at Advanced Custom Field? https://www.advancedcustomfields.com/
您是否看过Advanced Custom Field? https://www.advancedcustomfields.com/
You can easily achieve what you are trying to do.
您可以轻松实现您想要做的事情。
Have a look at this https://www.advancedcustomfields.com/resources/acfupload_prefilter/
看看这个https://www.advancedcustomfields.com/resources/acfupload_prefilter/
#1
4
You need to call below function in your child theme.
您需要在您的子主题中调用以下函数。
function add_image($order_id){
// Do your code of upload image.
}
add_action( 'woocommerce_checkout_order_processed', 'add_image', 1, 1 );
#2
2
Woocommerce checkout will always happens via Ajax ( I am not sure from what version it's been like this )
Woocommerce结帐将始终通过Ajax发生(我不确定它是什么版本)
PLUGIN-DIR/woocommerce/assets/frontend/checkout.js
this is the file which is responsible for the checkout action.
PLUGIN-DIR / woocommerce / assets / frontend / checkout.js这是负责结账操作的文件。
So uploading files from checkout page is not possible unless you intended to modify the checkout.js
file by yourself.
因此,除非您打算自行修改checkout.js文件,否则无法从结帐页面上传文件。
If you still prefer uploading file from checkout page, you may refer this answer.
如果您仍然希望从结帐页面上传文件,可以参考此答案。
#3
1
Have you given a look at Advanced Custom Field? https://www.advancedcustomfields.com/
您是否看过Advanced Custom Field? https://www.advancedcustomfields.com/
You can easily achieve what you are trying to do.
您可以轻松实现您想要做的事情。
Have a look at this https://www.advancedcustomfields.com/resources/acfupload_prefilter/
看看这个https://www.advancedcustomfields.com/resources/acfupload_prefilter/