Search code examples
phpwordpresswoocommerceordersemail-validation

Validate e-mail address based on previous orders before adding product to cart in WooCommerce


I have added a custom text-field on the single product page with the "PPOM for WooCommerce" plugin

The intention here is that the user enters his e-mail address and then clicks on the "add-to-cart" button

But before adding the product to his card, I want to check on the basis of the e-mail address whether this already occurs in the existing orders

I already found out about using a filter like woocommerce_add_to_cart_validation to accomplish what I want to do but to be honest, that is how far I got

function validate_email( $passed, $product_id, $quantity, $variation_id = '', $variations= '' ) {
    // TODO: Collect all previous orders using the entered email address and check if this already exists in the DB

    wc_add_notice( __( 'E-Mail Address already sold', 'woocommerce' ), 'error' );
    return false;
}
add_filter( 'woocommerce_add_to_cart_validation', 'validate_email', 10, 5 );

It would be great if anyone could give me some guidance on how to accomplish that


Solution

    • I don't use the (paid) plugin that you use, so in my answer, I add a field via custom code to the single product page
    • After entering an email address, some validation will take place (not empty, valid email), this can be further expanded to your needs
    • If the email address is valid, all previous orders with that email address will be read from the database
      • wc_get_orders() is used for this, this can be further expanded with additional parameters such as certain order statuses, etc.
    • If the product ID you want to add to the shopping cart is present in previous orders (with customer email), an error message will be displayed.

    So you get:

    // Add input field to single product page
    function action_woocommerce_before_add_to_cart_button() {
        echo '<div class="custom-field-wrapper">';
        echo '<label for="customer-email">Enter your email:</label>';
        echo '<input type="email" id="customer-email" name="customer-email">';
        echo '</div>';
    }
    add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button', 10, 0 );
    
    // Validate
    function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
        // Isset
        if ( isset ( $_POST['customer-email'] ) ) {
            // Sanitize
            $customer_email = sanitize_text_field( $_POST['customer-email'] );
            
            // Empty
            if ( empty ( $customer_email ) ) {
                wc_add_notice( __( 'Please enter a value into the field', 'woocommerce' ), 'error' );
                $passed = false;
                
            // Verifies that an email is valid.
            // NOT valid
            } elseif ( ! is_email( $customer_email ) ) {
                wc_add_notice( __( 'Invalid email address', 'woocommerce' ), 'error' );
                $passed = false;
            } else {
                // Get orders by email
                $orders_by_customer_email = wc_get_orders( array(
                    'customer' => $customer_email,
                ));
                
                // Set flag
                $flag = false;
                
                // This function is an alias of count()
                if ( sizeof( $orders_by_customer_email ) > 0 ) {
                    // Iterating through each order
                    foreach ( $orders_by_customer_email as $order ) {
                        // Going through order items
                        foreach ( $order->get_items() as $item ) {
                            // Get product ID
                            $product_id_in_order = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
                        
                            // Compare
                            if ( $product_id_in_order == $product_id ) {
                                $flag = true;
                                break;
                            }
                        }
                    }
                }
                
                // True
                if ( $flag ) {
                    wc_add_notice( __( 'You have already purchased this product before', 'woocommerce' ), 'error' );
                    $passed = false;
                }
            }
        }
        
        return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
    

    Note: if it is not specifically about the current product ID, but about all previously sold items

    Replace this part

    // This function is an alias of count()
    if ( sizeof( $orders_by_customer_email ) > 0 ) {
        // Iterating through each order
        foreach ( $orders_by_customer_email as $order ) {
            // Going through order items
            foreach ( $order->get_items() as $item ) {
                // Get product ID
                $product_id_in_order = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
            
                // Compare
                if ( $product_id_in_order == $product_id ) {
                    $flag = true;
                    break;
                }
            }
        }
    }
    
    // True
    if ( $flag ) {
        wc_add_notice( __( 'You have already purchased this product before', 'woocommerce' ), 'error' );
        $passed = false;
    }
    

    With

    // This function is an alias of count()
    if ( sizeof( $orders_by_customer_email ) > 0 ) {
        $flag = true;
    }
    
    // True
    if ( $flag ) {
        wc_add_notice( __( 'You have already purchased something before', 'woocommerce' ), 'error' );
        $passed = false;
    }