In the rooms_booking_manager.module I saw a function called rooms_price_modifier can you someone assist me on how to use that hook from another module?

Comments

Helgi Jónsson’s picture

When invoking the hook two $booking_info arrays are outputed, one for the current date and one for the chosen booking parameters. Is it supposed to be that way?

<?php
hook_rooms_price_modifier_alter($price_modifiers, $booking_info) {
  dpm($booking_info);
}
?>
Helgi Jónsson’s picture

StatusFileSize
new63.19 KB
new83.95 KB

Hi again :)

I have a booking scenario where a client wants to charge a different price for the first night and then a lower price for additional nights. So I set out to use hook_rooms_price_modifier_alter($price_modifiers, $booking_info) and I was able to achieve the desired outcome but since the 1.0 version I get a lot of errors when adjusting the base price of a given room type based on the $booking_info. It makes me wonder what I am doing wrong or if there is a new bug, in any case I am reporting my findings here. You will find two screenshots of the results of the code below on a commerce + rooms sandbox.

Here is my custom module code:

<?php
	
	function custom_module_rooms_price_modifier_alter($price_modifiers, $booking_info) {

			
		// Fetching the room type.
		$room_type = $booking_info['unit']->type;
		
		// Declaring various room types to check for.
		$cabin = 'cabin';
		$bedroom = 'room_double';
		
		// Fetching the booking parameter of booking dates.
		$start_date = $booking_info['start_date'];
		$end_date = $booking_info['end_date'];
		
		$begin_month = $start_date->format('m');
		$end_month = $end_date->format('m');
		
		// Declaring the additional costs of various room types.
		$additional_nights_cost_cabin = 85;
		$additional_nights_cost_cabin_high_season = 105;
		
		$additional_nights_cost_bedroom = 50;
		$additional_nights_cost_bedroom_high_season = 80;
		
		// Calculating number of nights from booking parameter dates.
		$nights = $end_date->diff($start_date)->days;
		
		if ($room_type == $cabin && $end_month <= 5) {
			// Calculating the difference of cost of various room types in relation to default base price.
			$difference_nights_cost_cabin = $booking_info['unit']->base_price-$additional_nights_cost_cabin;
			
			// Calculating the percentage difference of additional nights versus first night.
			$percentage_cabin = $additional_nights_cost_cabin/$booking_info['unit']->base_price;
			
			// Calculating the difference of additional nights versus first night per night.
			$difference_nights_cost_cabin_per_night = $difference_nights_cost_cabin/$nights;
			
			$new_base_price = ($booking_info['unit']->base_price*$percentage_cabin)+$difference_nights_cost_cabin_per_night;
			
			$booking_info['unit']->base_price = $new_base_price;
			
		} elseif ($room_type == $cabin && $end_month >= 6) {
			// Calculating the difference of cost of various room types in relation to default base price.
			$difference_nights_cost_cabin = $room_price-$additional_nights_cost_cabin_high_season;
			
			// Calculating the percentage difference of additional nights versus first night.
			$percentage_cabin = $additional_nights_cost_cabin_high_season/$room_price;
			
			// Calculating the difference of additional nights versus first night per night.
			$difference_nights_cost_cabin_per_night = $difference_nights_cost_cabin_high_season/$nights;
			
			$new_base_price = ($room_price*$percentage_cabin)+$difference_nights_cost_cabin_per_night;
			
			$booking_info['unit']->base_price = $new_base_price;
		}
		
		if ($room_type == $bedroom && $end_month <= 5) {
			// Calculating the difference of cost of various room types in relation to default base price.
			$difference_nights_cost_bedroom = $booking_info['unit']->base_price-$additional_nights_cost_bedroom;
			
			// Calculating the percentage difference of additional nights versus first night.
			$percentage_bedroom = $additional_nights_cost_bedroom/$booking_info['unit']->base_price;
			
			// Calculating the difference of additional nights versus first night per night.
			$difference_nights_cost_bedroom_per_night = $difference_nights_cost_bedroom/$nights;
			
			$new_base_price = ($booking_info['unit']->base_price*$percentage_bedroom)+$difference_nights_cost_bedroom_per_night;
			
			$booking_info['unit']->base_price = $new_base_price;
			
		} elseif ($room_type == $bedroom && $end_month >=6) {
			// Calculating the difference of cost of various room types in relation to default base price.
			$difference_nights_cost_bedroom = $room_price-$additional_nights_cost_bedroom_high_season;
			
			// Calculating the percentage difference of additional nights versus first night.
			$percentage_bedroom = $additional_nights_cost_bedroom_high_season/$room_price;
			
			// Calculating the difference of additional nights versus first night per night.
			$difference_nights_cost_bedroom_per_night = $difference_nights_cost_bedroom_high_season/$nights;
			
			$new_base_price = ($room_price*$percentage_bedroom)+$difference_nights_cost_bedroom_per_night;
			
			$booking_info['unit']->base_price = $new_base_price;
			
		}
	}

The price options don't display the new base price. But when the module worked with the dev release prior to 1.0 the base prices were all in sync and the booking cart displayed correctly as well.

I get several errors after performing "place booking". Here are those errors:

