【问题标题】:How to display custom field on woocommerce order item details template?如何在 woocommerce 订单商品详细信息模板上显示自定义字段?
【发布时间】:2016-04-30 15:27:17
【问题描述】:

我在尝试显示我在 woocommerce 的订单项目详细信息模板的表格中创建的字段时遇到了一些麻烦,而且我对 PHP 的了解并不多。我创建了一个名为会话的字段并将其注册为产品帖子类型。

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

这里是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) 以在结帐时显示它

【问题讨论】:

  • 编辑模板文件wp-content\themes\--themename--\woocommerce\checkout\form-checkout.php 并在此处回显该字段。你在用acf吗?
  • 你可以使用get_field$field= get_field( "sessions", idofproduct);
  • 但是如何在结帐表单中获取产品自定义字段,因为产品可以是多个。
  • 这是要知道的。尝试研究如何在结帐时显示包含多个产品的字段
  • 您可以通过两种方式做到这一点。一方面;您可以将其显示在产品名称下方(有点丑)。以另一种方式;您可以在其自己的列中显示会话,但您也必须编辑另一个文件。两种解决方案都在下面发布。试试看有没有适合你的...

标签: php loops woocommerce


【解决方案1】:

试试这个方法,看看你会得到什么:

    <?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>

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多