wooccommerce预订会修改日期输出,以包含每个产品或类别的自定义文本


woocommerce-bookings modify date output to include custom text per product or category

我只需要在特定的帖子id上添加/触发这个过滤器。

add_filter( 'woocommerce_get_item_data', 'woocommerce_cart_item_product', 10, 3 );
function woocommerce_cart_item_product( $item_data, $cart_item){
if ( ! empty( $cart_item['booking'] ) ) {
    $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
    $time_format = apply_filters( 'woocommerce_bookings_time_format', ', ' . wc_time_format() );
    $end_date = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $date_format, $cart_item['booking']['_end_date'] ) );
    $item_data[] = array(
        'key'    => __( 'for the week of', 'your-textdomain' ),
        'value'   => $cart_item['booking']['_end_date'],
        'display' => $end_date,
    );
}
return $item_data;

}

$cart_item参数具有您想要的关于要添加到购物车中的产品的所有详细信息。

$cart_item[ 'product_id' ] // Product Id ( the product that is being added to cart )
$cart_item[ 'variation_id' ] // Variable product's id  ( if that product is a variable )
$cart_item[ 'variation' ] // Variation array ( if that product is a variable )
$cart_item[ 'quantity' ] // Line Item quantity
$cart_item[ 'data' ] // Post object of the product that is being added

所以你的代码应该是这样的。

add_filter( 'woocommerce_get_item_data', 'woocommerce_cart_item_product', 10, 2 );
function woocommerce_cart_item_product( $item_data, $cart_item ) {
    if ( !empty( $cart_item['booking'] ) && $cart_item["product_id"] == "Your Product Id" ) {
        $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
        $time_format = apply_filters( 'woocommerce_bookings_time_format', ', ' . wc_time_format() );
        $end_date = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $date_format, $cart_item['booking']['_end_date'] ) );
        $item_data[] = array(
            'key'    => __( 'for the week of', 'your-textdomain' ),
            'value'   => $cart_item['booking']['_end_date'],
            'display' => $end_date,
        );
    }
    return $item_data;
}

"Your Product Id"替换为您的特定帖子id。

根据此代码的执行位置,它应该起作用:

    add_filter( 'woocommerce_get_item_data', 'woocommerce_cart_item_product', 10, 3 );
function woocommerce_cart_item_product( $item_data, $cart_item) {
    global $post;
    if ( ! empty( $cart_item['booking'] ) && $post->ID == YOURUNIQUEIDHERE ) {
        $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
        $time_format = apply_filters( 'woocommerce_bookings_time_format', ', ' . wc_time_format() );
        $end_date = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $date_format, $cart_item['booking']['_end_date'] ) );
        $item_data[] = array(
            'key'    => __( 'for the week of', 'your-textdomain' ),
            'value'   => $cart_item['booking']['_end_date'],
            'display' => $end_date,
        );
    }
    return $item_data;
}

我在您的if语句中添加了一个额外的邮政ID检查。在这里,你可以用你想要的任何帖子的ID替换YOURUNIQUEIDHERE。希望这能帮到你!