Warning: array_shift() expects parameter 1 to be array, null given in book_units_per_type_form_submit() (line 1427 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking_manager/rooms_booking_manager.module).
Notice: Trying to get property of non-object in book_units_per_type_form_submit() (line 1440 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking_manager/rooms_booking_manager.module).
Notice: Trying to get property of non-object in rooms_create_line_item() (line 13 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking_manager/rooms_booking_manager.commerce.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /Users/helgi/Sites/commerce/sites/all/modules/entity/includes/entity.controller.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 355 of /Users/helgi/Sites/commerce/includes/entity.inc).
Notice: Trying to get property of non-object in rooms_create_line_item() (line 18 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking_manager/rooms_booking_manager.commerce.inc).
Notice: Trying to get property of non-object in rooms_create_line_item() (line 30 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking_manager/rooms_booking_manager.commerce.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /Users/helgi/Sites/commerce/sites/all/modules/entity/includes/entity.controller.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 355 of /Users/helgi/Sites/commerce/includes/entity.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 241 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /Users/helgi/Sites/commerce/sites/all/modules/entity/includes/entity.controller.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 355 of /Users/helgi/Sites/commerce/includes/entity.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 244 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /Users/helgi/Sites/commerce/sites/all/modules/entity/includes/entity.controller.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 355 of /Users/helgi/Sites/commerce/includes/entity.inc).
Notice: Trying to get property of non-object in UnitCalendar->__construct() (line 20 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_availability/includes/rooms_availability.unit_calendar.inc).
Notice: Trying to get property of non-object in laekjarkot_rooms_price_modifier_alter() (line 7 of /Users/helgi/Sites/commerce/sites/all/modules/laekjarkot/laekjarkot.module).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 265 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /Users/helgi/Sites/commerce/sites/all/modules/entity/includes/entity.controller.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 355 of /Users/helgi/Sites/commerce/includes/entity.inc).
Notice: Trying to get property of non-object in UnitPricingCalendar->__construct() (line 29 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc).
Notice: Trying to get property of non-object in UnitPricingCalendar->__construct() (line 30 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc).
Notice: Trying to get property of non-object in UnitPricingCalendar->calculatePrice() (line 71 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 275 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 275 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 276 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 276 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 277 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 277 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 283 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in AvailabilityAgent->getUnitsByPriceType() (line 283 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking/includes/rooms_booking.availability_agent.inc).
Notice: Trying to get property of non-object in rooms_create_line_item() (line 60 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking_manager/rooms_booking_manager.commerce.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /Users/helgi/Sites/commerce/sites/all/modules/entity/includes/entity.controller.inc).
Notice: Trying to get property of non-object in laekjarkot_rooms_price_modifier_alter() (line 7 of /Users/helgi/Sites/commerce/sites/all/modules/laekjarkot/laekjarkot.module).
Notice: Trying to get property of non-object in rooms_booking_manager_price_apply() (line 2047 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_booking_manager/rooms_booking_manager.module).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /Users/helgi/Sites/commerce/sites/all/modules/entity/includes/entity.controller.inc).
Notice: Trying to get property of non-object in UnitPricingCalendar->__construct() (line 29 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc).
Notice: Trying to get property of non-object in UnitPricingCalendar->__construct() (line 30 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc).
Notice: Trying to get property of non-object in UnitPricingCalendar->calculatePrice() (line 71 of /Users/helgi/Sites/commerce/sites/all/modules/rooms/modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc).

Really looking forward on a response. :)

Best Regards,
Helgi.

ronald_istos’s picture

Hi Helgi,

could you do a dpm of both $price_modifiers and $booking_info and paste here - this way I can try and help more.

Also, please keep in mind that the idea of the price_modifiers is to "inject" changes via a price modifier, not to actually change the $booking_info information.

For example if you want to increase the price you can do:

 $price_modifiers['my_modifer'] = array(
                      '#type' => ROOMS_DYNAMIC_MODIFIER,
                      '#op_type' => add,
                      '#amount' => 10,
                    );

This would increase the price by 10. The price modifier operation is applied by Rooms. Instead what you are doing seems to be changing the base price.

Helgi Jónsson’s picture

Hi,

Thanks for your reply. I will choose the price modifier path instead. If I get to any trouble I will post here again. I also have further questions about $booking_info.

I am also currently working on a commerce payment module to use for the rooms module that pulls the customer profile info, line-item description & credit card information and sends it to interfax.net and their api. Their secure lounge is level 1 pci & dss environment and stores the information for 30 days. My module is a fax implementation, so that a hotel can get the order with booking information faxed and then they process the payment themselves.

This is quite important because Hotels want to confirm bookings before they capture the payment. Its possible to authorize first and capture payment later via paypal but in my case for Iceland, they transfer the money directly on to credit cards, not via bank transfers.

I tried to implement the hook_rooms_price_modifier_alter within the payement module to access the $booking_info in order to query information such as number of persons and more. I am hoping to release the module soon but this is still a critical issue at the moment. I hope that there is a simple solution to my problem.

Best Regards,
Helgi.

dreamsri’s picture

Hi,
I am used your comments but it ll not affect form actual price so please help me to fix this please very urgent and thank you.

Your Code:
$price_modifiers['my_modifer'] = array(
'#type' => ROOMS_DYNAMIC_MODIFIER,
'#op_type' => add,
'#amount' => 10,
);