我制作了这个插件,用于在订单完成前上传图像,但就我而言,我无法上传图像$_FILES总是不会返回任何内容,但我不知道为什么。我希望在上传图像之前不会完成结账,这可能吗?*有人告诉我,wooccommerce使用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');
?>
您需要在子主题中调用以下函数。
function add_image($order_id){
// Do your code of upload image.
}
add_action( 'woocommerce_checkout_order_processed', 'add_image', 1, 1 );
Wooccommerce结账总是通过Ajax进行(我不确定是什么版本)
PLUGIN-DIR/woocommerce/assets/frontend/checkout.js
这是负责签出操作的文件。
因此,除非您打算自己修改checkout.js
文件,否则无法从签出页面上传文件。
如果您仍然喜欢从结账页面上传文件,您可以参考这个答案。
您看过高级自定义字段吗?https://www.advancedcustomfields.com/
你可以很容易地实现你想要做的事情。
看看这个https://www.advancedcustomfields.com/resources/acfupload_prefilter/