第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為 WooCommerce 單品添加圖像文件上傳字段

為 WooCommerce 單品添加圖像文件上傳字段

PHP
湖上湖 2023-07-21 18:23:25
我創(chuàng)建了一個(gè)自定義插件,它可以在 woocommerce 單一產(chǎn)品頁面中動(dòng)態(tài)顯示自定義字段。顯示字段,將其添加到購(gòu)物車并添加到訂單數(shù)據(jù)和電子郵件。然而,我花了幾天時(shí)間嘗試添加文件上傳字段,但沒有成功。該字段在前端顯示如下: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}然后添加到購(gòu)物車,例如: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;}當(dāng)然,這只是代碼的一部分。其余字段工作完美。是的,如果有人想知道的話,我只嘗試過代碼本身。我已經(jīng)“玩”了好幾天了,但我不知道出了什么問題。非常感謝任何幫助:)
查看完整描述

3 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

您可以嘗試以下操作,將上傳的圖像數(shù)據(jù)存儲(chǔ)為自定義購(gòu)物車項(xiàng)目數(shù)據(jù)并將其另存為自定義訂單項(xiàng)目元數(shù)據(jù):


// Display additional product fields (+ jQuery code)

add_action( 'woocommerce_before_add_to_cart_button', 'display_additional_product_fields', 9 );

function display_additional_product_fields(){

    ?>

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

        <label for="file_field"><?php echo __("Upload Image") . ': '; ?>

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

        </label>

    </p>

    <?php

}



// Add custom fields data as the cart item custom data

add_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 ){

    if( isset($_FILES['image']) && ! empty($_FILES['image']) ) {

        $upload       = wp_upload_bits( $_FILES['image']['name'], null, file_get_contents( $_FILES['image']['tmp_name'] ) );

        $filetype     = wp_check_filetype( basename( $upload['file'] ), null );

        $upload_dir   = wp_upload_dir();

        $upl_base_url = is_ssl() ? str_replace('http://', 'https://', $upload_dir['baseurl']) : $upload_dir['baseurl'];

        $base_name    = basename( $upload['file'] );


        $cart_item['file_upload'] = array(

            'guid'      => $upl_base_url .'/'. _wp_relative_upload_path( $upload['file'] ), // Url

            'file_type' => $filetype['type'], // File type

            'file_name' => $base_name, // File name

            'title'     => ucfirst( preg_replace('/\.[^.]+$/', '', $base_name ) ), // Title

        );

        $cart_item['unique_key'] = md5( microtime().rand() ); // Avoid merging items

    }

    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 ) {

    if ( isset( $cart_item['file_upload']['title'] ) ){

        $cart_item_data[] = array(

            'name' => __( 'Image uploaded', 'woocommerce' ),

            'value' =>  str_pad($cart_item['file_upload']['title'], 16, 'X', STR_PAD_LEFT) . '…',

        );

    }

    return $cart_item_data;

}


// Save Image data as order item meta data

add_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 ) {

    if ( isset( $values['file_upload'] ) ){

        $item->update_meta_data( '_img_file',  $values['file_upload'] );

    }

}


// Admin orders: Display a linked button + the link of the image file

add_action( 'woocommerce_after_order_itemmeta', 'backend_image_link_after_order_itemmeta', 10, 3 );

function backend_image_link_after_order_itemmeta( $item_id, $item, $product ) {

    // Only in backend for order line items (avoiding errors)

    if( is_admin() && $item->is_type('line_item') && $file_data = $item->get_meta( '_img_file' ) ){

        echo '<p><a href="'.$file_data['guid'].'" target="_blank" class="button">'.__("Open Image") . '</a></p>'; // Optional

        echo '<p><code>'.$file_data['guid'].'</code></p>'; // Optional

    }

}


// Admin new order email: Display a linked button + the link of the image file

add_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 ){

    // On "new order" email notifications

    if ( 'new_order' === $email->id ) {

        foreach ($order->get_items() as $item ) {

            if ( $file_data = $item->get_meta( '_img_file' ) ) {

                echo '<p>

                    <a href="'.$file_data['guid'].'" target="_blank" class="button">'.__("Download Image") . '</a><br>

                    <pre><code style="font-size:12px; background-color:#eee; padding:5px;">'.$file_data['guid'].'</code></pre>

                </p><br>';

            }

        }

    }

}

此代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。


在 Woocommerce 版本 4.3.x 中進(jìn)行了測(cè)試,并使用所有類型的默認(rèn) WooCommerce 產(chǎn)品。


查看完整回答
反對(duì) 回復(fù) 2023-07-21
?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

您可以使用 Product Addons 等插件來完成此操作

https://woocommerce.com/products/product-add-ons/

這是文檔的鏈接,專門解釋了允許文件上傳(它們可以是免費(fèi)的,也可以按上傳收費(fèi))

https://docs.woocommerce.com/document/product-add-ons/#section-9

希望這可以幫助!


查看完整回答
反對(duì) 回復(fù) 2023-07-21
?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

如果有人需要的話,可以獲取多個(gè)圖像。


function add_custom_fields_data_as_custom_cart_item_data($cart_item)

{

    $images = ['front-image', 'back-image', 'sleeve-image'];


    foreach ($images as $image) {

        if (isset($_FILES[$image]) && $_FILES[$image]['error'] === 0) {


            $upload       = wp_upload_bits($_FILES[$image]['name'], null, file_get_contents($_FILES[$image]['tmp_name']));

            $filetype     = wp_check_filetype(basename($upload['file']), null);

            $upload_dir   = wp_upload_dir();

            $upl_base_url = is_ssl() ? str_replace('http://', 'https://', $upload_dir['baseurl']) : $upload_dir['baseurl'];

            $base_name    = basename($upload['file']);


            $cart_item['file_upload'][] = array(

                'guid'      => $upl_base_url . '/' . _wp_relative_upload_path($upload['file']), // Url

                'file_type' => $filetype['type'], // File type

                'file_name' => uniqid('logo_upload') . $base_name, // File name

                'title'     => ucfirst(preg_replace('/\.[^.]+$/', '', $base_name)), // Title

                'position' => $image

            );

            $cart_item['unique_key'] = md5(microtime() . rand()); // Avoid merging items

        }

    }

    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 

add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2);

function display_custom_item_data($cart_item_data, $cart_item)

{


    foreach ($cart_item['file_upload'] as $image) {

        if (isset($image['title'])) {

            $cart_item_data[] = array(

                'name' => __('Image uploaded', 'woocommerce'),

                'value' =>  $image['title']

            );

        }

    }

    return $cart_item_data;

}


// Save Image data as order item meta data

add_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)

{

    if (isset($values['file_upload'])) {

        $item->update_meta_data('_img_files',  $values['file_upload']);

    }

}


// Admin orders: Display a linked button + the link of the image file

add_action('woocommerce_after_order_itemmeta', 'backend_image_link_after_order_itemmeta', 10, 3);

function backend_image_link_after_order_itemmeta($item_id, $item, $product)

{

    // Only in backend for order line items (avoiding errors)

    if (is_admin() && $item->is_type('line_item') && $files_data = $item->get_meta('_img_files')) {


        $imagesHtml = '';


        foreach ($files_data as $key => $image) {

            $imagesHtml .= '<p><a href="' . $image['guid'] . '" target="_blank" class="button">' . __($image['position']) . '</a></p>'; // Optional

        }


        echo $imagesHtml;

        

    }

}



查看完整回答
反對(duì) 回復(fù) 2023-07-21
?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

我設(shè)法發(fā)現(xiàn)與我的模板(woodmart)沖突,代碼在二十和店面上完美運(yùn)行

我應(yīng)該早點(diǎn)想到這一點(diǎn),而不是在一周內(nèi)把頭撞到墻上。


查看完整回答
反對(duì) 回復(fù) 2023-07-21
  • 3 回答
  • 0 關(guān)注
  • 210 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)