Ashanna
6/21/2018 - 7:05 AM

Check that all items in cart have the same date(s)

Check that all items in cart have the same date(s)

add_action( 'woocommerce_check_cart_items', 'wceb_check_dates_in_cart', 10, 1 );

function wceb_check_dates_in_cart() {

    $return = true;
    $start_date = '';
    $end_date = '';

    $i = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {

        if ( isset( $values['_ebs_start'] ) ) {

            $start = $values['_ebs_start'];
            $end = isset( $values['_ebs_end'] ) ? $values['_ebs_end'] : $start;

            if ( $i === 0 ) {
                $start_date = $start;
                $end_date = $end;
            } else {

                if ( $start === $start_date && $end === $end_date ) {
                    $i++;
                    continue;
                } else {
                    wc_add_notice( __( 'You must select the same dates for all your items.', 'woocommerce' ), 'error' );
                    return false;
                }

            }

            $i++;

        }
    }

    return $return;
}