为 WooCommerce 单品添加图像文件上传字段

我创建了一个自定义插件,它可以在 woocommerce 单一产品页面中动态显示自定义字段。显示字段,将其添加到购物车并添加到订单数据和电子邮件。然而,我花了几天时间尝试添加文件上传字段,但没有成功。该字段在前端显示如下:


add_action( 'woocommerce_before_add_to_cart_button', 'display_custom_fields' );

function display_custom_fields() {

?>

    <p class="form-row validate-required" id="image" >

        <span class="woocommerce-input-wrapper">

        <label for="image" class=""><?php echo $stamp_welcome_text; ?> </label> 

        

        <input type="file" name="image" accept="image/*" >

        </span>

    </p>

<?php

}

然后添加到购物车,例如:


add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10,3 );

function add_cart_item_data( $cart_item_data, $product_id ) {

    if ($_FILES['image'] ) {

        require_once( ABSPATH . 'wp-admin/includes/image.php' );

        require_once( ABSPATH . 'wp-admin/includes/file.php' );

        require_once( ABSPATH . 'wp-admin/includes/media.php' );

        $attachment_id = media_handle_upload( 'image', 0 );

        if ( is_wp_error( $attachment_id ) AND $_FILES['image']['size'] > 0) {

            die($attachment_id->get_error_message().'. Παρακαλώ επικοινωνήστε μαζί μας.');

        } else $cart_item_data['image'] = $attachment_id;

    }

    return $item_cart_data;

}

当然,这只是代码的一部分。其余字段工作完美。是的,如果有人想知道的话,我只尝试过代码本身。


我已经“玩”了好几天了,但我不知道出了什么问题。非常感谢任何帮助:)


湖上湖
浏览 110回答 3
3回答

一只斗牛犬

您可以尝试以下操作,将上传的图像数据存储为自定义购物车项目数据并将其另存为自定义订单项目元数据:// Display additional product fields (+ jQuery code)add_action( 'woocommerce_before_add_to_cart_button', 'display_additional_product_fields', 9 );function display_additional_product_fields(){&nbsp; &nbsp; ?>&nbsp; &nbsp; <p class="form-row validate-required" id="image" >&nbsp; &nbsp; &nbsp; &nbsp; <label for="file_field"><?php echo __("Upload Image") . ': '; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type='file' name='image' accept='image/*'>&nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp; &nbsp; </p>&nbsp; &nbsp; <?php}// Add custom fields data as the cart item custom dataadd_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_data_as_custom_cart_item_data', 10, 2 );function add_custom_fields_data_as_custom_cart_item_data( $cart_item, $product_id ){&nbsp; &nbsp; if( isset($_FILES['image']) && ! empty($_FILES['image']) ) {&nbsp; &nbsp; &nbsp; &nbsp; $upload&nbsp; &nbsp; &nbsp; &nbsp;= wp_upload_bits( $_FILES['image']['name'], null, file_get_contents( $_FILES['image']['tmp_name'] ) );&nbsp; &nbsp; &nbsp; &nbsp; $filetype&nbsp; &nbsp; &nbsp;= wp_check_filetype( basename( $upload['file'] ), null );&nbsp; &nbsp; &nbsp; &nbsp; $upload_dir&nbsp; &nbsp;= wp_upload_dir();&nbsp; &nbsp; &nbsp; &nbsp; $upl_base_url = is_ssl() ? str_replace('http://', 'https://', $upload_dir['baseurl']) : $upload_dir['baseurl'];&nbsp; &nbsp; &nbsp; &nbsp; $base_name&nbsp; &nbsp; = basename( $upload['file'] );&nbsp; &nbsp; &nbsp; &nbsp; $cart_item['file_upload'] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'guid'&nbsp; &nbsp; &nbsp; => $upl_base_url .'/'. _wp_relative_upload_path( $upload['file'] ), // Url&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_type' => $filetype['type'], // File type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_name' => $base_name, // File name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title'&nbsp; &nbsp; &nbsp;=> ucfirst( preg_replace('/\.[^.]+$/', '', $base_name ) ), // Title&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $cart_item['unique_key'] = md5( microtime().rand() ); // Avoid merging items&nbsp; &nbsp; }&nbsp; &nbsp; return $cart_item;}// Display custom cart item data in cart (optional)add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );function display_custom_item_data( $cart_item_data, $cart_item ) {&nbsp; &nbsp; if ( isset( $cart_item['file_upload']['title'] ) ){&nbsp; &nbsp; &nbsp; &nbsp; $cart_item_data[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => __( 'Image uploaded', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' =>&nbsp; str_pad($cart_item['file_upload']['title'], 16, 'X', STR_PAD_LEFT) . '…',&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp; return $cart_item_data;}// Save Image data as order item meta dataadd_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 );function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) {&nbsp; &nbsp; if ( isset( $values['file_upload'] ) ){&nbsp; &nbsp; &nbsp; &nbsp; $item->update_meta_data( '_img_file',&nbsp; $values['file_upload'] );&nbsp; &nbsp; }}// Admin orders: Display a linked button + the link of the image fileadd_action( 'woocommerce_after_order_itemmeta', 'backend_image_link_after_order_itemmeta', 10, 3 );function backend_image_link_after_order_itemmeta( $item_id, $item, $product ) {&nbsp; &nbsp; // Only in backend for order line items (avoiding errors)&nbsp; &nbsp; if( is_admin() && $item->is_type('line_item') && $file_data = $item->get_meta( '_img_file' ) ){&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><a href="'.$file_data['guid'].'" target="_blank" class="button">'.__("Open Image") . '</a></p>'; // Optional&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><code>'.$file_data['guid'].'</code></p>'; // Optional&nbsp; &nbsp; }}// Admin new order email: Display a linked button + the link of the image fileadd_action( 'woocommerce_email_after_order_table', 'wc_email_new_order_custom_meta_data', 10, 4);function wc_email_new_order_custom_meta_data( $order, $sent_to_admin, $plain_text, $email ){&nbsp; &nbsp; // On "new order" email notifications&nbsp; &nbsp; if ( 'new_order' === $email->id ) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($order->get_items() as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $file_data = $item->get_meta( '_img_file' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="'.$file_data['guid'].'" target="_blank" class="button">'.__("Download Image") . '</a><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <pre><code style="font-size:12px; background-color:#eee; padding:5px;">'.$file_data['guid'].'</code></pre>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p><br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}此代码位于活动子主题(或活动主题)的functions.php 文件中。在 Woocommerce 版本 4.3.x 中进行了测试,并使用所有类型的默认 WooCommerce 产品。

