该wc_display_item_meta()
功能不能用于购物车或购物车项目。它明确表示WC_Order_Item
where$item
参数是 Order Item。
您将在源代码中看到:
/**
* Display item meta data.
*
* @since 3.0.0
* @param WC_Order_Item $item Order Item.
* @param array $args Arguments.
* @return string|void
*/
function wc_display_item_meta( $item, $args = array() ) { /* … … */ }
因此,此功能不适用于您的情况。
怎么做(2种方式):
1) 您可以使用两个自定义挂钩函数,这将删除除“人员”之外的所有预订数据,并且将仅删除您的可预订产品的数量。
// Display only 'Persons' for bookable products
add_filter( 'woocommerce_get_item_data', 'display_only_persons_for_bookings', 20, 2 );
function display_only_persons_for_bookings( $cart_data, $cart_item ){
// Check that is a bookable product and that is Cart page
if( ! isset($cart_item['booking']) || ! is_cart() ) return $cart_data; // Exit
// Loop through this cart item data
foreach($cart_data as $key =>$values){
// Checking that there is some data and that is Cart page
if( ! isset($values['name']) || ! is_cart() ) return $cart_data; // Exit
// Removing ALL displayed data except "Persons"
if( $values['name'] != __('Persons','woocommerce') ){
unset($cart_data[$key]);
}
}
return $cart_data;
}
// Remove cart quantity for bookable products
add_filter( 'woocommerce_cart_item_quantity', 'remove_cart_quantity_for_bookings', 20, 3 );
function remove_cart_quantity_for_bookings( $product_quantity, $cart_item_key, $cart_item ){
// Check that is a bookable product
if( isset($cart_item['booking']) )
$product_quantity = '';
return $product_quantity;
}
此代码位于您的活动子主题(或主题)的 function.php 文件中。
测试和工作。
2) 或者您可以删除所有预订显示的元数据并在数量字段中显示“人员”:
// Remove displayed cart item meta data for bookable products
add_filter( 'woocommerce_get_item_data', 'remove_cart_data_for_bookings', 20, 2 );
function remove_cart_data_for_bookings( $cart_data, $cart_item ){
// Check that is a bookable product and that is Cart page
if( ! isset($cart_item['booking']) || ! is_cart() ) return $cart_data; // Exit
// Loop through this cart item data
foreach($cart_data as $key =>$values){
// Removing ALL displayed data
unset($cart_data[$key]);
}
return $cart_data;
}
// Add "Persons" to replace cart quantity for bookable products
add_filter( 'woocommerce_cart_item_quantity', 'replace_cart_quantity_for_bookings', 20, 3 );
function replace_cart_quantity_for_bookings( $product_quantity, $cart_item_key, $cart_item ){
// Check that is a bookable product
if( isset($cart_item['booking']) ){
$product_quantity = '<span style="text-align: center; display:inline-block;">'.$cart_item['booking']['Persons'].'<br>
<small>(' . __('persons','woocommerce') . ')</small><span>';
}
return $product_quantity;
}
此代码位于您的活动子主题(或主题)的 function.php 文件中。
测试和工作
如果您只管理预订产品,您可以在模板 cart/cart.php 中将“数量”列标签重命名为“人员”(通过您的活动主题覆盖它)。