Reputation:
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 ?
Upvotes: 0
Views: 1052
Reputation:
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
Upvotes: 0
Reputation: 440
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 );
Upvotes: 1