0

I am setting up a wordpress/woocommerce website and would like users to only purchase a certain number of items just once per 30 days due to the price of the items but in a certain catergory which is 'microwaves', I have found this code on here and it works really really well but for ALL products I really just want ot to work for 'microwaves' catergory but keep all the other functions.

`

// 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 * 30))
    " );

    return $count > 0 ? true : false;
}

// 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 ( 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' );
    }
}

`

  • Then you will need to modify that query, and JOIN the relevant table that holds the relation between products and categories as well. https://stackoverflow.com/a/36973435/1427878 has an overview of the relations. – CBroe Nov 18 '22 at 13:09
  • hey CBroe yes I've seen this post but I couldn't figure it out can you help? – jamesnelson82 Nov 18 '22 at 13:15

0 Answers0