2016-10-04 121 views
0

所以我有這個wordpress插件,它自己隱藏'添加到購物車'按鈕並添加一個下載按鈕,當Woocommerce產品是免費的將購買woocommerce產品功能添加到插件

但是我想添加另一個功能到這個插件,當產品已經被購買時應該做同樣的事情。

所以我試圖使用wc_customer_bought_product($customer_email, $user_id, $the_product) woocommerce功能。

但它似乎並沒有工作。

//Returns true if the product/variation is free, downloadable and vitual. 
//Added by me: OR return true too if product was bought 

function dfd_is_virtual_free_download_product($post) { 

    global $woocommerce, $product; 
    $user_id = get_current_user_id(); 
    $current_user= wp_get_current_user(); 
    $customer_email = $current_user->email; 
    $current_product = $product->id; 

    if (empty($post)) return false; 


    $the_product = get_product($post); 

    if (!$the_product) { 
     //its not a product or product not found 
     return false; 
    } 

    $is_virtual = $the_product->is_virtual(); 
    $price = $the_product->regular_price; 
    $sale_price = $the_product->sale_price; 
    $is_downloadable = $the_product->is_downloadable(); 

    if ($sale_price == "0" || $sale_price == "0.00" || wc_customer_bought_product($customer_email, $user_id, $current_product)){ 
     $price = $sale_price; 
    } 
    if($price == 0 && $is_virtual == 1 && $is_downloadable == 1){ 
     return true; 
    } 
    return false; 

} 

回答

0

我在代碼中發現錯誤。 爲什麼你兩次獲取ID?

$user_id = get_current_user_id(); 
$current_user= wp_get_current_user(); 

$ CURRENT_USER已經注入這個屬性,你可以用這種方式$ current_user-致電> ID

的另一個問題是調用未定義的屬性$ current_user-的>電子郵件

//This is wrong 
$customer_email = $current_user->email; 

//This works 
$customer_email = $current_user->user_email; 

希望這幫助。

試圖調用函數以這種方式:

wc_customer_bought_product($customer_email, (int)$user_id, (int)$current_product); 

你最好嘗試用var_dump()用於獲取有關價值或者只是調試信息。

+0

感謝您的幫助,但它沒有奏效我仍然得到相同的結果。 –

+0

我編輯了我的答案 – barbocc

相關問題