慕田峪7331174

您可以使用 Product Addons 等插件来完成此操作https://woocommerce.com/products/product-add-ons/这是文档的链接,专门解释了允许文件上传(它们可以是免费的,也可以按上传收费)https://docs.woocommerce.com/document/product-add-ons/#section-9希望这可以帮助!

PIPIONE

如果有人需要的话,可以获取多个图像。function add_custom_fields_data_as_custom_cart_item_data($cart_item){&nbsp; &nbsp; $images = ['front-image', 'back-image', 'sleeve-image'];&nbsp; &nbsp; foreach ($images as $image) {&nbsp; &nbsp; &nbsp; &nbsp; if (isset($_FILES[$image]) && $_FILES[$image]['error'] === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $upload&nbsp; &nbsp; &nbsp; &nbsp;= wp_upload_bits($_FILES[$image]['name'], null, file_get_contents($_FILES[$image]['tmp_name']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filetype&nbsp; &nbsp; &nbsp;= wp_check_filetype(basename($upload['file']), null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $upload_dir&nbsp; &nbsp;= wp_upload_dir();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $upl_base_url = is_ssl() ? str_replace('http://', 'https://', $upload_dir['baseurl']) : $upload_dir['baseurl'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $base_name&nbsp; &nbsp; = basename($upload['file']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart_item['file_upload'][] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'guid'&nbsp; &nbsp; &nbsp; => $upl_base_url . '/' . _wp_relative_upload_path($upload['file']), // Url&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_type' => $filetype['type'], // File type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_name' => uniqid('logo_upload') . $base_name, // File name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title'&nbsp; &nbsp; &nbsp;=> ucfirst(preg_replace('/\.[^.]+$/', '', $base_name)), // Title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'position' => $image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart_item['unique_key'] = md5(microtime() . rand()); // Avoid merging items&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $cart_item;}add_filter('woocommerce_add_cart_item_data', 'add_custom_fields_data_as_custom_cart_item_data', 10, 3);// Display custom cart item data in cart&nbsp;add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2);function display_custom_item_data($cart_item_data, $cart_item){&nbsp; &nbsp; foreach ($cart_item['file_upload'] as $image) {&nbsp; &nbsp; &nbsp; &nbsp; if (isset($image['title'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart_item_data[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => __('Image uploaded', 'woocommerce'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' =>&nbsp; $image['title']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $cart_item_data;}// Save Image data as order item meta dataadd_action('woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4);function custom_field_update_order_item_meta($item, $cart_item_key, $values, $order){&nbsp; &nbsp; if (isset($values['file_upload'])) {&nbsp; &nbsp; &nbsp; &nbsp; $item->update_meta_data('_img_files',&nbsp; $values['file_upload']);&nbsp; &nbsp; }}// Admin orders: Display a linked button + the link of the image fileadd_action('woocommerce_after_order_itemmeta', 'backend_image_link_after_order_itemmeta', 10, 3);function backend_image_link_after_order_itemmeta($item_id, $item, $product){&nbsp; &nbsp; // Only in backend for order line items (avoiding errors)&nbsp; &nbsp; if (is_admin() && $item->is_type('line_item') && $files_data = $item->get_meta('_img_files')) {&nbsp; &nbsp; &nbsp; &nbsp; $imagesHtml = '';&nbsp; &nbsp; &nbsp; &nbsp; foreach ($files_data as $key => $image) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imagesHtml .= '<p><a href="' . $image['guid'] . '" target="_blank" class="button">' . __($image['position']) . '</a></p>'; // Optional&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo $imagesHtml;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}

RISEBY

我设法发现与我的模板(woodmart)冲突,代码在二十和店面上完美运行我应该早点想到这一点,而不是在一周内把头撞到墙上。
打开App,查看更多内容
随时随地看视频慕课网APP