2016-02-13 60 views
1

在首次購買任何產品後,是否有阻止登錄用戶購買任何產品的方法?Woocommerce:在所有商店中只允許每次訂購一次購買

我想限制每個訂閱只購買一個產品,然後沒有更多的商店在任何產品上,只有新的訂閱。

我發現了一個過濾器,幾乎做什麼,我需要,但限制每個產品只有一個,

add_filter('woocommerce_add_to_cart_validation','rei_woocommerce_add_to_cart_validation',20, 2); 
function rei_woocommerce_add_to_cart_validation($valid, $product_id){ 
    $current_user = wp_get_current_user(); 
    if (wc_customer_bought_product($current_user->user_email, $current_user->ID, $product_id)) { 
     wc_add_notice(__('Purchased', 'woocommerce'), 'error'); 
     $valid = false; 
    } 
    return $valid; 
} 

,但我需要先購買訂閱後鎖定所有其他產品。

也許最好的辦法是當用戶點擊支付產品在結帳頁面(此時他需要登錄)並檢查他是否已經購買任何產品..如果是,他不能支付,只有新的訂閱...

即時通訊編碼醜陋..有人可以幫助我嗎?

回答

0

對於那些誰正在尋找相同的答案,這裏是我做了什麼:

// Get all customer orders 
$customer_orders = get_posts(array(
    'numberposts' => -1, 
    'meta_key' => '_customer_user', 
    'meta_value' => get_current_user_id(), 
    'post_type' => wc_get_order_types(), 
    'post_status' => array('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed'), 

)); 

// Order count 
$order_count = 1; 

if (count($customer_orders) >= $order_count) { 
    add_filter('woocommerce_is_purchasable', false); 
} 
相關問題