Avvia un progetto

Come bloccare il checkout in Dokan quando il venditore è chiuso

· di Daniel

Qualche aggiornamento fa Dokan ha introdotto una funzione con cui i venditori possono impostare gli orari di apertura del proprio negozio. Questa funzione però si limita a mostrare gli orari. Se vuoi bloccare l’aggiunta al carrello o il checkout quando un venditore, ad esempio un ristorante, è chiuso, ti serve del codice personalizzato. Il codice qui sotto mostra ai clienti un messaggio di errore se vogliono completare l’ordine con prodotti di un venditore chiuso. Inseriscilo nel file functions.php del tuo tema child.

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() );
    }
}
Blog29 ottobre 2026 Direttiva EmpCo: cosa devono sapere i negozi online sulle dichiarazioni ambientali dal 27 settembre 2026 «Sostenibile», «spedizione climaticamente neutra», «imballaggio ecologico»: dichiarazioni come queste compaiono in molti negozi online, nei testi dei prodotti, nei banner, nei badg… Blog29 ottobre 2026 EU AI Act: cosa significa il regolamento europeo sull’IA per siti web e negozi online Dal 2 agosto 2026 si applicano gli obblighi di trasparenza del regolamento europeo sull’intelligenza artificiale, l’EU AI Act. Se usi un chatbot IA sul tuo sito, crei immagini con … Blog22 dicembre 2023 Impedire a WP Rocket di mettere in cache le pagine WooCommerce filtrate Se usi WP Rocket con il preloading insieme a un plugin di filtri come Filter Everything Pro, il database E la cartella della cache possono esplodere. Avevamo un sito con solo circa…

Aumentiamo le tue vendite online.

Raccontaci del tuo shop o sito web. Ti risponde uno sviluppatore, non un team commerciale.

★ 4,85/5 da oltre 800 valutazioni · Envato Elite Author · Made in Germany