【问题标题】:Woocommerce change upload file interfaceWoocommerce 更改上传文件界面
【发布时间】:2022-12-23 00:21:13
【问题描述】:

我在网上找到了一个代码,它允许我为每个订单附加自定义 PDF 发票。但问题是我不想使用经典的媒体上传器界面,我只想保留与网站相关的图像和文件,并且在此代码中有非常简单的上传文件按钮,用于在单独的子文件夹 /woocommerce_upload/invoices 中上传发票受 htaccess 保护。我尝试了几种方法来改变它,但没有任何运气:(

<?php


defined( 'ABSPATH' ) || exit;

class AttachFilesToWooCommerceOrderEmail {
private static $instance;

private $privateUploads;  // Whether to upload files to WooCommerce protected area.


// Returns an instance of this class. 
public static function get_instance() {
    if ( null == self::$instance ) {
        self::$instance = new self;
    } 
    return self::$instance;
}


// Initialize the plugin variables.
private function __construct() {
    $this->privateUploads = false;

    $this->init();
}


// Set up WordPress specfic actions.
private function init() {
    // Verify that CMB2 plugin is active.
    add_action( 'admin_notices', array( $this, 'verify_cmb2_active' ) );

    // Add the metabox to allow for adding files to attach to the "Completed order" email.
    add_action( 'cmb2_admin_init', array( $this, 'order_files_metabox' ) );
    
    add_filter( 'woocommerce_email_attachments', array( $this, 'conditionally_attach_order_files_to_order_email' ), 10, 4 );

    // For testing.
    //add_action( 'woocommerce_email_order_details', array( $this, 'testing_order_attachments_code' ), 10, 4 );

    if ( $this->privateUploads ) {
        add_filter( 'plupload_default_params', array( $this, 'add_page_type_to_upload_field' ) );
        add_filter( 'upload_dir', array( $this, 'change_upload_dir_for_shop_order_uploads' ), 20 );
    }

}


// Verify that CMB2 plugin is active.
function verify_cmb2_active() {
    if ( ! defined( 'CMB2_LOADED' ) ) {
        $plugin_data = get_plugin_data( __FILE__ );
        $plugin_name = $plugin_data['Name'];
?>
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo 
$plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div>
<?php
        //error_log( 'CMB2 is not active.' );
    }
}


// Add the metabox to allow for adding files to attach to the "Completed order" email.
function order_files_metabox() {
    // When private uploads is enabled previews may not be available so mention this in the metabox description text.
    $privateUploadsMessage = '';
    if ( $this->privateUploads ) {
        $privateUploadsMessage = '<br/><br/>Note: Private uploads is enabled so previews of attachments may not be available.';
    }

    $cmb = new_cmb2_box( array(
        'id'            => 'order_attachments',
        'title'         => 'Order Attachments',
        'object_types'  => array( 'shop_order', ), // Limit to Order post type
        'context'       => 'side',
        'priority'      => 'high',
        'show_names'    => true, // Show field names on the left
    ) );
    $cmb->add_field( array(
        'desc' => 'Upload the files that will be attached to the "Completed order" email.<br/><br/>The files must be uploaded <strong>before</strong> marking the order Complete.' . $privateUploadsMessage,
        'id'   => 'order_file_list',
        'type' => 'file_list',
        'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
        'query_args' => array( 'type' => array( 'image', 'application/pdf' ) ), // Set to only allow image attachments. This can be disabled or edited.
    ) );
}


function conditionally_attach_order_files_to_order_email( $attachments, $email_id, 
$object, $email_obj ) {
    // Only attach files to Completed Order email, otherwise return early.
    if ( 'customer_completed_order' != $email_id ) {
        return $attachments;
    }
    
    $files = get_post_meta( $object->get_id(), 'order_file_list', true );
    foreach ( (array) $files as $attachment_id => $attachment_url ) {
        $attachments[] = get_attached_file( $attachment_id );
    }

    return $attachments;
}


// Add the post type as a field that will be available when the file is uploaded. This
// will be used to change the upload destination directory for uploads from 'shop_order' type.
function add_page_type_to_upload_field( $params ) {
    if ( ! is_admin() ) { return $params; }
    
    $screen = get_current_screen();
    if ( isset( $screen ) ){
        $params['post_type'] = $screen->post_type;
    }
    
    return $params;
}


// Put uploads from a 'shop_order' into the same directory as downloadable files. This
// directory is protected with a .htaccess file to prevent direct access.
function change_upload_dir_for_shop_order_uploads( $pathdata ) {
    //error_log( 'upload_dir $_POST: ' . var_export( $_POST, true ) );

    // This code is (almost) identical to that in upload_dir() in woocommerce/includes/admin/class-wc-admin-post-types.php.
    if( (isset( $_POST['post_type'] ) && 'shop_order' === $_POST['post_type'] )) {
        if ( empty( $pathdata['subdir'] ) ) {
            $pathdata['path']   = $pathdata['path'] . '/woocommerce_uploads/invoices';
            $pathdata['url']    = $pathdata['url'] . '/woocommerce_uploads/invoices';
            $pathdata['subdir'] = '/woocommerce_uploads';
            //error_log( 'Empty subdir: ' . var_export( $pathdata, true ) );
        } else {
            $new_subdir = '/woocommerce_uploads/invoices' . $pathdata['subdir'];
            $pathdata['path']   = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
            $pathdata['url']    = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
            $pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
            //error_log( 'Non-empty subdir: ' . var_export( $pathdata, true ) );
        }
    }
    
    return $pathdata;
}


// Test the code - list the paths to the attachments.
function testing_order_attachments_code( $order, $sent_to_admin, $plain_text, $email ) {
    $files = get_post_meta( $order->get_id(), 'order_file_list', true );

    echo '<pre>', var_export( $files, true ), '</pre>';

    foreach ( (array) $files as $attachment_id => $attachment_url ) {
        echo '<p>', get_attached_file( $attachment_id ), '</p>';
    }
}
}

$AttachFilesToWooCommerceOrderEmail = AttachFilesToWooCommerceOrderEmail::get_instance();

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    要更改上传文件按钮以将发票上传到 /woocommerce_uploads/invoices 文件夹,您可以尝试修改以下代码:

    if ( $this->privateUploads ) {
        add_filter( 'plupload_default_params', array( $this, 'add_page_type_to_upload_field' ) );
        add_filter( 'upload_dir', array( $this, 'change_upload_dir_for_shop_order_uploads' ), 20 );
    }
    

    您可以将 $this->privateUploads 设置为 true,这将启用 add_page_type_to_upload_field 和 change_upload_dir_for_shop_order_uploads 过滤器。然后,您可以修改 change_upload_dir_for_shop_order_uploads 函数,将 upload_dir 路径设置为 /woocommerce_upload/invoices 文件夹。

    以下是如何修改 change_upload_dir_for_shop_order_uploads 函数的示例:

    function change_upload_dir_for_shop_order_uploads( $upload ) {
        if ( isset( $_REQUEST['page_type'] ) && 'shop_order' === $_REQUEST['page_type'] ) {
            $upload['subdir'] = '/woocommerce_upload/invoices';
            $upload['path']   = $upload['basedir'] . $upload['subdir'];
            $upload['url']    = $upload['baseurl'] . $upload['subdir'];
        }
        return $upload;
    }
    

    这应该将附加到“已完成订单”电子邮件的文件的上传目录更改为 /woocommerce_upload/invoices 文件夹。您可能需要根据您的 WordPress 安装调整路径和 URL。

    请注意,您还需要确保 /woocommerce_upload/invoices 文件夹可由 WordPress 写入,以便成功上传文件。

    【讨论】:

      猜你喜欢
      • 2010-09-27
      • 2014-09-13
      • 2013-07-10
      • 2022-01-16
      • 2016-05-28
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多