php - 有没有办法在没有插件的情况下在结帐完成前上传图片?

标签 php ajax wordpress file woocommerce

我制作了这个插件,用于在完成订单之前上传图片,但我这辈子都无法上传图片。 $_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');

    ?>

最佳答案

您需要在子主题中调用以下函数。

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

关于php - 有没有办法在没有插件的情况下在结帐完成前上传图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025129/

相关文章:

php - 在没有预先准备好的语句的情况下清理 MySQL 查询(PHP + 旧的 mysql 模块)

php - 交响乐 :HOW TO Create Shared -General- (Helper) used in multiple bundles

java - android 在网络上发布数据

javascript - ASP.NET 以编程方式从 AJAX 插入表行

php - 存档产品的 `<ul>` 的 Woocommerce 位置

wordpress - wordpress中的主题目录路径

javascript - Stripe 付款问题 - 网络错误,您尚未被收取费用

python - 如何使用 AJAX 使用相同的 html 模板(使用 Python 和 Django)创建和编辑新帖子?

c# - 运行时 ASP.NET 确认对话框

php - ssl 页面不安全不知道为什么 https ://www. englishandproud.org/donate/