1

我有这个代码

<?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 次。(有三个不同的产品预订..但循环显示相同的产品三次)..

4

1 回答 1

1

获取产品 SKU 的方法有多种:

  1. get_post_meta()使用带有元键的WordPress_sku
  2. 在对象上使用该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;
于 2018-06-28T18:06:15.753 回答