gpt4 book ai didi

php - 按可用日期查询 woocommerce 预订

转载 作者:可可西里 更新时间:2023-11-01 06:33:54 26 4
gpt4 key购买 nike

我使用 Woothemes booking对于我的 woocommerce 网站

当我为产品建立自定义搜索时,我还必须搜索可用性,这是我应该如何管理来自 B.O Booking plugin B.O 的可用性以及插件如何存储到数据库

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";}}

我可以从 wordpress 内置函数 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'
)
);

}
}

最佳答案

由于元值是一个数组,it may not be possible to use WP_Meta_Query .虽然我同意使用 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 值,没有简单的方法来区分 monthday 值。因此,您可以使用上述功能:

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

另请注意,如果您将字符串'no' 作为第四个参数传递给该函数,您可以获得不可用 时间的列表。

希望这对您有所帮助!

关于php - 按可用日期查询 woocommerce 预订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com