如果用户之前购买过产品,则在 WooCommerce 购物车页面的产品名称下方显示消息

Display message below product name on WooCommerce cart page if user has bought product before

如果登录用户之前已经购买了产品,我想在购物车页面上显示通知。

在结帐页面上我设法显示了它 通过在第 31 行

周围添加此 checkout/review-order.php

if( is_user_logged_in() ) {
    $user = wp_get_current_user();
}

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

    if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
        ?>
        <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
            <td class="product-name">
                <?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                <?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '&times;&nbsp;%s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                <?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                <?php
                    // Here is your code -- Start
                    if( is_user_logged_in() && wc_customer_bought_product( $user->user_email, $user->ID, $_product->get_id() ) ) {
                        echo apply_filters( 'woocommerce_checkout_cart_alredy_bought', '<div class="user-bought">' . sprintf( "Hi %s you already purchased in the past.", $user->first_name ) . '</div>' );
                    }
                    // Here is your code -- End
                ?>
            </td>
            <td class="product-total">
                <?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
            </td>
        </tr>
        <?php
    }
}

对于 cart/cart.php 中的购物车页面,我需要在第几行添加该代码?

您可以使用 woocommerce_after_cart_item_name 操作挂钩

而不是覆盖模板文件

所以你得到:

function action_woocommerce_after_cart_item_name( $cart_item, $cart_item_key ) {
    // Only for logged-in users
    if ( ! is_user_logged_in() ) return;
    
    // Get current user
    $user = wp_get_current_user();
    
    // Get product ID
    $product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
    
    // If true
    if ( wc_customer_bought_product( $user->user_email, $user->ID, $product_id ) ) {        
        echo '<p class="my-class">' . sprintf( __( 'Hi %s you already purchased this product in the past.', 'woocommerce' ), $user->first_name ) . '</p>'; 
    }
}
add_action( 'woocommerce_after_cart_item_name', 'action_woocommerce_after_cart_item_name', 10, 2 );