Woocommerce allows one purchase per week

I am building an e-commerce site. What can I add to my theme's functions.php file so that a customer can only buy one product from the store and then disable any other purchases in 1 week?

The customer can buy any product, but after purchase, no further purchase can be made for one week. The client can make any other purchases only after a week.

I could only disable purchases made for the 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 );
}

      

+3


source to share


2 answers


July 2018 Update - Improved lighter and more efficient version code

  • Simplified product add to checkout basket
  • Better and easier way to check if a user has made a purchase in the past week (built into a reusable conditional function).
  • Added cart check and checkout.

The following code will allow the customer to add only one item to the cart and will only allow one purchase per week , displaying error messages as needed, avoiding any prohibited purchases.



Code:

// Utility conditional function (Check if user has purchased in the passed week)
function has_week_purshases( $user_id = 0 ){
    global $wpdb;

    $customer_id = $user_id > 0 ? $user_id : get_current_user_id();

    $count = $wpdb->get_var( "
        SELECT COUNT(p.ID)
        FROM {$wpdb->prefix}posts as p
        INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
        WHERE p.post_type LIKE 'shop_order'
        AND pm.meta_key LIKE '_customer_user'
        AND pm.meta_value = $customer_id
        AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400 * 7))
    " );

    return $count > 0 ? true : false;
}

// product add to cart validation
add_filter( 'woocommerce_add_to_cart_validation', 'conditionally_allowing_product_added_to_cart', 10, 3 );
function conditionally_allowing_product_added_to_cart( $passed, $product_id, $quantity) {
    // If cart is not empty, customer will not be allowed and a notice will be displayed
    if ( ! WC()->cart->is_empty() ){
        // Display an error notice if there is already an item in cart
        wc_add_notice( __("You can only add an item to cart"), 'error' );
        $passed = false; // Not Allowed
    } elseif ( is_user_logged_in() && has_week_purshases() ) {
        // Display an error notice when customer is not allowed yet
        wc_add_notice( __("You are not allowed yet to add any product in cart"), 'error' );
        $passed = false; // Not Allowed
    }
    return $passed;
}

// Cart and checkout validation
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
    if ( sizeof(WC()->cart->get_cart()) > 1 ) {
        // Display an error notice if there is more than 1 item in cart
        wc_add_notice( _("You can only have only one item in cart"), 'error' );
    } elseif ( is_user_logged_in() && has_week_purshases() ) {
        // Display an error notice when customer is not allowed yet
        wc_add_notice( __("You are not allowed yet to make purchases"), 'error' );
    }
}

      

The code is placed in the function.php file of your active child theme (or active theme). Tested and working.

+3


source


How can this be applied to the following case? : The store is open weekly from Thursday 6:00 am and closes on Tuesday at 11:55 pm. The customer can only make 1 purchase during this time.



Thank!

0


source







All Articles