【问题标题】:Is there a way to upload an image before checkout completion without a plugin?有没有办法在没有插件的情况下在结帐完成之前上传图片?
【发布时间】: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


【解决方案1】:

Woocommerce 结帐将始终通过 Ajax 进行(我不确定它是从哪个版本开始的)

PLUGIN-DIR/woocommerce/assets/frontend/checkout.js这是负责结帐操作的文件。

因此,除非您打算自己修改 checkout.js 文件,否则无法从结帐页面上传文件。

如果您仍然喜欢从结帐页面上传文件,您可以参考此answer

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 可惜没有效果,测试文件什么也没有记录
    【解决方案3】:

    您看过高级自定义字段吗? https://www.advancedcustomfields.com/

    您可以轻松实现您想要做的事情。

    看看这个https://www.advancedcustomfields.com/resources/acfupload_prefilter/

    【讨论】:

    • 我需要在没有插件的情况下这样做,上传的项目数量在后期编辑时将无法确定,并且会有所不同。
    猜你喜欢
    • 2011-06-02
    • 1970-01-01
    • 2016-07-07
    • 2012-03-29
    • 2015-10-12
    • 2021-09-24
    • 1970-01-01
    • 2011-10-31
    • 2019-12-18
    相关资源
    最近更新 更多