Iniciar un proyecto
WordPress

Cómo bloquear el pago en Dokan cuando el vendedor está cerrado

· por Daniel

Hace algunas actualizaciones, Dokan introdujo una función para que los vendedores definan el horario de apertura y cierre de su tienda. Sin embargo, esta función solo muestra el horario. Si quiere bloquear el carrito o el pago cuando un vendedor, por ejemplo un restaurante, está cerrado, necesitará código propio. El siguiente código muestra a sus clientes un mensaje de error si intentan pagar productos de un vendedor que está cerrado. Colóquelo en el functions.php de su tema hijo.

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() );
    }
}
WordPress22 de marzo de 2022 Flatsome: cambiar las columnas de productos por categoría Si quiere cambiar el número de columnas de productos por categoría o en la página de la tienda en Flatsome, use este fragmento: General22 de marzo de 2022 Activar HTML en todos los correos de Contact Form 7 Si no quiere activar «Usar HTML» en cada formulario de Contact Form 7, use el siguiente fragmento de código: WordPress3 de abril de 2020 Flatsome Mostrar descripción en el elemento de menú Si quiere mostrar la descripción de un elemento de menú en Flatsome, añada el siguiente código a su functions.php

Aumentemos sus ventas online.

Cuéntenos sobre su tienda o su web. Le responde un desarrollador, no un comercial.

★ 4,85/5 de 800+ valoraciones · Envato Elite Author · Made in Germany