Следующий код позволит иметь определенные предметы, которые можно купить по воскресеньям, и определенные предметы, которые можно купить за определенный промежуток времени (от 12 ч. До 14 ч. 30 м.).
Это комбинация из двух кодов ответов:
Вот этот код:
// The "sunday" products (setting your product IDS in the array)
function sunday_products() {
// HERE your product IDs in the array (need to be coma separated)
return array( 37 );
}
// The 12h to 14h30 products (setting your product IDS in the array)
function time_range_products() {
// HERE your product IDs in the array (need to be coma separated)
return array( 53, 57 );
}
// Utility conditional function that check if day is sunday (returns boolean)
function is_sunday() {
// Set Your shop time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/London');
// If the current day is "sunday" return true (else retun false)
return ( date('w') == 0 ) ? true : false;
}
// Utility conditional funtion for open hours (returns boolean true when store is open)
function in_time_range() {
// Set Your shop time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/London');
// Below your shop time and dates settings
$open_time = mktime('12', '00', '00', date('m'), date('d'), date('Y')); // 12:00:00
$end_time = mktime('14', '30', '00', date('m'), date('d'), date('Y')); // 14:30:00
$now = time(); // Current timestamp in seconds
return ( $now >= $start_time && $now <= $end_time ) ? true : false;
}
// Enable purchases for specific items on sundays only
add_filter( 'woocommerce_variation_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
function enable_specific_products_on_sundays( $purchasable, $product ) {
// Enable purchases for specific defined item only on sunday
if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) )
$purchasable = false;
// Enable purchases for specific defined item only from 12h to 14h30
if( ! in_time_range() && in_array( $product->get_id(), time_range_products() ) )
$purchasable = false;
return $purchasable;
}
// Add a notice in specific products outside sundays
add_action( 'woocommerce_before_single_product', 'filter_before_single_product' );
function filter_before_single_product() {
global $product;
// For sundays product
if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) ) {
wc_print_notice( __( 'This product is only purchasable on sundays', 'woocommerce' ), 'error' );
}
// For hour range product
elseif( ! in_time_range() && in_array( $product->get_id(), time_range_products() ) ) {
wc_print_notice( __( 'This product is only purchasable between 12 AM and 2h30 PM', 'woocommerce' ), 'error' );
}
}
// IN CASE OF (but not needed): Cart and checkout validation + error message
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// Check cart items
if( ! is_sunday() && in_array( $cart_item['data']->get_id(), sunday_products() ) ){
wc_add_notice( sprintf(__("%s can be only purchase on sundays.", "woocommerce"), $cart_item['data']->get_name() ), 'error' );
break;
}
else if( ! in_time_range() && in_array( $cart_item['data']->get_id(), time_range_products() ) ){
wc_add_notice( sprintf(__("%s can be only purchase between 12 AM and 2h30 PM.", "woocommerce"), $cart_item['data']->get_name() ), 'error' );
break;
}
}
}
Код находится в файле function.php вашей активной дочерней темы (или активной темы).Это должно работать.