我只需要在特定的帖子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;
}
答案 0 :(得分:1)
!important
参数包含您想要添加到购物车的产品的所有详细信息。
#main-nav .navbar-toggle...
所以你的代码应该是这样的。
$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
替换为您的特定帖子ID。
答案 1 :(得分:0)
根据执行此代码的位置,这应该有效:
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的额外检查。在这里,您可以将YOURUNIQUEIDHERE替换为您想要的任何帖子的ID。希望这会帮助你!