仅在购物车项目中显示特定的 Woocommerce 预订元数据

Show only specific Woocommerce Bookings meta data in cart items

我喜欢 WooCommerce 和 WooCommerce 预订插件。在他们的文档中,有使用 "persons" 数量预订多个项目的解决方案。

那么 Bookings 插件的作用是,它向变体添加元数据,它还包括 "persons" 我们目前用作预订项目数量指标的内容。

我们接下来要做的是从产品变体中检索元数据 [Persons] 并将其显示为项目数量。

到目前为止我所做的是编辑 woocommerce 购物车模板并将其复制到我们的子主题中。 (我们不能使用挂钩,因为商店里没有可用的挂钩 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>

我需要弄清楚的是,如何过滤这些变体并只显示我需要的元数据?我只需要显示元数据 "persons".

下一步是仅过滤开始日期和结束日期以显示在其他元数据列中。

[编辑 18/02]

所以我发现我不能使用 "get_item_date",因为它不需要任何参数来显示特定值。

我尝试使用 "wc_display_item_meta",但我不知道如何 select 我需要的项目。

<!-- 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>

通过使用 vardump,我发现我需要的密钥是“['booking']['Persons']

也许有人可以帮我指出如何过滤所需项目的数组?

wc_display_item_meta() 函数 不能在购物车或购物车项目中使用 。它明确用于 WC_Order_Item,其中 $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() ) { /* … … */ }

所以这个功能对你的情况不起作用。


怎么做 (2ways):

1) 您可以使用两个自定义挂钩函数,这将删除除 'Persons' 之外的所有预订数据,并且只会删除您的可预订产品的数量。

// 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) 或者您可以删除所有预订显示的元数据并在数量字段中显示 "Persons":

// 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 文件中。

已测试并有效

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).