Shared availability between products within the same category

Issue #922 resolved
Valentina R created an issue

Example of how it would be used:

Category 1 :
Product A, Product B, Product C
for each product I have : staffMember1, staffMember2, staffMember3

Category 2 :
Product C, Product D, Product E
for each product I have : staffMember1, staffMember2, staffMember3

the 17/11/2021 at 12:00, a client schedules Product A. It’s OK. Automatically with staffMember1.
the 17/11/2021 at 12:00, the slot for Product A, Product B, Product C is no more possible. Because there are in the same category.
the 17/11/2021 at 12:00, a client can schedules Product C. It’s Ok because product C is in another category and no product of it has been already scheduled. Automatically with staffMember2 (staffMember1 has been scheduled with product A).

Requested here.

Comments (1)

  1. Valentina R reporter

    Can be done with this code snippet:

    // Make sure common capacity is shared between common products.
    add_filter( 'wc_apointments_check_appointment_product', 'common_capacity_between_products', 10, 3 );
    function common_capacity_between_products( $return, $appointment_id, $product_id ) {
        $appointment            = get_wc_appointment( $appointment_id );
        $appointment_product_id = (int) $appointment->get_product_id();
        $common_product_ids     = array( 291, 340, 582, 583); #product IDs with shared capacity.
    
        if ( $appointment_product_id !== $product_id && in_array( $product_id, $common_product_ids ) ) {
            return false;
        }
    
        return $return;
    };
    
    // Include all common product IDs inside appointment query.
    add_filter( 'woocommerce_appointments_in_date_range_query_args', 'common_product_ids_in_date_range_query_args' );
    add_filter( 'woocommerce_appointments_for_objects_query_args', 'common_product_ids_in_date_range_query_args' );
    function common_product_ids_in_date_range_query_args( $args ) {
        if ( isset( $args['product_id'] ) && ! empty( $args['product_id'] ) ) {
            $product_ids        = is_array( $args['product_id'] ) ? $args['product_id'] : array( $args['product_id'] );
            $common_product_ids = array( 291, 340 ); #product IDs with shared capacity.
            $common_product_ids_2 = array( 582, 583 ); #product IDs with shared capacity.
    
            // Check if queried product ID has shared capacity.
            $common_result = array_intersect( $product_ids, $common_product_ids );
            if ( $common_result ) {
                $args['product_id'] = array_unique( array_merge( $product_ids, $common_product_ids ) );
            }
    
            // Check if queried product ID has shared capacity.
            $common_result_2 = array_intersect( $product_ids, $common_product_ids_2 );
            if ( $common_result_2 ) {
                $args['product_id'] = array_unique( array_merge( $product_ids, $common_product_ids_2 ) );
            }
        }
    
        return $args;
    }
    
  2. Log in to comment