根据之前的订单和 WooCommerce 中的 phone 数量限制来宾用户在同一周内多次购买特定产品

Restrict guest users to buy a specific product multiple times in the same week based on previous orders and phone number in WooCommerce

我正在寻找一个限制,如果来宾用户在一周内购买了特定产品 2 次,他就不能再购买相同的产品。

我希望根据访客用户的 phone 号码应用此限制

我看过很多与此相关的帖子,但都关注注册用户,但我想对来宾用户应用此限制。

基于 答案代码,这是我目前正在使用的代码,不幸的是没有得到想要的结果(即使数字是全新的和不同的,这个代码段仍然限制用户)。

function action_woocommerce_checkout_process() {
    // Only for guests
    if ( is_user_logged_in() ) return;
    
    if ( isset( $_POST['billing_phone'] ) ) {
        // NOT empty
        if ( ! empty ( $_POST['billing_phone'] ) ) {
            $customer_phone = $_POST['billing_phone'];  
        }
    }
    // Phone NOT empty
    if ( ! empty ( $customer_phone ) ) {        
        // Time in seconds (1 week)
        $time_in_seconds = 604800;
        
        // Set limit per week
        $limit = 2;
        
        // Specific product id
        $specific_product_name = 'Buy product' ;
        
        // Get orders from last week from customer by email
        $orders_last_week_by_customer_phone = wc_get_orders( array(
            'date_created' => '>' . (time() - $time_in_seconds ),
            'customer-phone' => $customer_phone,
        ));
        
        // Total (counter)
        $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 ) {
                // Get product ID
                $product_name = $item->get_name();
                
                // Compare
                if ( $specific_product_name == $product_name ) {
                    // 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 products. For more information please contact support. phone', 'woocommerce' ), $limit), 'error' );
        }       
    }
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );

customer-phone根本不存在,你应该用billing_phone代替。

参见:wc_get_orders and WC_Order_Query

地址和姓名

billing_first_name、billing_last_name、billing_company、billing_address_1、billing_address_2、billing_city、billing_state、billing_postcode, billing_country, billing_email, billing_phone, shipping_first_name, shipping_last_name, shipping_company, shipping_address_1, shipping_address_2 , shipping_city, shipping_state, shipping_postcode, shipping_country, customer_ip_address


客户

接受字符串或整数:订单的帐单电子邮件或客户 ID。



注意:如果纯粹是关于产品在订单中出现的次数(每个订单1x)与每个订单的产品数量,则没有必要在 foreach 循环中使用 get_quantity()


所以你得到:

function action_woocommerce_checkout_process() {
    // Only for guests
    if ( is_user_logged_in() ) return;
    
    // 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 product';
        
        // 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 (counter)
        $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 product
                $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 products. For more information please contact support.', 'woocommerce' ), $limit ), 'error' );
        }   
    }
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );