programing

예약 가능 날짜별 woocommerce 예약 쿼리

codeshow 2023. 3. 8. 21:38
반응형

예약 가능 날짜별 woocommerce 예약 쿼리

나는 Woocommerce 사이트에 Woothemes 예약을 사용한다.

제품의 커스텀 검색을 구축하기 위해 가용성도 검색해야 합니다.B에서 가용성을 관리하는 방법은 다음과 같습니다.o예약 플러그인 B.플러그인이 데이터베이스에 저장하는 방법

a:8:{i:0;a:4:{s:4:"type";s:4:"days";s:8:"bookable";s:3:"yes";s:4:"from";s:1:"3";s:2:"to";s:1:"3";}i:1;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-09";s:2:"to";s:10:"2015-07-09";}i:2;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-08";s:2:"to";s:10:"2015-07-08";}i:3;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-10";s:2:"to";s:10:"2015-07-10";}i:4;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-15";s:2:"to";s:10:"2015-07-15";}i:5;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-16";s:2:"to";s:10:"2015-07-16";}i:6;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-17";s:2:"to";s:10:"2015-07-17";}i:7;a:4:{s:4:"type";s:6:"months";s:8:"bookable";s:2:"no";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}}

워드프레스 내장 함수 get_post_meta에서 이러한 값을 가져올 수 있습니다.

$avail = get_post_meta($product->id, '_wc_booking_availability');

결과는 다음과 같습니다.

Array
(
[0] => Array
    (
        [type] => days
        [bookable] => yes
        [from] => 3
        [to] => 3
    )

[1] => Array
    (
        [type] => custom
        [bookable] => yes
        [from] => 2015-07-09
        [to] => 2015-07-09
    )

[2] => Array
    (
        [type] => custom
        [bookable] => yes
        [from] => 2015-07-08
        [to] => 2015-07-08
    )

[3] => Array
    (
        [type] => custom
        [bookable] => yes
        [from] => 2015-07-10
        [to] => 2015-07-10
    )

[4] => Array
    (
        [type] => custom
        [bookable] => yes
        [from] => 2015-07-15
        [to] => 2015-07-15
    )

[5] => Array
    (
        [type] => custom
        [bookable] => yes
        [from] => 2015-07-16
        [to] => 2015-07-16
    )

[6] => Array
    (
        [type] => custom
        [bookable] => yes
        [from] => 2015-07-17
        [to] => 2015-07-17
    )

[7] => Array
    (
        [type] => months
        [bookable] => no
        [from] => 8
        [to] => 8
    )

)

유저는 제품의 예약 가능 여부를 지정할 수 있기 때문에, 이 제품의 날짜 변수가 있는지 확인하기 위한 sql 쿼리를 어떻게 할 수 있는지, meta_query는 (아래와 같이) 작업을 할 수 있다고 생각합니다만, 사용할 수 없는 날짜는 어떻게 지정할 수 있습니까?당신은 어떻게 생각하시나요?

if($_GET['when']){

        /* checkIsAValidDate() >> check date format */
        if ( checkIsAValidDate( $_GET['when'] ) ){
            $quand = $_GET['when'];
            $available = array(
                "post_type" => "product",
                "meta_query" => array(
                    "relation"  => "AND",
                    "key"       => '_wc_booking_availability',
                    "value"     => $when,
                    'compare'   => 'IN'
                )
            );

        }
    }

메타 값이 배열이기 때문에 를 사용하지 못할 수 있습니다.사용하는 것은 동의하지만WP_Meta_Query보다 우아한 방법인 것 같습니다만, 이 함수는 항상 매우 혼란스럽고, 예약 가능한 오브젝트의 배열을 얻을 수 있기 때문에, 다음과 같은 함수를 쓰는 것은 간단하다고 생각합니다.

/**
 * Returns an array of bookable products according to supplied criteria
 * 
 * @param  int    $pid      the $product->ID of the bookable object
 * @param  mixed  $when     a date in YYYY-MM-DD format or integer representing a day or month
 * @param  string $type     a value of 'day', 'month', or 'custom'
 * @param  string $bookable whether the bookable object is bookable or not ('yes' or 'no')
 * @return array            an array of bookable objects for the product
 */
function getBookables( $pid, $when, $type, $bookable = 'yes' ) {
    $result = array();

    // is $when in the YYYY-MM-DD format?
    if ( 'custom' == $type ) {
        // it's a custom date so convert $when to DateTime
        $when = DateTime::createFromFormat( 'Y-m-d', $when );
    }

    $availability = get_post_meta( $pid, '_wc_booking_availability' );
    foreach ( $availability as $a ) {
        if ( $a[ 'bookable' ] == $bookable ) {
            // is it in the YYYY-MM-DD format?
            if ( $when instanceof DateTime ) {
                // it's a custom date so use date compare
                $from = DateTime::createFromFormat( 'Y-m-d', $a[ 'from' ] );
                $to   = DateTime::createFromFormat( 'Y-m-d', $a[ 'to'   ] );
                if ( $when >= $from && $when <= $to ) {
                    $result[] = $a;
                }
            } else {
                // it is an integer value (day or month)
                if ( $type == $a[ 'type' ] && ( $when >= $from && $when <= $to ) ) {
                    $result[] = $a;
                }
            }
        }
    }
    return $result;
}

아마 다음 단계를 통과해야 할 것입니다.type검색 기능을 사용할 수 있습니다.YYYY-MM-DD로부터int가치관을 구별하는 쉬운 방법은 없습니다.month부터day가치.따라서 위의 기능을 사용할 수 있습니다.

if ( $_GET[ 'when' ] && $_GET[ 'type' ] ) {
    $when = $_GET[ 'when' ];
    $type = $_GET[ 'type' ];
    $bookables = getBookables( $product->ID, $when, $type );
}

또한 스트링을 전달하면'no'함수의 네 번째 파라미터로 사용할 수 없는 시간 목록을 얻을 수 있습니다.

이게 도움이 됐으면 좋겠네요!

언급URL : https://stackoverflow.com/questions/30714222/query-woocommerce-booking-by-availability-date

반응형