如何通过 WooCommerce 中的时间限制来限制每个客户的订单数量

问题描述 投票:0回答:2

我想阻止客户每 24 小时进行一次销售。

检查过去24小时内该客户是否有其他购买,并在付款前显示错误并要求稍后返回

到目前为止我已经尝试过的事情

function prevent_repeat_order() { 
    $last_24_hours_from_order_results = wc_get_customer_last_order($user_id);
    (array( 'date_created' => '>=' . (time() - 86400), // time in seconds 'paginate' => true // adds a total field to the results ));

    if ( $last_24_hours_from_last_order->total > 1 ) { 
        wc_add_notice('Too many orders in the last 24 hours. Please return later.', 'error');
    }
}
add_action('woocommerce_checkout_process', 'prevent_repeat_order', 10, 0); 
php wordpress woocommerce hook-woocommerce orders
2个回答
3
投票

当然这取决于你想在哪里显示消息。以下代码在结帐页面上显示错误消息,如果不满足条件,则隐藏

proceed_to_checkout
按钮。


https://www.php.net/manual/en/function.date.php 秒可以转换为小时等...

function new_order_allowed() {
    // Only on cart and check out pages
    if( ! ( is_cart() || is_checkout() ) ) return;

    // Will only work for logged in users
    if ( is_user_logged_in() ) {
        // Get user id
        $user_id = get_current_user_id();

        // Get last order
        $last_order = wc_get_customer_last_order( $user_id );

        if ( $last_order ) {
            // Get date last order created - Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
            $date_created = $last_order->get_date_created()->format( 'U' );

            // Get current date - Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
            $current_time = current_time( 'U', true );

            // 24hr in seconds
            $day_in_sec = 60 * 60 * 24;

            // Seconds have passed since the last order
            $seconds_passed = $current_time - $date_created;

            // Check
            if ( $seconds_passed < $day_in_sec ) {
                // Add notice
                wc_add_notice( sprintf( 'New orders are only allowed %1$s seconds after your previous order, currently %2$s seconds are passed', $day_in_sec, $seconds_passed ), 'error' );

                // Removing the Proceed to checkout button from the Cart page
                remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
            }
        }
    }
}
add_action( 'woocommerce_check_cart_items', 'new_order_allowed' );

0
投票

对于那些想要避免每年购买一次的人:

function new_order_allowed() {
// Only on cart and check out pages
if( ! ( is_cart() || is_checkout() ) ) return;

// Will only work for logged in users
if ( is_user_logged_in() ) {
    // Get user id
    $user_id = get_current_user_id();

    // Get last order
    $last_order = wc_get_customer_last_order( $user_id );

    if ( $last_order ) {
        // Get date last order created 
        $date_created = $last_order->get_date_created()->format( 'z' )+1;

        // Get current date
        $current_time = current_time( 'z', true )+1;

        // 1 year in days
        $year_in_day = 365;

        // Days have passed since the last order
        $days_passed = $current_time - $date_created;

        // Check
        if ( $days_passed < $year_in_day ) {
            // Add notice
            wc_add_notice( sprintf( 'New orders are only allowed %1$s days after your previous order, currently %2$s days are passed', $year_in_day, $days_passed ), 'error' );

            // Removing the Proceed to checkout button from the Cart page
            remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
        }
    }
}

}

add_action( 'woocommerce_check_cart_items', 'new_order_allowed' );

© www.soinside.com 2019 - 2024. All rights reserved.