问题
I amusing WooCommerce and with WooCommerce Bookings plugin. In their documentation there's this solution for booking multiple items by using the "persons" quantity.
So what the Bookings plugins does, it ads meta data to the variations, it also includes "persons" what we're currently using as an indicator for the amount of items being booked.
What we want to next is retrieve the meta data [Persons] from the product variations and display this as the item quantity.
What I've done so far is edit the woocommerce cart template and copied it to our child theme. (we can't use hooks since there's no hook available to use in the shop table).
</td>
<!-- Use [_persons] as product quantity -->
<td class="product-quantity--booking">
<?php
if ( ! empty( $cart_item['booking'] ) ) {
echo WC()->cart->get_item_data( $cart_item );
}
?>
</td>
What I need to figure out is, how do i filter these variations and only show the meta data I need? I would only need to show meta data "persons".
The next thing would be to filter only the start date and end date to display on the other meta data column.
[EDIT 18/02]
So I've found out that I can't use "get_item_date" since it does not take any arguments to show a specific value.
I tried to use "wc_display_item_meta", but I don't know how to select the item I need.
<!-- Use ['Persons'] as product quantity -->
<td class="product-quantity--booking">
<?php
if ( ! empty( $cart_item['booking'] ) ) {
$item_meta = wc_display_item_meta( $cart_item['booking']['Persons'])
echo $item_meta;
}
?>
</td>
By using the vardump I found the key I need is "['booking']['Persons']
"
Maybe someone could help me to point out how to filter the array for needed items?
回答1:
The wc_display_item_meta() function is not to be used in Cart or with cart items. It's explicitly for WC_Order_Item
where $item
argument is an Order Item.
You will see in the source code:
/**
* 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() ) { /* … … */ }
So this function will not work in your case.
How to do it (2ways):
1) You can use two custom hooked functions, that will remove all booking data except 'Persons' and that will remove the quantity for your bookable products only.
// 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;
}
This code goes on function.php file of your active child theme (or theme).
Tested and works.
2) Or you can remove all booking displayed meta data and display the "Persons" in the quantity field:
// 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;
}
This code goes on function.php file of your active child theme (or theme).
Tested and works
If you manage only booking products, you could rename "Quantity" column label to "Persons" in the template cart/cart.php (overriding it via your active theme).
来源:https://stackoverflow.com/questions/48840412/show-only-specific-woocommerce-bookings-meta-data-in-cart-items