mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/78735562/restrict-customers-to-buy-once-per-year-in-woocommerce
21 lines
1.1 KiB
Text
21 lines
1.1 KiB
Text
add_action( 'woocommerce_check_cart_items', 'new_order_allowed' );
|
|
function new_order_allowed() {
|
|
if ( is_user_logged_in() && ( is_cart() || is_checkout() ) ) {
|
|
$last_order = wc_get_customer_last_order( get_current_user_id() );
|
|
|
|
if ( is_a( $last_order, 'WC_Order') ) {
|
|
$created_timestamp = $last_order->get_date_created()->getTimestamp();
|
|
$allowed_timestamp = strtotime('-1 year');
|
|
$remaining_days = floor(($created_timestamp - $allowed_timestamp) / DAY_IN_SECONDS);
|
|
$days_passed = 365 - $remaining_days;
|
|
|
|
if ( $created_timestamp >= $allowed_timestamp ) {
|
|
wc_add_notice( sprintf(
|
|
__('<strong>ONLY ONE PURCHASE IS ALLOWED WITHIN 365 DAYS.</strong><br>Your last order was made %s ago. Please try again when the 365 day period has been reached.'),
|
|
sprintf( _n( '%s day', '%s days', $days_passed ) ) ), 'error' );
|
|
|
|
remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
|
|
}
|
|
}
|
|
}
|
|
}
|