Support » Plugin: Booking Activities » Use one booking form for all products, but only display 1 product in calendar

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author yoancutillas

    (@yoancutillas)

    Hello,

    No, sorry, you can’t do that without code.
    I have noted the feature request at that time, and unfortunately I have had no other requests since then, but I add your vote, thank you!

    The best option is to use the code in the topic you referred. I can adapt it for multiple “master forms” if you are interested.

    Regards,
    Yoan Cutillas

    Thread Starter MichaelNi

    (@michaelni)

    Hi Yoan,

    Sorry for the late reply.

    I feel like the easiest way to solve it would be as follows:

    When viewing a booking form within the product page, only the activity that is associated to this product is shown in the calendar. All other activities are filtered out automatically.

    This would allow a non-technical user to simply create 3 or 4 master booking forms (depending on the activities), and the “activity filtering magic” happens automatically.

    Is this realisable with your plugin?

    Best,
    Michael

    Plugin Author yoancutillas

    (@yoancutillas)

    Hello Michael,

    It is, but only with custom code.

    You can hide activities according to the selected product / product variation with that hook: ‘bookacti_form_field_calendar_attributes’ in controller-forms.php line 35.

    // Display certain activities on the calendar according to the bound product
    function my_theme_display_activities_according_to_product( $field, $instance_id, $context ) {	
    	// Get product ID
    	if( $context === 'wc_product_init' && substr( $instance_id, 0, 8 ) === 'product-' ) {
    		$product_id = intval( substr( $instance_id, 8 ) );
    	} else if( $context === 'wc_switch_variation' && substr( $instance_id, 0, 18 ) === 'product-variation-' ) {
    		$product_id = intval( substr( $instance_id, 18 ) );
    	}
    	if( empty( $product_id ) ) { return $field; }
    	
    	// Option #1: Set your product_id => activity_id correspondence array manually
    	$activity_by_product = array(
    		641 => 14,
    		2 => 5,
    	);
    	
    	// Option #2: Or use the "Redirect to a product page" array for that (in "Calendar" field settings > "Action" tab > form action = "Redirect to a product page"
    //	if( ! empty( $field[ 'product_by_activity' ] ) ) {
    //		$activity_by_product = array_flip( $field[ 'product_by_activity' ] );
    //	}
    	
    	if( $product_id && ! empty( $activity_by_product[ $product_id ] ) ) {
    		$field[ 'activities' ] = array( $activity_by_product[ $product_id ] );
    	}
    	
    	return $field;
    }
    add_filter( 'bookacti_form_field_calendar_attributes', 'my_theme_display_activities_according_to_product', 20, 3 );

    This is only for display, so there are no server checks, but this may be enough unless you are selling events for hackers :).

    Regards,
    Yoan Cutillas

    Thread Starter MichaelNi

    (@michaelni)

    Hello Yoan,

    Thank you for your quick reply.

    Unfortunately, managing an array in PHP is very cumbersome for my customer.

    I have instead implemented a different solution based on your template code – thank you very much for it.

    For anyone looking for an alternative: The following code filters activities based on the product name. If a product name matches exactly one activity name, only this activity is shown in the calendar on the product page. If 0 or more than 2 matching events are found, all events in the respective calendar are shown.

    // Display certain activities on the calendar according to the bound product
    function eb_display_filter_activities_by_product_name( $field, $instance_id, $context ) {	
    
    	global $wpdb;
    
    	// Only apply this if a booking form is shown on a product page
    	if(!is_product()) {
    		return $field;
    	}
    
    	global $product;
    
    	// Ensure a product name is available
    
    	if(empty($product->get_name())) { 
    		return $field; 
    	}
    
    	// Get activity ID that matches the exact name of the product
    	$activity_id_query = $wpdb->prepare(
    		"SELECT activity_id FROM {$wpdb->prefix}bookacti_events
    		WHERE UPPER(title) = UPPER(%s) 
    		GROUP BY activity_id",
    		$product->get_name()
    	);
    
    	$found_activity_ids = $wpdb->get_results($activity_id_query);
    
    	// Only filter if exactly one match is found
    	if(count($found_activity_ids) != 1) {
    		return $field;
    	}
    
    	$field[ 'activities' ] = array($found_activity_ids[0]->activity_id);
    
    	// return the only entry possible
    	return $field;
    }
    add_filter( 'bookacti_form_field_calendar_attributes', 'eb_display_filter_activities_by_product_name', 20, 3 );
    

    Best,
    Michael

    • This reply was modified 1 year, 4 months ago by MichaelNi.
    Plugin Author yoancutillas

    (@yoancutillas)

    Hello Michael,
    Thank you very much for sharing!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Use one booking form for all products, but only display 1 product in calendar’ is closed to new replies.