Advertisement
drkskwlkr

Prevent Purchasing Same Product Again

Mar 16th, 2025
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.34 KB | Software | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: WooCommerce Prevent Repeat Purchases
  4.  * Description: Prevents customers from purchasing products they've already bought.
  5.  * Version: 1.0.0
  6.  * Author: Your Name
  7.  */
  8.  
  9. // Exit if accessed directly
  10. if (!defined('ABSPATH')) {
  11.     exit;
  12. }
  13.  
  14. /**
  15.  * Check if product was previously purchased before adding to cart
  16.  */
  17. function check_if_product_previously_purchased($passed, $product_id) {
  18.     // Skip check if user is not logged in
  19.     if (!is_user_logged_in()) {
  20.         return $passed;
  21.     }
  22.  
  23.     $current_user_id = get_current_user_id();
  24.    
  25.     // Get all customer orders
  26.     $customer_orders = wc_get_orders(array(
  27.         'customer_id' => $current_user_id,
  28.         'status'      => array('wc-completed', 'wc-processing'),
  29.         'limit'       => -1, // Get all orders
  30.     ));
  31.    
  32.     // Check each order to see if this product was previously purchased
  33.     foreach ($customer_orders as $order) {
  34.         foreach ($order->get_items() as $item) {
  35.             // If product ID matches the current product being added to cart
  36.             if ($item->get_product_id() == $product_id) {
  37.                 // Product was previously purchased, prevent adding to cart
  38.                 wc_add_notice(
  39.                     sprintf(
  40.                         __('You\'ve already purchased %s in a previous order (#%s). This product can only be purchased once.', 'woocommerce'),
  41.                         get_the_title($product_id),
  42.                         $order->get_order_number()
  43.                     ),
  44.                     'error'
  45.                 );
  46.                
  47.                 return false;
  48.             }
  49.         }
  50.     }
  51.    
  52.     // Product hasn't been purchased before, allow adding to cart
  53.     return $passed;
  54. }
  55. add_filter('woocommerce_add_to_cart_validation', 'check_if_product_previously_purchased', 10, 2);
  56.  
  57. /**
  58.  * Display a notice on the product page if the customer has already purchased this product
  59.  */
  60. function display_already_purchased_notice() {
  61.     if (!is_user_logged_in() || !is_product()) {
  62.         return;
  63.     }
  64.    
  65.     global $product;
  66.     $product_id = $product->get_id();
  67.     $current_user_id = get_current_user_id();
  68.    
  69.     // Get all customer orders
  70.     $customer_orders = wc_get_orders(array(
  71.         'customer_id' => $current_user_id,
  72.         'status'      => array('wc-completed', 'wc-processing'),
  73.         'limit'       => -1, // Get all orders
  74.     ));
  75.    
  76.     // Check each order to see if this product was previously purchased
  77.     foreach ($customer_orders as $order) {
  78.         foreach ($order->get_items() as $item) {
  79.             // If product ID matches the current product
  80.             if ($item->get_product_id() == $product_id) {
  81.                 // Display notice that product was previously purchased
  82.                 wc_print_notice(
  83.                     sprintf(
  84.                         __('You\'ve already purchased this product in order #%s on %s. This product can only be purchased once.', 'woocommerce'),
  85.                         $order->get_order_number(),
  86.                         $order->get_date_created()->date_i18n(get_option('date_format'))
  87.                     ),
  88.                     'notice'
  89.                 );
  90.                
  91.                 return;
  92.             }
  93.         }
  94.     }
  95. }
  96. add_action('woocommerce_before_single_product', 'display_already_purchased_notice');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement