仅显示购物车项目中的特定Woocommerce预订元数据 [英] Show only specific Woocommerce Bookings meta data in cart items

查看:65
本文介绍了仅显示购物车项目中的特定Woocommerce预订元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很开心WooCommerce和WooCommerce Bookings插件。在他们的文档中,有一种使用人员数量来预订多个项目的解决方案。



因此,Bookings插件的作用是,将元数据投放到变体中,同时包括人,这是我们当前用来指示已预订商品数量的指标。



我们接下来要检索的是元数据[Persons] from



到目前为止,我所做的是编辑woocommerce购物车模板并将其复制到我们的子主题中。 (我们无法使用钩子,因为在商店表中没有可用的钩子。)

 < / td> 
<!-使用[_persons]作为产品数量->
< td class = product-quantity--booking>
<?php
if(!empty($ cart_item [’booking’])){
echo WC()-> cart-> get_item_data($ cart_item);
}
?>
< / td>

我需要弄清楚的是,如何过滤这些变化并仅显示元数据需要?我只需要显示元数据人。





接下来的事情是仅过滤开始日期和结束日期以显示在另一个元数据列上。





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



我尝试使用 wc_display_item_meta,但我不知道如何选择所需的项目

 <!-使用['Persons']作为产品数量->。 
< 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']



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

解决方案






2)或者您可以删除所有预订显示的元数据,并在数量字段中显示人:

  / /删除显示的可预订产品
的购物车商品元数据add_filter('woocommerce_get_item_data','remove_cart_data_for_bookings',20,2);
函数remove_cart_data_for_bookings($ cart_data,$ cart_item){
//检查是否为可预订产品,即购物车页面
if(!isset($ cart_item ['booking']))|| !is_cart())返回$ cart_data; //退出
//遍历该购物车项目数据
foreach($ cart_data as $ key => $ values){
//删除所有显示的数据
unset($ cart_data [$ key]);
}
返回$ cart_data;
}

//添加人员以替换可预订产品的购物车数量
add_filter(‘woocommerce_cart_item_quantity’,‘replace_cart_quantity_for_bookings’,20,3);
函数replace_cart_quantity_for_bookings($ product_quantity,$ cart_item_key,$ cart_item){
//检查是否为可预订产品
if(isset($ set _ $$ art_item ['booking'])){
$ product_quantity ='< span style = text-align:center; display:inline-block;>'。$ cart_item ['booking'] ['Persons']。'< br>
< small>('。__('persons','woocommerce')。’)< / small>< span>';;
}
返回$ product_quantity;
}

此代码位于活动子主题的function.php文件中(或主题)。



经过测试并可以工作


如果您只管理预订产品,则可以在模板cart / cart.php模板中将数量(Quantity)列标签重命名为人(Persons)(通过活动主题对其进行覆盖)。



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?

解决方案

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

这篇关于仅显示购物车项目中的特定Woocommerce预订元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