在 WooCommerce 中将产品添加到购物车之前,根据以前的订单验证电子邮件地址
Validate e-mail address based on previous orders before adding product to cart in WooCommerce
我使用“PPOM for WooCommerce”插件在单个产品页面上添加了自定义文本字段
这里的目的是让用户输入他的电子邮件地址,然后点击“添加到购物车”按钮
但是在将产品添加到他的卡之前,我想根据电子邮件地址检查现有订单中是否已经出现这种情况
我已经知道如何使用像 woocommerce_add_to_cart_validation
这样的过滤器来完成我想做的事情,但老实说,这就是我的进展
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 );
如果有人能给我一些如何实现的指导就太好了
- 我不使用您使用的(付费)插件,所以在我的回答中,我通过自定义代码向单个产品页面添加了一个字段
- 输入电子邮件地址后,会进行一些验证(不是空的,有效的电子邮件),这可以根据您的需要进一步扩展
- 如果电子邮件地址有效,将从数据库中读取具有该电子邮件地址的所有先前订单
- wc_get_orders() 用于此,可以使用其他参数进一步扩展,例如某些订单状态等。
- 如果您要添加到购物车的产品 ID 存在于之前的订单中(带有客户电子邮件),将显示一条错误消息。
所以你得到:
// 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 );
注:如果不是具体针对当前商品ID,而是针对所有之前售出的商品
替换这部分
// 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;
}
有
// 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;
}
我使用“PPOM for WooCommerce”插件在单个产品页面上添加了自定义文本字段
这里的目的是让用户输入他的电子邮件地址,然后点击“添加到购物车”按钮
但是在将产品添加到他的卡之前,我想根据电子邮件地址检查现有订单中是否已经出现这种情况
我已经知道如何使用像 woocommerce_add_to_cart_validation
这样的过滤器来完成我想做的事情,但老实说,这就是我的进展
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 );
如果有人能给我一些如何实现的指导就太好了
- 我不使用您使用的(付费)插件,所以在我的回答中,我通过自定义代码向单个产品页面添加了一个字段
- 输入电子邮件地址后,会进行一些验证(不是空的,有效的电子邮件),这可以根据您的需要进一步扩展
- 如果电子邮件地址有效,将从数据库中读取具有该电子邮件地址的所有先前订单
- wc_get_orders() 用于此,可以使用其他参数进一步扩展,例如某些订单状态等。
- 如果您要添加到购物车的产品 ID 存在于之前的订单中(带有客户电子邮件),将显示一条错误消息。
所以你得到:
// 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 );
注:如果不是具体针对当前商品ID,而是针对所有之前售出的商品
替换这部分
// 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;
}
有
// 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;
}