- 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;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…