pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在最近的訂單模板和管理訂單(woocommerce)上顯示產(chǎn)

Display product post type advanced custom field on recent orders template and admin orders (woocommerce)(在最近的訂單模板和管理訂單(woocommerce)上顯示產(chǎn)品帖子類型高級(jí)自定義字段) - IT屋-程序員軟件開發(fā)技術(shù)分
本文介紹了在最近的訂單模板和管理訂單(woocommerce)上顯示產(chǎn)品帖子類型高級(jí)自定義字段的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我正在嘗試顯示我在 WooCommerce 最近的訂單模板中創(chuàng)建的字段,但我對(duì) PHP 不是很了解.

I’m trying to display the fields I have created in the recent order template of WooCommerce and I’m not very knowledgeable in PHP.

我創(chuàng)建了一個(gè)名為 session 的字段并注冊(cè)為產(chǎn)品帖子類型.用戶購(gòu)買產(chǎn)品后,我希望在我的帳戶">最近的訂單"(模板)中顯示海關(guān)字段 會(huì)話" 值.

I have created a field called sessions and registered as a product post type. Once a user purchase a product I want thats customs fields "sessions" values to be displayed in the My account > Recent orders (template).

我試圖尋找答案和解決方案,但似乎卡住了.

I tried looking for answers and solutions and I seem to be stuck.

這里是我一直在做的 my-order.php 模板的自定義代碼.我已經(jīng)對(duì)其進(jìn)行了數(shù)天的黑客攻擊,但似乎無法在我最近的訂單表中顯示此值.

Here is the customized code of my-order.php template that I have been working on. I have been hacking it for days and can't seem to display this values in my recent orders table.

更新 - 添加圖像和更多描述以澄清我的問題

1.) 正如您在此處看到的,我創(chuàng)建了兩種類型的字段并將它們注冊(cè)為產(chǎn)品帖子類型

1.) As you can see here I created two types of fields and registered them as product post type

2.) 然后我在我創(chuàng)建的這兩個(gè)字段上放置了一個(gè)值

2.) Then I placed a value on those two fields I have created

3.) 一旦用戶或客戶購(gòu)買了物品/產(chǎn)品/包裹.我希望這兩個(gè)值顯示在用戶 my-account 模板的最近訂單中 "Sessions"

3.) Once a user or customer purchase the item/product/package. I want those two values to be shown on the recent orders of the user my-account template under the column "Sessions"

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$my_orders_columns = apply_filters( 'woocommerce_my_account_my_orders_columns', array(
    'order-number'   => __( 'Package', 'woocommerce' ),
    'sessions'       => __( 'Session', 'woocommerce' ),
    'order-total'    => __( 'Package Prize', 'woocommerce' ),
    'order-date'     => __( 'Date', 'woocommerce' ),
    'order-end-date' => __( 'End Date', 'woocommerce'),
    'order-status'   => __( 'Status', 'woocommerce' ),
    'order-actions'  => ' ',
) );

$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
    'numberposts' => $order_count,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types( 'view-orders' ),
    'post_status' => array_keys( wc_get_order_statuses() )
) ) );

if ( $customer_orders ) : ?>

    <h2><?php // echo apply_filters( 'woocommerce_my_account_my_orders_title', __( 'Recent Orders', 'woocommerce' ) ); ?></h2>

    <table class="shop_table shop_table_responsive my_account_orders">

        <thead>
            <tr>
                <?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
                    <th class="<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
                <?php endforeach; ?>
            </tr>
        </thead>

        <tbody>
            <?php foreach ( $customer_orders as $customer_order ) :
                $order      = wc_get_order( $customer_order );
                $item_count = $order->get_item_count();
                ?>
                <tr class="order">
                    <?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
                        <td class="<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
                            <?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
                                <?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>

                            <?php elseif ( 'order-number' === $column_id ) : ?>
                                <?php foreach($order->get_items() as $item) {
                                    $product_name = $item['name'];

                                } ?>

                                <?php echo $product_name;?>

                            <?php elseif ( 'session'  === $column_id ) : ?>
                                <?php if (get_field('session_period', $product->id) ) :  ?>



                            <?php endif; ?>


                            <?php elseif ( 'order-total' === $column_id ) : ?>
                                <?php echo sprintf( _n( '%s', '%s', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?>

                            <?php elseif ( 'order-date' === $column_id ) : ?>
                                <time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>

                            <?php /* Order End Date */ ?>
                            <?php elseif ( 'order-end-date' === $column_id ) : ?>
                                <?php if (get_field('date_ended', $order->id) ) :    ?>
                                <p class="sendungsnummer"><?php the_field('date_ended', $order->id); ?>

                            <?php endif; ?>

                            <?php elseif ( 'order-status' === $column_id ) : ?>
                                <?php echo wc_get_order_status_name( $order->get_status() ); ?>


                            <?php elseif ( 'order-actions' === $column_id ) : ?>
                                <?php
                                    $actions = array(
                                        'pay'    => array(
                                            'url'  => $order->get_checkout_payment_url(),
                                            'name' => __( 'Pay', 'woocommerce' )
                                        ),
                                        'view'   => array(
                                            'url'  => $order->get_view_order_url(),
                                            'name' => __( 'View', 'woocommerce' )
                                        ),
                                        'cancel' => array(
                                            'url'  => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
                                            'name' => __( 'Cancel', 'woocommerce' )
                                        )
                                    );

                                    if ( ! $order->needs_payment() ) {
                                        unset( $actions['pay'] );
                                    }

                                    if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
                                        unset( $actions['cancel'] );
                                    }

                                    /* -------- View Button --------
                                    if ( $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ) ) {
                                        foreach ( $actions as $key => $action ) {
                                            echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
                                        }
                                    }
                                    */
                                ?>
                            <?php endif; ?>
                        </td>
                    <?php endforeach; ?>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

推薦答案

這里我們采用了一種與使用 ACF 插件制作的自定義字段不同的方法.我們創(chuàng)建了一個(gè)專用的元框,里面有 2 個(gè)字段,位于后端產(chǎn)品頁面的右側(cè)列,使用以下代碼:

Here we are going on a different approach than custom fields made with ACF plugin. We create a dedicated metabox with 2 fields inside it, located on the right side column in backend product pages, with this code:

//
//Adding Meta container admin product pages
//
add_action( 'add_meta_boxes', 'cc_add_meta_boxes' );
if ( ! function_exists( 'cc_add_meta_boxes' ) )
{
    function cc_add_meta_boxes()
    {
        global $woocommerce, $post;

        add_meta_box( 'cc_other_fields', __('Sessions','woocommerce'), 'cc_add_other_fields_for_packaging', 'product', 'side', 'core' );
    }
}

//
//adding Meta field in the meta container admin product pages
//
if ( ! function_exists( 'cc_save_wc_order_other_fields' ) )
{
    function cc_add_other_fields_for_packaging()
    {
        global $woocommerce, $product, $post;

        $meta_field_session_period = get_post_meta( $post->ID, '_session_period', true ) ? get_post_meta( $post->ID, '_session_period', true ) : '';

        $meta_field_number_sessions = get_post_meta( $post->ID, '_number_sessions', true ) ? get_post_meta( $post->ID, '_number_sessions', true ) : '';

        echo '<input type="hidden" name="cc_other_meta_field_nonce" value="' . wp_create_nonce() . '">
    <p><label style="display:inline-block;" class="cc_opt_label">' .   __( "Session period", "your_theme_slug" ) . '</label><br>
        <input type="text" style="width:250px;";" name="session_period" placeholder="' . $meta_field_session_period . '" value="' . $meta_field_session_period . '"></p>
    <p><label style="display:inline-block;" class="cc_opt_label">' .   __( "Number of sessions", "your_theme_slug" ) . '</label><br>
        <input type="text" style="width:250px;";" name="number_sessions" placeholder="' . $meta_field_number_sessions . '" value="' . $meta_field_number_sessions . '"><br></p>';

    }
}

//Save the data of the product Meta fields pages
add_action( 'save_post', 'cc_save_product_other_fields', 10, 1 );
if ( ! function_exists( 'cc_save_product_other_fields' ) )
{

    function cc_save_product_other_fields( $post_id ) {

        // We need to verify this with the proper authorization (security stuff).

        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'cc_other_meta_field_nonce' ] ) ) {
            return $post_id;
        }
        $nonce = $_REQUEST[ 'cc_other_meta_field_nonce' ];

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce ) ) {
            return $post_id;
        }

        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        // Check the user's permissions.
        if ( 'page' == $_POST[ 'post_type' ] ) {

            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {

            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }
        // --- Its safe for us to save the data ! --- //

        // Sanitize user input  and update the meta field in the database.
        update_post_meta( $post_id, '_session_period', $_POST[ 'session_period' ] );
        update_post_meta( $post_id, '_number_sessions', $_POST[ 'number_sessions' ] );
    }
}

注意:您將此代碼粘貼到活動(dòng)子主題或主題的function.php文件中.

Note: you will paste this code in the function.php file of active child theme or theme.

如何在模板代碼中使用它 + 更正的錯(cuò)誤:

對(duì)于那些簡(jiǎn)單的字段,您不需要使用 ACF 插件.您還會(huì)發(fā)現(xiàn)缺少的代碼來檢索訂單 ID、產(chǎn)品 ID 并顯示在我的帳戶 > 最近的訂單表、會(huì)話數(shù)據(jù):

You will not need to use ACF plugin for those simple fields. You will find also the missing code to retrieve order ID, Product ID and to display on My account > recent orders table, the sessions data:

// This is your existing code:
<?php 

<?php foreach ( $customer_orders as $customer_order ) :
                $order      = wc_get_order( $customer_order );

                // This way you can retrieve order ID:
                $order_id   = $order->post->ID;

您需要的:

if (get_field('date_ended', $order_id) ) : 
// or
if (get_field('date_ended', $order->post->id) ) :
// instead of: 
//get_field('date_ended', $order->id)

// and use </p> instead of <p> at the end of this line:
<p class="sendungsnummer"><?php the_field('date_ended', $order->id); ?></p>

.../...

foreach($order->get_items() as $item) {
$product_name = $item['name'];

// This way you can retrieve product ID:
$product_id = $item['product_id'];

.../...

您將使用產(chǎn)品 ID(或帖子 ID)和模板中的 Wordpress 函數(shù)get_post_meta() 訪問此數(shù)據(jù):

You will access this data with product ID (or post ID) and the Wordpress function get_post_meta() in your template:

// Then you get your data fields with this two:
$session_period = get_post_meta( $product_id, '_session_period', true );
$number_sessions = get_post_meta( $product_id, '_number_sessions', true ); 

您將使用它們,以這種方式顯示它們的值:

And you will use them, displaying their values this way:

echo $session_period; // For Session period
echo $number_sessions; // For Number of sessions

.../...

elseif ( 'sessions'  === $column_id ) // <=== It is 'sessions' instead of 'session' !!!

這種方法更專業(yè)、更干凈:

This approach is more professional and cleaner:

這是基于不同的問題,但有些類似:
WooCommerce:將自定義 Metabox 添加到管理訂單頁面

This is based on a different problem but some kind of similar:
WooCommerce : Add custom Metabox to admin order page

這篇關(guān)于在最近的訂單模板和管理訂單(woocommerce)上顯示產(chǎn)品帖子類型高級(jí)自定義字段的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 昆明化妆培训-纹绣美甲-美容美牙培训-昆明博澜培训学校 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | NBA直播_NBA直播免费观看直播在线_NBA直播免费高清无插件在线观看-24直播网 | 自清洗过滤器-全自动自清洗过反冲洗过滤器 - 中乂(北京)科技有限公司 | 首页|光催化反应器_平行反应仪_光化学反应仪-北京普林塞斯科技有限公司 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 立式壁挂广告机厂家-红外电容触摸一体机价格-华邦瀛 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 微水泥_硅藻泥_艺术涂料_艺术漆_艺术漆加盟-青岛泥之韵环保壁材 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 南昌旅行社_南昌国际旅行社_南昌国旅在线 | 防潮防水通风密闭门源头实力厂家 - 北京酷思帝克门窗 | 蒸压釜-陶粒板隔墙板蒸压釜-山东鑫泰鑫智能装备有限公司 | 心肺复苏模拟人|医学模型|急救护理模型|医学教学模型上海康人医学仪器设备有限公司 | 超声波清洗机_超声波清洗机设备_超声波清洗机厂家_鼎泰恒胜 | 北京印刷厂_北京印刷_北京印刷公司_北京印刷厂家_北京东爵盛世印刷有限公司 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 宝宝药浴-产后药浴-药浴加盟-艾裕-专注母婴调养泡浴 | 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 上海三信|ph计|酸度计|电导率仪-艾科仪器 | 蓝鹏测控平台 - 智慧车间系统 - 车间生产数据采集与分析系统 | 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | [品牌官网]贵州遵义双宁口腔连锁_贵州遵义牙科医院哪家好_种植牙_牙齿矫正_原华美口腔 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 定时排水阀/排气阀-仪表三通旋塞阀-直角式脉冲电磁阀-永嘉良科阀门有限公司 | 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 - 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 | 阴离子_阳离子聚丙烯酰胺厂家_聚合氯化铝价格_水处理絮凝剂_巩义市江源净水材料有限公司 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | EPK超声波测厚仪,德国EPK测厚仪维修-上海树信仪器仪表有限公司 | 展厅设计-展馆设计-专业企业展厅展馆设计公司-昆明华文创意 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 |