首页 > 解决方案 > 如何在订单详情页面从购买产品的客户那里获得产品评论?

问题描述

首先,我解释我想要什么。我想在订单详细信息页面上从购买产品的客户那里获得产品评论。我从我当前的主题 > woocommerce > single-product-reviews.php 复制了代码。在我将代码粘贴到 order-details-item.php 页面上之后。

如果:

<?php if ( get_option( ‘woocommerce_review_rating_verification_required’ ) === ‘no’ || wc_customer_bought_product( ”, get_current_user_id(), $sub_product->get_id() ) ) : ?>

我编写了与 single-product-reviews.php 类似的代码,但 comment_form( apply_filters( ‘woocommerce_product_review_comment_form_args’, $comment_form ) );功能不起作用,因此表单没有出现在 order-details-item.php 上

我能做什么?

  <?php
/**
 * Order Item Details
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.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 https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates
 * @version 3.7.0
 */

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


global $wp;
global $wpdb;
$current_url = home_url(add_query_arg(array(), $wp->request));
$order_items = $order->get_items(apply_filters('woocommerce_purchase_order_item_types', 'line_item'));
$main_order_id = wp_get_post_parent_id($order->get_id());
// set the meta_key to the appropriate custom field meta_key1 and meta_key2
$meta_key1 = $order->get_id();

$allmiles = $wpdb->get_var(
    $wpdb->prepare(
        "
            SELECT item_qtys
            FROM $wpdb->dokan_refund
            WHERE order_id =%d
        ",
        $meta_key1

    )
);

