• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Woocommerce Force Sells/Bookings - Snippet to force number of person in a booking

piet

New member
Aug 27, 2019
5
0
1
Hi !

plugins :
  • woocommerce bookings
  • woocommerce force sells

I want to use WooCommerce Force Sells to add one force-sold item per number of person in a booking.

Woocommerce guys already provided a snippet to be added in functions.php which modifies the behaviour of Force Sells. With this snippet you can set a fixed quantity (1) of side product to be added :

PHP:
// only add one force sell item per product no matter how many of the original product are added
function my_wc_force_sell_add_to_cart_product( $product ){
    $product['quantity'] = 1;
    return $product;
}
add_filter( 'wc_force_sell_add_to_cart_product', 'my_wc_force_sell_add_to_cart_product' );

// when a synced force sell product is updated always set it to 1
function my_wc_force_sell_update_quantity( $quantity, $product ){
    return 1;
}
add_filter( 'wc_force_sell_update_quantity', 'my_wc_force_sell_update_quantity' );

What I would like to do :
Replacing the fixed quantity (1) with a function retrieving the number of people specified in the booking.


Any help would be awesome !

Pierre.
 
I think I found it !
PHP:
function my_wc_force_sell_add_to_cart_product( $product ){
    foreach(WC()->cart->get_cart() as $cart_item) {
    // The item persons count
    $person = array_sum( $cart_item['booking']['_persons'] );
}
    $product['quantity'] = $person;
    return $product;
}
add_filter( 'wc_force_sell_add_to_cart_product', 'my_wc_force_sell_add_to_cart_product' );

// when a synced force sell product is updated always set it to 1
function my_wc_force_sell_update_quantity( $quantity, $product ){
    return 1;
}
add_filter( 'wc_force_sell_update_quantity', 'my_wc_force_sell_update_quantity' );
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock