1

I know there is another question similar, but my problem look strange. I am running a PHP script, and keep getting errors like:

Notice: Undefined variable: product_id in D:\ MY-WEBSERVER\InstantWP_4.3.1\iwpserver\htdocs\wordpress\wp-content\themes\MYTHEME\functions.php on line 580

Line 580 looks like this:

 if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) echo '<div class="user-bought">&hearts; Hey ' . $current_user->first_name . ', you\'ve purchased this in the past. Buy again?</div>';

Here full code:

 add_action ( 'woocommerce_after_shop_loop_item', 
 'user_logged_in_product_already_bought', 30);

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

Is there a quick fix to resolve these notify? Really appreciate for any help

Thank you

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ryana milisku
  • 27
  • 1
  • 6
  • 1
    No, nothing is strange, standard coding error, `$product_id` isn't defined in the scope of function `user_logged_in_product_already_bought()`. – Scuzzy Nov 26 '17 at 23:58
  • I see you've tried to copy and paste code from https://businessbloomer.com/woocommerce-check-current-user-already-purchased-product/ but the product_id is malformed. use `$product->id` instead – Scuzzy Nov 27 '17 at 00:03
  • Scuzzy : yes, I am using code from them. Old code before Woocommerce updated. – ryana milisku Nov 27 '17 at 00:05
  • 1
    Well change `$product_id` to `$product->id` – Scuzzy Nov 27 '17 at 00:06
  • Scuzzy : that is old code, call id directly is not allowed on newest WooCommerce. – ryana milisku Nov 27 '17 at 00:08
  • 2
    I have no idea what the problem is, all I'm telling you is `$product_id` is not defined and is the direct reason you are seeing the `Undefined variable: product_id` error message. – Scuzzy Nov 27 '17 at 00:15

1 Answers1

2

The correct way to get this function working in WooCommerce 3+ is:

 add_action ( 'woocommerce_after_shop_loop_item',  'user_logged_in_product_already_bought', 30 );
 function user_logged_in_product_already_bought() {
    if ( ! is_user_logged_in() ) return;

    global $product;

    $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>';
 }

(missing $product->get_id() on line 580 instead of undefined $product_id)

OR using global $post; it should be:

 add_action ( 'woocommerce_after_shop_loop_item',  'user_logged_in_product_already_bought', 30 );
 function user_logged_in_product_already_bought() {
    if ( ! is_user_logged_in() ) return;

    global $post;

    $current_user = wp_get_current_user();

    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $post->ID ) ) 
        echo '<div class="user-bought">&hearts; Hey ' . $current_user->first_name . ', you\'ve purchased this in the past. Buy again?</div>';
 }

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • LoicTheAztec : it's fix the notify, but I can not see "you\'ve purchased this in the past. Buy again?" on the shop page, single product or anywhere. Am I missing something? I am using Twenty Fifteen theme to test. – ryana milisku Nov 27 '17 at 00:51
  • @ryanamilisku This code works for a defined customer (logged in)… Message will appear in shop and archives pages (not single product pages), under the add to cart button, only when the product has already been bought… In my test server, the code is perfectly working… Now your actual question was related to the error, that is solved… – LoicTheAztec Nov 27 '17 at 02:03