I am creating an e-commerce website. What can I add to the functions.php file of my theme to allow a customer to purchase only one product from the store and then disable any other purchases for 1 week?
The customer can buy any product but after the purchase, no further purchase of any sort can be done for one week. The customer can make any other purchases after one week only.
I could only disable purchases made for a product using this code:
add_filter( 'woocommerce_add_cart_item_data', 'woo_allow_one_item_only' );
function woo_allow_one_item_only( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed' ),
) );
// Order count
$order_count = 1;
if ( count( $customer_orders ) >= $order_count ) {
add_filter( 'woocommerce_is_purchasable', false );
}
The code:
The code goes in function.php file of your active child theme (or active theme). Tested and works.