2

I am using the following code to update the Cart page for Qty with Persons for Booking Products:

// 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;
}

But this code is not working and displays that error:

Notice: Undefined index: Persons in /home/www/wp-content/themes/my-child-theme/functions.php

Some help will be appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mehul Jain
  • 25
  • 4

1 Answers1

2

The correct way to get cart item persons count for a bookable product is to use:

$cart_item['booking']['_qty']

So in your code:

add_filter( 'woocommerce_cart_item_quantity', 'replace_cart_quantity_for_bookings', 20, 3 );
function replace_cart_quantity_for_bookings( $quantity, $cart_item_key, $cart_item ){
    // Only for bookable product items
    if( isset($cart_item['booking']) && isset($cart_item['booking']['_qty']) ){
        $quantity  = '<span style="text-align:center; display:inline-block; line-height:10px">'.$cart_item['booking']['_qty'].'<br>
        <small>(' . __('persons','woocommerce') . ')</small><span>';
    }

    return $quantity;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


Now to get the cart item count by person type for a bookable product is (gives an array):

$cart_item['booking']['_persons']

that will give an array like for 2 different person types in the case below:

Array (
        [872] => 2
        [873] => 2
    )
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • your solution worked perfectly fine and fixed the problem. I am coding something more to pick up the total cart quantity and hoping the code just doesn't replaces the quantity in the front end but also from the back end - it should because we are replacing the quantity meta. Will confirm that as well for others benefit. – Mehul Jain May 09 '20 at 06:05
  • unfortunately, its not replacing the quantity at the backend. Other functions like shipping and cart fee, are still treating the quantity to be 1 which is the default value for bookable product. – Mehul Jain May 17 '20 at 13:45
  • No it doesn't replace and this is another question/answer: The rule in stackOverFlow is one question at the time to avoid too broad questions, which are off-topic. The hook that you are using in your question is just affecting the quantity display and the main problem was to get the the persons quantity of one booking from a cart item which is `$cart_item['booking']['_qty']` instead of `$cart_item['booking']['Persons']`… – LoicTheAztec May 17 '20 at 23:26
  • 1
    Noted. I have raised a separate question question already for this. But i thought I'll close the discussion here as well for communities benefit. Thanks for the guidance. Cheers! – Mehul Jain May 18 '20 at 06:17
  • Hi, link to the other question is this if you could please help. https://stackoverflow.com/questions/61852727/replace-qty-in-cart-for-bookable-product-at-the-backend-with-persons-for-all-sub – Mehul Jain May 22 '20 at 11:14