Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //prevent the logged-in user to purchase twice the same product
- add_filter ( 'woocommerce_add_to_cart_validation', '__woocommerce_add_to_cart_validation', 99, 3 );
- function __woocommerce_add_to_cart_validation( $status, $product_id, $quantity )
- {
- if ( is_user_logged_in() === FALSE )
- return FALSE;
- //rtrieve all previous orders for the current user
- $args = array(
- 'customer_id' => get_current_user_id(),
- 'limit' => -1, // to retrieve _all_ orders by this user
- );
- $orders = wc_get_orders($args);
- $found_product = FALSE;
- $check_for_product_ids = array( 1, 10, 194 );
- foreach ( $orders as $order )
- {
- foreach ( $order->get_items() as $item_id => $item )
- {
- if ( in_array ( $item->get_product_id(), $check_for_product_ids ) && $product_id == $item->get_product_id() )
- {
- $found_product = TRUE;
- break 2;
- }
- }
- }
- if ( ! $found_product )
- return $status;
- //wc_add_to_cart_message( array( $product_id => $quantity ), true );
- wc_add_notice ('Product already purchased', 'error');
- return FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement