Trying to disable add to cart button on the weekends in Woocommerce

Viewed 39

I've been working on a website that will be selling products during the week, but won't be accepting orders throughout the weekend. I've been using the following code through the plugin Snippets on WordPress, but it seems to not be taking effect. Thanks for the help

function is_shop_open() {
    date_default_timezone_set('America/Port_of_Spain');
    $days_opened = array(1, 2, 3, 4, 5); // mondays, tuesdays, wednesdays, thursdays and fridays
    if(in_array( date(w), $days_opened, true )){
        return true;
    }
    else{
        return false;
    }
    
}

function widget( $args, $instance ) {
    add_action( 'wp', 'cross_remove_add_to_cart_product_categories' );

}

function cross_remove_add_to_cart_product_categories(){
    if( is_product_category( 'Regular Menu Items' ) && is_shop_open() == false) {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    }
}
1 Answers

Try this code.

function is_shop_open()
{
    date_default_timezone_set('America/Port_of_Spain');
    $days_opened = array(1, 2, 3, 4, 5); // mondays, tuesdays, wednesdays, thursdays and fridays
    if (in_array(date(w), $days_opened, true)) {
        return true;
    } else {
        return false;
    }
    
}

add_action('init', 'cross_remove_add_to_cart_product_categories');

function cross_remove_add_to_cart_product_categories()
{
    if (is_product_category('Regular Menu Items') && !is_shop_open()) {
        
        remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
        
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
    }
}
Related