if (empty($sub_orders)) {
    if ($main_order_id!=0){
        echo sprintf('
    <a id="button-go"
    href="' ."%s/hesabim/siparisi-goruntule/%d/". '"
    style="border-radius: 4px;border:1px solid #92cecc; background-color: white;color:#92cecc;margin-top:5px;margin-bottom:5px;"
    data-id="35375"
    class="woocommerce-Button button woocommerce-column__title">%s</a>',
            home_url(),wp_kses_post($main_order_id), 'Ana Siparişe Geri Dön');
    }
    foreach ($order_items as $item_id => $item) {
        if (!apply_filters('woocommerce_order_item_visible', true, $item)) {
            return;
        }
        $product_delivery_date = get_post_meta($item->get_product_id(), '_kargo_verilis_suresi', true);
        $product_id=$item->get_product_id();
        $sub_product=wc_get_product( $product_id );
        $product_meta = get_post_meta($item->get_product_id());
        $sub_refund_start = wc_get_order_item_meta($item_id, 'sub_status', true);
        // $sub_item_status=wc_add_order_item_meta( $item_id, 'item_status_custom', $order->get_status(), $unique = false )
        $tarih = explode('T', $order->get_date_completed());
        $now = date("Y-m-d");
        $origin = date_create($tarih[0]);
        $target = date_create($now);
        $interval = date_diff($origin, $target);
        $iade_hakki = $interval->d;
        $aide_yazi = '';
        $refund = $item_id;

        if (strpos($allmiles, strval($item_id)) > 0) {
            $sub_item_status = $wpdb->get_var(
                $wpdb->prepare(
                    "
            SELECT status
            FROM $wpdb->dokan_refund
            WHERE order_id =%d
        ",
                    $meta_key1
                )
            );
            switch ($sub_item_status) {
                case "0":
                    $sub_item_status = 'Alıcı Iadesi Bekleniyor';
                    break;
                case "1":
                    $sub_item_status = 'İade Onaylandı';
                    break;
                case "2" :
                    $sub_item_status = 'İade Onaylanmadı';
                    break;
            }
        } else {
            if (!empty($sub_refund_start)) {
                $sub_item_status = $sub_refund_start;
            } else {
                $sub_item_status = $order->get_status();
            }

        }
        ?>

        <?php

        ?>


        <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'woocommerce-table__line-item order_item', $item, $order)); ?>">
            <div class="customer-order-detail-custom">
                <div class="customer-order-detail-top-custom ">
                    <div class="left">
                        <div class="seller-custom-name">
                            <?php
                            $dizge = do_action('woocommerce_order_item_meta_start', $item_id, $item, $order, false);
                            //                echo esc_html( $dizge);
                            ?>
                        </div>

                        <div class="custom-sub-id">
                            Alt Sipariş No:
                            <span>
                                <?= wp_kses_post($order->get_id()) ?>
                            </span>
                        </div>

                        <div class="custom-sub-order-status">
                            Sipariş Durumu:
                            <span class="sub-order-status__<?= esc_attr__($order->get_status()) ?>">
                                <?= wp_kses_post(wc_get_order_status_name($order->get_status())) ?>
                            </span>
                        </div>


                    </div>
                    <div class="right">
                        <div class="custom-sub-order-total">
                            Tutar:
                            <span>
                                <?php
                                echo $order->get_formatted_line_subtotal($item);
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                ?>
                            </span>

                        </div>
                    </div>

                </div>
                <div class="customer-order-detail-bottom-custom">
                    <!--                    order image start-->
                    <div class="bottom-image-custom">
                        <?= wp_kses_post(wp_get_attachment_image($product_meta['_thumbnail_id'][0], 'full')) ?>
                    </div>
                    <!--                    order image end-->

                    <!--                    order desc start-->
                    <div class="bottom-desc-custom">
                        <div class="desc">
                            <?php
                            $is_visible = $product && $product->is_visible();
                            $product_permalink = apply_filters('woocommerce_order_item_permalink', $is_visible ? $product->get_permalink($item) : '', $item, $order);
                            echo apply_filters('woocommerce_order_item_name', $product_permalink ? sprintf('<a href="%s">%s</a>', $product_permalink, $item->get_name()) : $item->get_name(), $item, $is_visible); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped


                            ?>
                        </div>
                        <div class="sub-meta">
                            <div class="size">
                                Beden:
                                <span>Tek Ebat</span>
                            </div>
                            <div class="qty">
                                <?php
                                $qty = $item->get_quantity();
                                $refunded_qty = $order->get_qty_refunded_for_item($item_id);

                                if ($refunded_qty) {
                                    $qty_display = '<del>' . esc_html($qty) . '</del> <ins>' . esc_html($qty - ($refunded_qty * -1)) . '</ins>';
                                } else {
                                    $qty_display = esc_html($qty);
                                }

                                echo apply_filters('woocommerce_order_item_quantity_html', ' Adet: <strong class="product-quantity">' . sprintf('%s', $qty_display) . '</strong>', $item); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

                                ?>

                            </div>
                        </div>
                        <?php
                        // wc_display_item_meta($item); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                        do_action('woocommerce_order_item_meta_end', $item_id, $item, $order, false);
                        ?>

                    </div>
                    <!--order desc end-->
                    <!--order button start-->
                    <!--                    <div class="bottom-button-custom">-->
                    <!--                        --><?php
                    //                        global $wp;
                    //                        $current_url = home_url(add_query_arg(array(), $wp->request));
                    //                        if ($sub_item_status=="completed") {
                    //                            if ($iade_hakki>=14){
                    //                                echo sprintf('
                    //                        <button id="button-sub-iade"
                    //                        style="border-radius: 4px;border:2px solid #92cecc; background-color: white;color:#92cecc"
                    //                        data-id="'.esc_attr__("%d").'"
                    //                        class="woocommerce-Button button woocommerce-column__title">
                    //                        %s </button>',
                    //                        wp_kses_post($order->get_id()), esc_html('İade Et'));
                    //
                    //
                    //                            } else{  }
                    //                        }
                    //                        elseif ($sub_item_status=="shipped" ){
                    //                            echo sprintf('
                    //                        <button id="button-sub-iade"
                    //                        style="border-radius: 4px;border:2px solid #FF9D78; background-color: white;color:#FF9D78"
                    //                        data-id="'.esc_attr__("%d").'"
                    //                        data-name="'.esc_attr__("%s").'"
                    //                        data-item="'.esc_attr__("%d").'"
                    //                        data-redirect="'.esc_url("%s").'"
                    //                        class="woocommerce-Button button woocommerce-column__title sub-iade">
                    //                        %s </button>',
                    //                                wp_kses_post($order->get_id()),wp_kses_post($item->get_name()),wp_kses_post($item_id) ,wp_kses_post($current_url),esc_html('İade Et'));
                    //                        }
                    //
                    //                        elseif ($sub_item_status=='on-hold' | $sub_item_status=='processing' ){
                    //                            echo sprintf('
                    //                        <button id="button-sub-iptal"
                    //                        style="border-radius: 4px;border:1px solid #92cecc; background-color: white;color:#92cecc"
                    //                         data-id="'.esc_attr__("%d").'"
                    //                        data-name="'.esc_attr__("%s").'"
                    //                        data-item="'.esc_attr__("%d").'"
                    //                        data-redirect="'.esc_url("%s").'"
                    //                        class="woocommerce-Button button woocommerce-column__title sub-iptal">
                    //                        %s </button>',
                    //                                wp_kses_post($order->get_id()),wp_kses_post($item->get_name()),wp_kses_post($item_id) ,wp_kses_post($current_url),esc_html('İptal Et'));
                    //                         }else{ echo '';}?>
                    <!---->
                    <!---->
                    <!---->
                    <!--                    </div>-->
                    <!--order button end-->

                </div>
                <div class="customer-order-detail-bottom-custom mobile">
                    <div class="sub-order-total-text-mobile">
                        Tutar:
                    </div>
                    <div class="sub-order-total-digit-mobile">
                        <?php echo $order->get_formatted_line_subtotal($item); ?>
                    </div>

                </div>
                <?php if ($order->get_status() == 'refunded-customer' || $order->get_status() == 'shipped-refunded'): ?>
                    <div class="customer-refund-main">
                        <div class="refund-text">
                            <div class="custom-refund-text">
                                <?= esc_html_e('İade Talebi Oluşturuldu', 'yk-plugin') ?>
                                <p><?= esc_html_e('İade onaylandığında iade tutarı hesabınıza aktarılacaktır.', 'yk-plugin') ?></p>
                            </div>
                            <br>

                            <div class="refund-shipping-desc">
                                <?= _e('İade edeceğiniz ürünü <b>faturası ile birlikte</b> tek bir pakete koyunuz..', 'yk-plugin') ?>
                                <p><?= _e('Paketi iade kodunuzla birlikte <b>en geç 7 iş günü</b> içinde MNG Kargo’ya verin', 'yk-plugin') ?></p>
                            </div>


                        </div>
                        <div class="refund-shipped">
                            <div class="shipping-brand">
                                <div class="shipping-brand-text">
                                    Kargo Firması
                                </div>
                                <div class="shipping-brand-img">
                                    <img src="<?= esc_url('https://hstaging.yuvanikur.com/wp-content/plugins/yk-plugin-v1.3/inc/assets/svg/357048.svg') ?>"
                                         alt="">
                                </div>
                            </div>
                            <div class="shipping-brand">
                                <div class="shipping-brand-text">
                                    Kargo Iade Kodu
                                </div>
                                <div class="shipping-brand-code">
                                    1234567891234
                                </div>
                            </div>
                        </div>

                    </div>
                <?php endif; ?>

            </div>
            <div id="reviews" class="woocommerce-Reviews">
                <div id="comments">
                    <h2 class="woocommerce-Reviews-title">
                        <?php
                        $count = $sub_product->get_review_count();
                        if ( $count && wc_review_ratings_enabled() ) {
                            /* translators: 1: reviews count 2: product name */
                            $reviews_title = sprintf( esc_html( _n( '%1$s review for %2$s', '%1$s reviews for %2$s', $count, 'woocommerce' ) ), esc_html( $count ), '<span>' . $sub_product->get_title(). '</span>' );
                            echo apply_filters( 'woocommerce_reviews_title', $reviews_title, $count, $sub_product ); // WPCS: XSS ok.
                        } else {
                            esc_html_e( 'Reviews', 'woocommerce' );
                        }
                        ?>
                    </h2>

                    <?php if ( have_comments() ) : ?>
                        <ol class="commentlist">
                            <?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); ?>
                        </ol>

                        <?php
                        if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
                            echo '<nav class="woocommerce-pagination">';
                            paginate_comments_links(
                                apply_filters(
                                    'woocommerce_comment_pagination_args',
                                    array(
                                        'prev_text' => is_rtl() ? '&rarr;' : '&larr;',
                                        'next_text' => is_rtl() ? '&larr;' : '&rarr;',
                                        'type'      => 'list',
                                    )
                                )
                            );
                            echo '</nav>';
                        endif;
                        ?>
                    <?php else : ?>
                        <p class="woocommerce-noreviews"><?php esc_html_e( 'There are no reviews yet.', 'woocommerce' ); ?></p>
                    <?php endif; ?>
                </div>
                <?php if ( get_option( 'woocommerce_review_rating_verification_required' ) === 'no' || wc_customer_bought_product( '', get_current_user_id(), $sub_product->get_id() ) ) : ?>
                    <div id="review_form_wrapper">
                        <div id="review_form">
                            <?php
                            $commenter    = wp_get_current_commenter();
                            $comment_form = array(
                                /* translators: %s is product title */
                                'title_reply'         => have_comments() ? esc_html__( 'Add a review', 'woocommerce' ) : sprintf( esc_html__( 'Be the first to review &ldquo;%s&rdquo;', 'woocommerce' ), $sub_product->get_title() ),
                                /* translators: %s is product title */
                                'title_reply_to'      => esc_html__( 'Leave a Reply to %s', 'woocommerce' ),
                                'title_reply_before'  => '<span id="reply-title" class="comment-reply-title">',
                                'title_reply_after'   => '</span>',
                                'comment_notes_after' => '',
                                'label_submit'        => esc_html__( 'Submit', 'woocommerce' ),
                                'logged_in_as'        => '',
                                'comment_field'       => '',
                            );



                            $name_email_required = (bool) get_option( 'require_name_email', 1 );
                            $fields              = array(
                                'author' => array(
                                    'label'    => __( 'Name', 'woocommerce' ),
                                    'type'     => 'text',
                                    'value'    => $commenter['comment_author'],
                                    'required' => $name_email_required,
                                ),
                                'email'  => array(
                                    'label'    => __( 'Email', 'woocommerce' ),
                                    'type'     => 'email',
                                    'value'    => $commenter['comment_author_email'],
                                    'required' => $name_email_required,
                                ),
                            );

                            $comment_form['fields'] = array();

                            foreach ( $fields as $key => $field ) {
                                $field_html  = '<p class="comment-form-' . esc_attr( $key ) . '">';
                                $field_html .= '<label for="' . esc_attr( $key ) . '">' . esc_html( $field['label'] );

                                if ( $field['required'] ) {
                                    $field_html .= '&nbsp;<span class="required">*</span>';
                                }

                                $field_html .= '</label><input id="' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" type="' . esc_attr( $field['type'] ) . '" value="' . esc_attr( $field['value'] ) . '" size="30" ' . ( $field['required'] ? 'required' : '' ) . ' /></p>';

                                $comment_form['fields'][ $key ] = $field_html;
                            }


                            if ( wc_review_ratings_enabled() ) {
                                $comment_form['comment_field'] = '<div class="comment-form-rating"><label for="rating">' . esc_html__( 'Your rating', 'woocommerce' ) . ( wc_review_ratings_required() ? '&nbsp;<span class="required">*</span>' : '' ) . '</label><select name="rating" id="rating" required>
                        <option value="">' . esc_html__( 'Rate&hellip;', 'woocommerce' ) . '</option>
                        <option value="5">' . esc_html__( 'Perfect', 'woocommerce' ) . '</option>
                        <option value="4">' . esc_html__( 'Good', 'woocommerce' ) . '</option>
                        <option value="3">' . esc_html__( 'Average', 'woocommerce' ) . '</option>
                        <option value="2">' . esc_html__( 'Not that bad', 'woocommerce' ) . '</option>
                        <option value="1">' . esc_html__( 'Very poor', 'woocommerce' ) . '</option>
                    </select></div>';
                            }

                            $comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . esc_html__( 'Your review', 'woocommerce' ) . '&nbsp;<span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="8" required></textarea></p>';
                            comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
                            ?>
                        </div>
                    </div>
                <?php else : ?>
                    <p class="woocommerce-verification-required"><?php esc_html_e( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ); ?></p>
                <?php endif; ?>


                <div class="clear"></div>
            </div>

        </tr>

        <?php if ($show_purchase_note && $purchase_note) : ?>

            <tr class="woocommerce-table__product-purchase-note product-purchase-note">

                <td colspan="2"><?php echo wpautop(do_shortcode(wp_kses_post($purchase_note))); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>

            </tr>

        <?php endif; ?>

    <?php }
} ?>

标签: wordpresswoocommerce

解决方案


我找到了我的解决方案。我找到了我的解决方案。Woocommerce 不允许在帐户页面上审查产品。我的解决方案很简单。

comment_form( apply_filters( ‘woocommerce_product_review_comment_form_args’, $comment_form ),$product_id ); 

我给函数提供了产品 ID,因为 woocommerce 允许在单个产品页面上进行审查。这是一个简单的技巧。


推荐阅读