@Pupkin_v1

Что заменить в коде что бы не было ошибки?

Здравствуйте, нашел код, который в определенное время запрещает оформление заказа к примеру с 10 вечера до ... Но включив дебаг обнаружил ошибки, соответственно функция не работает

// Custom conditional function that checks for parent product categories from a product category slug
function has_parent_term( $product_id, $category_slug ) {

    // Convert category term slug to term id
    $category_id   = get_term_by('slug', $category_slug, 'product_cat')->term_id;
    $parent_term_ids = array(); // Initializing

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id;
        }
    }
    return in_array( $category_id, array_unique($parent_term_ids) );
}

// Custom conditional function that checks from a time range
function is_on_time( $start_time, $end_time, $time_zone = 'UTC' ) {
    // Set the default time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set($time_zone);

    $from   = explode( ':', $start_time ); //
    $from_h = isset($from[0]) ? $from[0] : 0; // hours
    $from_m = isset($from[1]) ? $from[1] : 0; // minutes
    $from_s = isset($from[2]) ? $from[2] : 0; // seconds
    $start  = mktime( $from_h,  $from_m, $from_s, date("m"), date("d"), date("Y"));

    $to   = explode( ':', $end_time );
    $to_h = isset($to[0]) ? $to[0] : 0; // hours
    $to_m = isset($to[1]) ? $to[1] : 0; // minutes
    $to_s = isset($to[2]) ? $to[2] : 0; // seconds
    $end  = mktime( $to_h,  $to_m,  $to_s, date("m"), date("d"), date("Y"));

    $now   = strtotime("now");

    //return ( $start >= $now && $end < $now ) ? true : false;
	return ( $start >= $now && $end < $now && ! in_array( date('w'), array('6','0') ) ) ? true : false;
}

// Checking cart items and avoid checkout displaying an error notice
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $found = false; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Check for product category term and parent term
        if ( has_parent_term( $cart_item['product_id'], 't-shirts' ) )
            $found = true; // category is found
    }

    // Checking product category and time
    if ( $found && ! is_on_time( '8:00', '21:30', 'Europe/Moscow' ) ){
        // Avoiding checkout displaying a custom error notice
        wc_add_notice( __("Мы не принимаем заказы через сайт с 21.30 до 08.00.", "woocommerce" ), 'error' );
    }
}


Notice: Trying to get property 'term_id' of non-object
а так-же Notice: Undefined variable: category_id
  • Вопрос задан
  • 52 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы