I have an array of product ids = [21,82]
...i want to prevent the user from buying that product again if user has purchased it already ..is it possible to achieve this in woocommerce ?
Asked
Active
Viewed 860 times
0
2 Answers
1
You can try this function
function sv_disable_repeat_purchase( $purchasable, $product ) {
// Enter the ID of the product that shouldn't be purchased again
$non_purchasable = 21;
// Get the ID for the current product (passed in)
$product_id = $product->is_type( 'variation' ) ? $product->variation_id :
$product->id;
// Bail unless the ID is equal to our desired non-purchasable product
if ( $non_purchasable != $product_id ) {
return $purchasable;
}
// return false if the customer has bought the product
if ( wc_customer_bought_product( wp_get_current_user()->user_email,
get_current_user_id(), $product_id ) ) {
$purchasable = false;
}
// Double-check for variations: if parent is not purchasable, then
//variation is not
if ( $purchasable && $product->is_type( 'variation' ) ) {
$purchasable = $product->parent->is_purchasable();
}
return $purchasable;
}
add_filter( 'woocommerce_variation_is_purchasable', 'sv_disable_repeat_purchase', 10,
2 );
add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );

Problem Solver
- 422
- 2
- 9
-
Hello thanks for answer! i am having a array of products. Please assist in that case – Oct 29 '18 at 07:11
-
@LatheeshVMVilla you have to add for each loop if you have an array of products and go through this function – Problem Solver Oct 29 '18 at 07:14
-
Okay just testing it out..will it be possible to show how it is used. – Oct 29 '18 at 07:23
-
Hi code above slows my website very much..any possible solution – Oct 30 '18 at 12:15
0
Another method ..just disabling the place order button with a filter
add_filter( 'woocommerce_order_button_html', 'replace_order_button_html', 10, 2 );
function replace_order_button_html( $order_button ) {
if ( is_user_logged_in() ) {
global $product;
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$customer_email = $current_user->user_email;
$product_id= 182;
if ( wc_customer_bought_product($customer_email,$user_id,$product_id)) {
$order_button_text = __( "Max volume reached", "woocommerce" );
$style = ' style="color:#fff;cursor:not-allowed;background-color:#999;"';
return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
}else{
return $order_button;
}
}}
Thanks to Loic : https://stackoverflow.com/a/50222706/3697484