marwah
marwah

Reputation: 81

Check on the checkout page,if Logged In User Has Already Purchased a Product

can someone help me, i found this code and tried it and it works on the shop page

add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_user_logged_in_product_already_bought', 30 );
  
function bbloomer_user_logged_in_product_already_bought() {
global $product;
if ( ! is_user_logged_in() ) return;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) echo '<div class="user-bought">&hearts; Hey ' . $current_user->first_name . ', you\'ve purchased this in the past. Buy again?</div>';
}

can someone over there change this code for notification only on the checkout page, before the customer continues the payment. I want to show notifications only on the checkout page, not on any other page Thank you very much

Upvotes: 0

Views: 1186

Answers (1)

HPTheWebNuts
HPTheWebNuts

Reputation: 81

I presume that your cart (review order part) on checkout page have many products and you want to show Already Purchased text somewhere near product name.

You can do this by overriding template. copy file from wp-plugins/woocommerce/templates/checkout/review-order.php into your active theme (preferably child theme) here you can see how to create child theme (https://developer.wordpress.org/themes/advanced-topics/child-themes/). Now create woocommerce folder in your active theme if not exist, then create checkout folder if not exist and paste that file. Open that file and you can see the Cart loop around line no 31.

add code before that loop as follow:

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

Now, update loop code bit as follow:

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

This should be working for you.

Upvotes: 1

Related Questions