在 WooCommerce 中限制客户在特定时间范围内多次购买特定产品
Limit customers to buy a particular product multiple times within a certain time frame in WooCommerce
如果来宾用户在一周内购买了特定产品 2 次,我正在寻找限制,他不能再购买相同的产品。
我希望根据来宾用户的电子邮件应用此限制。
我看过很多与此相关的帖子,但都关注注册用户,但我想对来宾用户应用此限制。
这是我目前使用的代码,遗憾的是没有得到想要的结果
function my_ip_checker() {
$last_order = get_posts(array(
//'date_created' => '>=' . (time() - 86400), time in seconds
'meta_key' => '_billing_email',
'meta_value' => sanitize_email( $_POST['cb_email'] ),
'post_type' => 'shop_order',
'post_status' => array('wc-processing', 'wc-completed')
));
if($last_order->total > 1) {
wc_add_notice('Too many orders in the last 24 hours. Please return later.', 'error');
}
}
add_action('woocommerce_checkout_process', 'my_ip_checker', 10, 0);
感谢任何帮助。
对于每周特定产品的限制,您可以使用:
- 基于账单电子邮件地址
wc_get_orders
提供检索订单的标准方法 - wc_get_orders and WC_Order_Query
- 将适用于来宾用户或登录用户
function action_woocommerce_checkout_process() {
// Initialize
$customer_email = '';
// Get email
if ( is_user_logged_in() ) {
// Get current user
$user = wp_get_current_user();
// Get email
$customer_email = $user->user_email;
} elseif ( isset( $_POST['billing_email'] ) && ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
} else {
// Get billing_email
$customer_email = WC()->customer->get_billing_email();
}
// NOT empty
if ( ! empty ( $customer_email ) ) {
// Time in seconds (1 week)
$time_in_seconds = 604800;
// Set limit per week
$limit = 2;
// Specific product id
$specific_product_id = 30;
// Get orders from last week from customer by email
$orders_last_week_by_customer_email = wc_get_orders( array(
'date_created' => '>' . (time() - $time_in_seconds ),
'customer' => $customer_email,
));
// Total (counter)
$total = 0;
// Iterating through each order
foreach ( $orders_last_week_by_customer_email as $order ) {
// Going through order items
foreach ( $order->get_items() as $item ) {
// Get product ID
$product_id = $item->get_product_id();
// Compare
if ( $specific_product_id == $product_id ) {
// Get quantity
$quantity = $item->get_quantity();
// Add to total
$total += $quantity;
}
}
}
// Show error when total >= limit
if ( $total >= $limit ) {
wc_add_notice( sprintf( __( 'You are not allowed to buy more than %d pieces of product with ID = %d in one week', 'woocommerce' ), $limit, $specific_product_id ), 'error' );
}
}
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
仅对来宾用户应用此:
替换
// Get email
if ( is_user_logged_in() ) {
// Get current user
$user = wp_get_current_user();
// Get email
$customer_email = $user->user_email;
} elseif ( isset( $_POST['billing_email'] ) && ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
}
有
// Only for guests
if ( ! is_user_logged_in() ) return;
if ( isset( $_POST['billing_email'] ) && ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
}
对于每周特定可变产品的限制,我使用了:
- 我使用父产品 ID 来限制来宾用户:
- 基于账单电子邮件地址
- 基于phone个数
- wc_get_orders 提供检索订单的标准方法 -
wc_get_orders
和 WC_Order_Query
function action_woocommerce_checkout_process() {
$specific_product_name = 'Buy iPhone 12 with Z Protect+ included';
// Only for guests
if ( is_user_logged_in() ) return;
//product check start
global $woocommerce;
$parent_id;
$_product = '';
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
}
$parent_id = $_product->get_parent_id();
if( $parent_id == 389 ){
//product check end
// Isset
if ( isset( $_POST['billing_phone'] ) ) {
// NOT empty
if ( ! empty ( $_POST['billing_phone'] ) ) {
$customer_phone = $_POST['billing_phone'];
}
}
// Isset
if ( isset ( $customer_phone ) ) {
// Time in seconds (1 week)
$time_in_seconds = 604800;
// Set limit per week
$limit = 2;
// Specific product name
$specific_product_name = 'Buy iPhone 12 with Z Protect+ included';
// Get orders from last week from customer by phone
$orders_last_week_by_customer_phone = wc_get_orders( array(
'date_created' => '>' . (time() - $time_in_seconds ),
'billing_phone' => $customer_phone,
));
$total = 0;
// Iterating through each order
foreach ( $orders_last_week_by_customer_phone as $order ) {
// Going through order items
foreach ( $order->get_items() as $item ) {
// Name of the producte
$product_name = $item->get_name();
// Compare
if ( $specific_product_name == $product_name ) {
// Add to total
$total += 1;
}
}
}
// Show error when total >= limit
if ( $total >= $limit ) {
wc_add_notice( sprintf( __( 'You are not allowed to buy more than %d iPhones. For more information please contact support.', 'woocommerce' ), $limit ), 'error' );
}
}
// Isset
if ( isset( $_POST['billing_email'] ) ) {
// NOT empty
if ( ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
}
}
// Email NOT empty
if ( ! empty ( $customer_email ) ) {
// Time in seconds (1 week)
$time_in_seconds = 604800;
// Set limit per week
$limit = 2;
// Specific product id
$specific_product_id = 'Buy iPhone 12 with Z Protect+ included';
// Get orders from last week from customer by email
$orders_last_week_by_customer_email = wc_get_orders( array(
'date_created' => '>' . (time() - $time_in_seconds ),
'customer' => $customer_email,
));
// Total (counter)
$total = 0;
// Iterating through each order
foreach ( $orders_last_week_by_customer_email as $order ) {
// Going through order items
foreach ( $order->get_items() as $item ) {
// Get product ID
$product_id = $item->get_name();
// Compare
if ( $specific_product_id == $product_id ) {
// Get quantity
$quantity = $item->get_quantity();
// Add to total
$total += $quantity;
}
}
}
// Show error when total >= limit
if ( $total >= $limit ) {
wc_add_notice( sprintf( __( 'You are not allowed to buy more than %d iPhones. For more information please contact support.', 'woocommerce' ), $limit), 'error' );
}
}
//product check start
}
//product check start
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
感谢上面的第一个答案为我提供了开始实施的基础。
欢迎对我的代码进行任何进一步的改进。
如果来宾用户在一周内购买了特定产品 2 次,我正在寻找限制,他不能再购买相同的产品。
我希望根据来宾用户的电子邮件应用此限制。
我看过很多与此相关的帖子,但都关注注册用户,但我想对来宾用户应用此限制。
这是我目前使用的代码,遗憾的是没有得到想要的结果
function my_ip_checker() {
$last_order = get_posts(array(
//'date_created' => '>=' . (time() - 86400), time in seconds
'meta_key' => '_billing_email',
'meta_value' => sanitize_email( $_POST['cb_email'] ),
'post_type' => 'shop_order',
'post_status' => array('wc-processing', 'wc-completed')
));
if($last_order->total > 1) {
wc_add_notice('Too many orders in the last 24 hours. Please return later.', 'error');
}
}
add_action('woocommerce_checkout_process', 'my_ip_checker', 10, 0);
感谢任何帮助。
对于每周特定产品的限制,您可以使用:
- 基于账单电子邮件地址
wc_get_orders
提供检索订单的标准方法 - wc_get_orders and WC_Order_Query- 将适用于来宾用户或登录用户
function action_woocommerce_checkout_process() {
// Initialize
$customer_email = '';
// Get email
if ( is_user_logged_in() ) {
// Get current user
$user = wp_get_current_user();
// Get email
$customer_email = $user->user_email;
} elseif ( isset( $_POST['billing_email'] ) && ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
} else {
// Get billing_email
$customer_email = WC()->customer->get_billing_email();
}
// NOT empty
if ( ! empty ( $customer_email ) ) {
// Time in seconds (1 week)
$time_in_seconds = 604800;
// Set limit per week
$limit = 2;
// Specific product id
$specific_product_id = 30;
// Get orders from last week from customer by email
$orders_last_week_by_customer_email = wc_get_orders( array(
'date_created' => '>' . (time() - $time_in_seconds ),
'customer' => $customer_email,
));
// Total (counter)
$total = 0;
// Iterating through each order
foreach ( $orders_last_week_by_customer_email as $order ) {
// Going through order items
foreach ( $order->get_items() as $item ) {
// Get product ID
$product_id = $item->get_product_id();
// Compare
if ( $specific_product_id == $product_id ) {
// Get quantity
$quantity = $item->get_quantity();
// Add to total
$total += $quantity;
}
}
}
// Show error when total >= limit
if ( $total >= $limit ) {
wc_add_notice( sprintf( __( 'You are not allowed to buy more than %d pieces of product with ID = %d in one week', 'woocommerce' ), $limit, $specific_product_id ), 'error' );
}
}
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
仅对来宾用户应用此:
替换
// Get email
if ( is_user_logged_in() ) {
// Get current user
$user = wp_get_current_user();
// Get email
$customer_email = $user->user_email;
} elseif ( isset( $_POST['billing_email'] ) && ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
}
有
// Only for guests
if ( ! is_user_logged_in() ) return;
if ( isset( $_POST['billing_email'] ) && ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
}
对于每周特定可变产品的限制,我使用了:
- 我使用父产品 ID 来限制来宾用户:
- 基于账单电子邮件地址
- 基于phone个数
- wc_get_orders 提供检索订单的标准方法 -
wc_get_orders
和WC_Order_Query
function action_woocommerce_checkout_process() {
$specific_product_name = 'Buy iPhone 12 with Z Protect+ included';
// Only for guests
if ( is_user_logged_in() ) return;
//product check start
global $woocommerce;
$parent_id;
$_product = '';
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
}
$parent_id = $_product->get_parent_id();
if( $parent_id == 389 ){
//product check end
// Isset
if ( isset( $_POST['billing_phone'] ) ) {
// NOT empty
if ( ! empty ( $_POST['billing_phone'] ) ) {
$customer_phone = $_POST['billing_phone'];
}
}
// Isset
if ( isset ( $customer_phone ) ) {
// Time in seconds (1 week)
$time_in_seconds = 604800;
// Set limit per week
$limit = 2;
// Specific product name
$specific_product_name = 'Buy iPhone 12 with Z Protect+ included';
// Get orders from last week from customer by phone
$orders_last_week_by_customer_phone = wc_get_orders( array(
'date_created' => '>' . (time() - $time_in_seconds ),
'billing_phone' => $customer_phone,
));
$total = 0;
// Iterating through each order
foreach ( $orders_last_week_by_customer_phone as $order ) {
// Going through order items
foreach ( $order->get_items() as $item ) {
// Name of the producte
$product_name = $item->get_name();
// Compare
if ( $specific_product_name == $product_name ) {
// Add to total
$total += 1;
}
}
}
// Show error when total >= limit
if ( $total >= $limit ) {
wc_add_notice( sprintf( __( 'You are not allowed to buy more than %d iPhones. For more information please contact support.', 'woocommerce' ), $limit ), 'error' );
}
}
// Isset
if ( isset( $_POST['billing_email'] ) ) {
// NOT empty
if ( ! empty ( $_POST['billing_email'] ) ) {
$customer_email = $_POST['billing_email'];
}
}
// Email NOT empty
if ( ! empty ( $customer_email ) ) {
// Time in seconds (1 week)
$time_in_seconds = 604800;
// Set limit per week
$limit = 2;
// Specific product id
$specific_product_id = 'Buy iPhone 12 with Z Protect+ included';
// Get orders from last week from customer by email
$orders_last_week_by_customer_email = wc_get_orders( array(
'date_created' => '>' . (time() - $time_in_seconds ),
'customer' => $customer_email,
));
// Total (counter)
$total = 0;
// Iterating through each order
foreach ( $orders_last_week_by_customer_email as $order ) {
// Going through order items
foreach ( $order->get_items() as $item ) {
// Get product ID
$product_id = $item->get_name();
// Compare
if ( $specific_product_id == $product_id ) {
// Get quantity
$quantity = $item->get_quantity();
// Add to total
$total += $quantity;
}
}
}
// Show error when total >= limit
if ( $total >= $limit ) {
wc_add_notice( sprintf( __( 'You are not allowed to buy more than %d iPhones. For more information please contact support.', 'woocommerce' ), $limit), 'error' );
}
}
//product check start
}
//product check start
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
感谢上面的第一个答案为我提供了开始实施的基础。 欢迎对我的代码进行任何进一步的改进。