2017-11-26 9 views
1

私は同じような別の質問があることを知っていますが、私の問題は奇妙に見えます。私は、PHPスクリプトを実行している、とのようなエラーを得続けるのです:

注意:未定義の変数を:PRODUCT_IDをDに:\ MY-WEBSERVER \ InstantWP_4.3.1 \ iwpserver \ htdocsに\ワードプレス\ WP-コンテンツ\テーマ\ MYTHEME \ライン上のfunctions.php 580

ライン580は、次のようになります。ここでは

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

フルコード:

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

これらの通知を解決するためのクイックフィックスはありますか? 本当に

は、この関数はWooCommerce 3+での作業を取得するための正しい方法があるあなたに

+1

いいえ、何も '$のproduct_id'は、関数のスコープで定義されていません' user_logged_in_product_already_bought() '、奇妙な、標準的なコーディングエラーではありません。 – Scuzzy

+0

https://businessbloomer.com/woocommerce-check-current-user-already-purchased-product/からコードをコピー&ペーストしようとしましたが、product_idが不正です。代わりに '$ product-> id'を使用してください – Scuzzy

+0

あいまいさ:はい、私はそれらからコードを使用しています。 Woocommerceが更新される前の古いコード。 –

答えて

2

ありがとうござい任意の助けのために感謝:

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

が(代わりに未定義$product_idのライン580上の$product->get_id()を逃します)

OR、それがあるべきglobal $post;を使用した:

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

コードはfunction.phpのあなたのアクティブな子テーマのファイル(またはアクティブテーマ)または任意のプラグインファイルになります。

テスト済みで動作します。

+0

LoicTheAztec:通知を修正しましたが、過去にこれを購入したことがありません。もう一度購入しますか?ショップページ、単一の製品、またはどこでも。何か不足していますか?私はテストするために20の15のテーマを使用しています。 –

+0

@ryanamiliskuこのコードは定義された顧客のために働きます(ログインしました)...メッセージは、商品がすでに購入されている場合にのみショップページとアーカイブページ(単一の商品ページではありません)に表示されます。 、コードは完全に動作しています...今あなたの実際の質問は、解決されたエラーに関連していた... – LoicTheAztec