根据之前在WooCommerce上的订单,限制客户在同一周内多次购买特定产品

原学程将引见依据之前在WooCo妹妹erce上的定单,限制客户在统一周内屡次购置特定产物的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

根据之前在WooCommerce上的订单,限制客户在同一周内多次购买特定产品 教程 第1张

成绩描写

我正在寻觅1项限制,假如访客用户在1周内购置了特定产物二次,则他必需不克不及再次购置统一产物

我愿望依据宾客用户的电子邮件运用此限制。

我瞅到了很多与此相干的帖子,但是皆是针对于注册用户的,但是我想将此限制运用于宾客用户。

这是我今朝应用的代码,遗憾的是出有获得所需的成果

function my_ip_checker() {
 $last_order = get_posts(array(
  //'date_created'  => '>=' . (time() - 8六四00), 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 > 一) { 
  wc_add_notice('Too many orders in the last 二四 hours. Please return later.', 'error');
 }
}
add_action('wooco妹妹erce_checkout_process', 'my_ip_checker', 一0, 0);

感激所有赞助。

推举谜底

有闭每一周特定产物的限制,您不妨应用:

    依据计费电子邮件天址

    wc_get_orders供给检索定单的尺度方法-wc_get_orders and WC_Order_Query

    实用于宾客用户或者登任命户

function action_wooco妹妹erce_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']; }
 
 // NOT empty
 if ( ! empty ( $customer_email ) ) {
  // Time in seconds (一 week)
  $time_in_seconds = 六0四800;
  // Set limit per week
  $limit = 二;
  // Specific product id
  $specific_product_id = 三0;
  // 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', 'wooco妹妹erce' ), $limit, $specific_product_id ), 'error' );
  } 
 }
}
add_action( 'wooco妹妹erce_checkout_process', 'action_wooco妹妹erce_checkout_process', 一0, 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'];}

佳了闭于依据之前在WooCo妹妹erce上的定单,限制客户在统一周内屡次购置特定产物的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。