Start a project
WordPress

How to block checkout in Dokan when vendor is closed

· by Daniel

Some updates ago Dokan introduces a feature for vendors to set opening and closing hours for their store. However this new functionality does nothing than showing vendors business hours. If you want to block the add to cart or checkout process, when a vendor e.g. a restaurant is closed, you will need some custom code. Below code will show your customers an error message when they want to checkout with products from a vendor who is closed. Place that into your functions.php of your child theme.

add_action( 'woocommerce_checkout_process', 'check_shop_open' );
add_action( 'woocommerce_check_cart_items' , 'check_shop_open' );
function check_shop_open()
{
    $vendorId = false;
    // Set vendor ID based on cart items
    $cartItems = WC()->cart->get_cart();
    foreach ( $cartItems as $cartItemKey => $cartItem ) {
	    $vendorId   = get_post_field( 'post_author', $cartItem['product_id'] );
	    $vendorInfo  = dokan_get_store_info( $vendorId );
	}
	if(!$vendorId) {
		return;
	}
	if(empty($vendorInfo)) {
		return;
	}
	if(!isset($vendorInfo['dokan_store_time']) || empty($vendorInfo['dokan_store_time']) ) {
		return;
	}
	// Get All Vendor Opening Hours
	$wpTimezone = wp_timezone();
	$vendorName = $vendorInfo['store_name'];
	$storeOpeningHours = $vendorInfo['dokan_store_time'];
	$dateFormat = 'd.m.Y';
	$currentDate = DateTime::createFromFormat($dateFormat, date($dateFormat), $wpTimezone);
	$currentDay = strtolower( $currentDate->format('l') );
	if(!isset($storeOpeningHours[$currentDay]) || empty($storeOpeningHours[$currentDay])) {
		return;
	}
	// Set current Day Vendor Opening Hours
	$storeOpeningHoursCurrentDay = $storeOpeningHours[$currentDay];
	// Store Closed Today
	if(isset($storeOpeningHoursCurrentDay['status']) && $storeOpeningHoursCurrentDay['status'] == "close") {
    	show_cart_checkout_message( sprintf( __('Oh sorry. %s is closed today! Please return another day.', 'flatsome-child'), $vendorName ) );
    	return false;
	}
	// Store not open
	if(isset($storeOpeningHoursCurrentDay['opening_time']) && !empty($storeOpeningHoursCurrentDay['opening_time'])) {
		$storeOpeningHoursCurrentDayOpeningTime = $storeOpeningHoursCurrentDay['opening_time'];
		$storeOpeningHoursCurrentDayOpeningTimeObject = DateTime::createFromFormat('H:i', $storeOpeningHoursCurrentDayOpeningTime, $wpTimezone);
		if($currentDate <= $storeOpeningHoursCurrentDayOpeningTimeObject) { show_cart_checkout_message( sprintf( __('Oh sorry! %s opens at %s o\'clock. Please return another day.', 'flatsome-child'), $vendorName, $storeOpeningHoursCurrentDayOpeningTime) ); return false; } } // Store closed time if(isset($storeOpeningHoursCurrentDay['closing_time']) && !empty($storeOpeningHoursCurrentDay['closing_time'])) { $storeClosingHoursCurrentDayClosingTime = $storeOpeningHoursCurrentDay['closing_time']; $storeClosingHoursCurrentDayClosingTimeObject = DateTime::createFromFormat('H:i', $storeClosingHoursCurrentDayClosingTime, $wpTimezone); if($currentDate >= $storeClosingHoursCurrentDayClosingTimeObject) {
			show_cart_checkout_message( sprintf( __('Oh sorry! %s is closed since %s o\'clock. Please return another day.', 'flatsome-child'), $vendorName, $storeClosingHoursCurrentDayClosingTime) );
			return false;
		}
	}
}
// Show a custom WC error mesagge (cart + checkout)
function show_cart_checkout_message($message)
{
	if(empty($message)) {
		return;
	}
    if( is_cart() ) {
        wc_print_notice(
            $message, 'error'
        );
    } else {
        wc_add_notice(
            $message, 'error'
        );
        wp_redirect( wc_get_cart_url() );
    }
}

More articles

All articles →
WordPressMarch 22, 2022 Flatsome Change Product Columns per Category If you want to change the product columns per category or on your shop page in Flatsome, use this snippet: GeneralMarch 22, 2022 How to Globally Enable HTML for CF7 Mails If you do not want to enable “Use HTML” for every contact form 7 forms, then you can use the following code snippet: WordPressApril 3, 2020 Flatsome Show Description in Menu Item If you want to show the description of a menu item in flatsome you need to add the following code into your functions.php

Let's increase your online sales.

Tell us about your shop or website. A developer replies, not a sales team.

★ 4.85/5 from 800+ ratings · Envato Elite Author · Made in Germany