Hello Syde,
Thank you for your response. On behalf of Noman, I am sharing the code below. Please feel free to let us know if you need any further information to help us resolve this challenge. Looking forward to your support.
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'include_addon_service_fees' ) );
/**
* Add fees for add on services.
*
* @param object $cart cart object for the particular order.
* @return void
*/
public function include_addon_service_fees( $cart ) {
...
$product = wc_get_product( $product_id );
if ( $product->is_type( 'simple' ) ) {
$value = intval( $product->get_meta( 'custom_property_id_field' ) );
$extra_charges_per_guest = intval( $product->get_meta( 'extra_charges_per_guest' ) );
$extra_starts_at = intval( $product->get_meta( 'extra_charges_starting_at' ) );
$quantity = $cart_item['quantity'];
break;
}
}
// Get discount amount based on coupon.
// Select the addons that have been checked by the user.
$selected_add_ons = array_filter(
$post_data,
function ( $key ) {
return strpos( $key, '_custom_checkbox_' ) === 0;
},
ARRAY_FILTER_USE_KEY
);
$adults = isset( $post_data['_number_of_adults'] ) ?
intval( $post_data['_number_of_adults'] ) :
0;
$total_guests = $adults + ( isset( $post_data['_number_of_kids'] ) ? intval( $post_data['_number_of_kids'] ) : 0 );
// Check if the number of adults is more than 2.
if ( $adults > 2 ) {
... ' Error message here '
return;
}
// Checks whether the prices include taxes or is exclusive of taxes.
$prices_include_tax = get_option( 'woocommerce_prices_include_tax' );
...
// Adds the extra guest fees based on the set value in the meta field ( extra_charges_starting_at )
// not in Smoobu as we were having troule getting this extra guest charges from Smoobu.
if (
! empty( $extra_charges_per_guest ) &&
! empty( $extra_starts_at ) &&
$total_guests >= $extra_starts_at
) {
// $guest_fee = ( $total_guests - $extra_starts_at + 1 ) * $extra_charges_per_guest * ( 1 - $discount_percentage );
$guest_fee = ( $total_guests - $extra_starts_at + 1 ) * $extra_charges_per_guest * $quantity;
...
...
$cart->add_fee( $guest_key, floatval( $guest_fee ), $taxable, $tax_class );
}
if ( ! empty( $selected_add_ons ) ) {
foreach ( $selected_add_ons as $key => $value ) {
$addon_key = substr( strrchr( $key, '_' ), 1 );
$add_on_price = floatval( sanitize_text_field( $post_data[ '_custom_addon_price_' . $addon_key ] ) );
$add_on_name = sanitize_text_field( $post_data[ '_custom_addon_name_' . $addon_key ] );
$add_on_type = sanitize_text_field( $post_data[ '_custom_addon_type_' . $addon_key ] );
$addon_tax_class = ( 'no' === $is_property_owned ) ? 'Zero rate' : 'Addon';
switch ( intval( $add_on_type ) ) {
case 0:
$total_fee = floatval( $add_on_price );
$multiplier = 1;
break;
case 1:
...
break;
case 2:
...
break;
case 3:
...
break;
case 4:
...
break;
case 5:
$guest_count = isset( $post_data[ '_include_kids_checkbox_' . $addon_key ] ) && $post_data[ '_include_kids_checkbox_' . $addon_key ] ?
intval( $total_guests ) :
intval( $adults );
$total_fee = $guest_count * floatval( $quantity ) * floatval( $add_on_price );
$multiplier = $guest_count * floatval( $quantity );
break;
}
// $total_fee *= ( 1 - $discount_percentage );
// Add the fee to the cart with the appropriate tax class.
$cart->add_fee( $multiplier . ' x ' . $add_on_name . '__' . $addon_key, floatval( $total_fee ), ! $taxable, $addon_tax_class );
}
}
}