如何在woocommerce订单商品详情模板上显示自定义字段?

时间:2016-04-30 06:17:57

标签: php loops woocommerce

我在尝试显示我在woocommerce订单商品详情模板表格中创建的字段时遇到了一些麻烦,而且我对PHP不是很了解。我创建了一个名为sessions的字段,并将其注册为产品发布类型。

enter image description here

enter image description here

用户购买产品后,我也希望显示自定义字段(会话)。

这是woocommerce视图订单的模板。

<?php
/**
 * Order Item Details
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.php.
 *
 */

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

if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
    return;
}
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
    <td class="product-name">

        <?php
            $is_visible = $product && $product->is_visible();

            echo apply_filters( 'woocommerce_order_item_name', $is_visible ? sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ) : $item['name'], $item, $is_visible );
            echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '&times; %s', $item['qty'] ) . '</strong>', $item );

            do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );

            $order->display_item_meta( $item );
            $order->display_item_downloads( $item );

            do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
        ?>
    </td>
    <td class="product-total">
        <?php echo $order->get_formatted_line_subtotal( $item ); ?>
    </td>
</tr>
<?php if ( $show_purchase_note && $purchase_note ) : ?>
<tr class="product-purchase-note">
    <td colspan="3"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>

我不知道或不知道在哪里插入the_field(sessions)以在结帐时显示

1 个答案:

答案 0 :(得分:0)

尝试这种方法,看看你得到了什么:

    <?php
        /**
         * Order Item Details
         *
         * This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.php.
         *
         */

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

        if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
            return;
        }
        $sessions   = "sessions";       //DEFINE THE SESSIONS STRING...
    ?>
    <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
        <td class="product-name">

            <?php
                $is_visible = $product && $product->is_visible();

                echo apply_filters( 'woocommerce_order_item_name', $is_visible ? sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ) : $item['name'], $item, $is_visible );
                echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '&times; %s', $item['qty'] ) . '</strong>', $item );

                do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );

                $order->display_item_meta( $item );
                $order->display_item_downloads( $item );

                do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );

                //PERHAPS RIGHT HERE ADD THE SESSION
                // IT IS IMPORTANT TO PASS IN THE PRODUCT-ID: $item['product_id'] SO THAT THE CORRECT FIELD IS RETRIEVED.
                echo "<p class='session'><strong>" .__("Session: ", "ttd") . "</strong><em class='session-value'>" . get_field($sessions, $item['product_id']) . "</em></p>";
                //OR THIS WAY
                echo "<p class='session'><strong>" .__("Session: ", "ttd") . "</strong><em class='session-value'>" . get_post_meta($item['product_id'], $sessions, true) . "</em></p>";
            ?>
        </td>
        <td class="product-total">
            <?php echo $order->get_formatted_line_subtotal( $item ); ?>
        </td>
    </tr>
    <?php if ( $show_purchase_note && $purchase_note ) : ?>
        <tr class="product-purchase-note">
            <td colspan="3"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
        </tr>
    <?php endif; ?>

或者,您也可以这样做:

    <?php
        /**
         * Order Item Details
         *
         * This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.php.
         *
         */

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

        if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
            return;
        }
        $sessions   = "sessions";       //DEFINE THE SESSIONS STRING...
    ?>
    <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
        <td class="product-name">

            <?php
                $is_visible = $product && $product->is_visible();

                echo apply_filters( 'woocommerce_order_item_name', $is_visible ? sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ) : $item['name'], $item, $is_visible );
                echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '&times; %s', $item['qty'] ) . '</strong>', $item );

                do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );

                $order->display_item_meta( $item );
                $order->display_item_downloads( $item );

                do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
            ?>
        </td>
        <td class="product-session">
            <!-- ALTERNATIVELY ADD THE SESSION RIGHT HERE ON A UNIQUE COLUMN-->
            <!-- IT IS IMPORTANT TO PASS IN THE PRODUCT-ID: $item['product_id'] SO THAT THE CORRECT FIELD IS RETRIEVED.-->
            <p class='session'>
                <strong><?php echo __("Session: ", "ttd"); ?></strong>
                <em class='session-value'><?php the_field($sessions, $item['product_id']); ?></em>
            </p>
            <!-- OR THIS WAY -->
            <p class='session'>
                <strong><?php echo __("Session: ", "ttd"); ?></strong>
                <em class='session-value'><?php echo get_post_meta($item['product_id'], $sessions, true); ?></em>
            </p>
        </td>
        <td class="product-total">
            <?php echo $order->get_formatted_line_subtotal( $item ); ?>
        </td>
    </tr>
    <?php if ( $show_purchase_note && $purchase_note ) : ?>
        <tr class="product-purchase-note">
            <td colspan="3"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
        </tr>
    <?php endif; ?>

但是,找到文件/templates/order/order-details.php。选择第1行到第35行并将下面的代码粘贴到其上:

<?php
/**
 * Order details
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/order/order-details.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
 * will need to copy the new files to your theme to maintain compatibility. We try to do this.
 * as little as possible, but it does happen. When this occurs the version of the template file will.
 * be bumped and the readme will list any important changes.
 *
 * @see         http://docs.woothemes.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.5.3
 */

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

$order = wc_get_order( $order_id );

$show_purchase_note    = $order->has_status( apply_filters( 'woocommerce_purchase_note_order_statuses', array( 'completed', 'processing' ) ) );
$show_customer_details = is_user_logged_in() && $order->get_user_id() === get_current_user_id();
?>
<h2><?php _e( 'Order Details', 'woocommerce' ); ?></h2>
<table class="shop_table order_details">
    <thead>
    <tr>
        <th class="product-name"><?php _e( 'Product', 'woocommerce' ); ?></th>
        <th class="product-session"><?php _e( 'Session', 'your_ttd' ); ?></th> <!-- HERE WE ARE ADDING A COLUMN TO THE HEAD AS WELL TO MATCH...-->
        <th class="product-total"><?php _e( 'Total', 'woocommerce' ); ?></th>
    </tr>
    </thead>
    <tbody>

希望这会有所帮助......