我有这个代码
<?php
$dateTimeStart = new DateTime(tribe_get_start_date( null, false, 'Y-m-d' ));
$dateTimeEnd = new DateTime(tribe_get_start_date( null, false, 'Y-m-d' ));
$bookings = WC_Bookings_Controller::get_bookings_in_date_range(
$dateTimeStart->getTimestamp(),
$dateTimeEnd->getTimestamp(),
'',
false
);
$booked[] = 0;
foreach ($bookings as $booking) {
$booked[] = $booking->product_id;
}
$args = array (
'post__in' => $booked,
'post_type' => ['product'],
);
$query = new WP_Query( $args );
$posts = $query->posts;
$sku = $product->get_sku();
wp_reset_query();
?>
从上述查询中提取SKU的正确方法是什么?
我尝试过
<?php echo esc_html( get_post_meta( get_the_ID(), '_sku', true ) ); ?>
该查询适用的
<?php
$args = array( 'post_type' => 'product', 'terms' => array('fvip-ddb','fvip-sdb'), 'posts_per_page' => -1, 'meta_key' => '_regular_price' );
$loop = new WP_Query( $args );
$sku = get_post_meta( $item['product_id'], '_sku', true );
?>
但对于第一个查询不起作用(显示为空白)
更新
在运行此代码时,使用下面的代码:
<?php foreach($posts as $post) { echo "{ toolTip: \"Say something here.\", key : \"$skubooked\", staticState: true },"; } ?>
由于某种原因,循环将相同的值返回3次。 (有三种不同的产品预订..但循环显示同一产品三遍)。
答案 0 :(得分:1)
有多种获取产品SKU的方法:
get_post_meta()
和meta键_sku
WC_Product
对象上使用get_sku()
方法WC_product
您的查询应类似于以下代码:
$query = new WP_Query( array(
'post_type' => 'product',
// 'post_status' => 'publish',
'posts_per_page' => -1 ,
'post__in' => $booked,
) );
if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post();
$sku = get_post_meta( get_the_ID(), '_sku', true );
endwhile; wp_reset_query(); endif;
或
$query = new WP_Query( array(
'post_type' => 'product',
// 'post_status' => 'publish',
'posts_per_page' => -1 ,
'post__in' => $booked,
) );
if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post();
// get an instance of the WC_product object
$product = wc_get_product( get_the_ID() );
$sku = $product get_sku();
endwhile; wp_reset_query(); endif;