限制访客用户根据WooCommerce中的先前订单在同一周内多次购买特定产品 [英] Limit guest users to buy a particular product multiple times in the same week based on previous orders in WooCommerce

查看:80
本文介绍了限制访客用户根据WooCommerce中的先前订单在同一周内多次购买特定产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个限制,如果来宾用户一周内两次购买特定产品,他一定不能再次购买同一产品.

I'm looking for a restriction if a guest user has bought a specific product 2 times in a week he must not be able to make another purchase of same product.

我希望根据来宾用户的电子邮件和电话号码应用此限制.

Im looking to apply this restriction based on guest user's email and phone number.

我已经看到很多与此相关的帖子,但是所有帖子都针对注册用户,但是我想将此限制应用于来宾用户.

I've seen many posts related to that but all focus on registered users however i want to apply this restriction for guest users.

不幸的是,这是我当前正在使用的代码

This is the code I am currently using, unfortunately without the desired result

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);


感谢您的帮助.


Any help is appreciated.

推荐答案

对于每周特定产品的限制,您可以使用:

For a restriction for a specific product per week you could use:

  • Based on the billing email address
  • wc_get_orders provide a standard way of retrieving orders - wc_get_orders and WC_Order_Query
function action_woocommerce_checkout_process() {
    // Only for guests
    if ( is_user_logged_in() ) return;
    
    // Isset
    if ( isset( $_POST['billing_email'] ) ) {
        // NOT empty
        if ( ! empty ( $_POST['billing_email'] ) ) {
            $customer_email = $_POST['billing_email'];  
        }
    }
    
    // Isset
    if ( isset ( $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( __( 'Guest users 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 );

这篇关于限制访客用户根据WooCommerce中的先前订单在同一周内多次购买特定产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