0

I have the following function that is overriding some of the values that are saved to the Cart and Checkout, via the YITH Bookings plugin. Essentially, I have overridden the Duration field with my own value, which is shown in the Cart and Checkout, but I need to also update this value in the Order Item Meta, so that the new value shows in all the Order areas too.

Here is the function used to save the values to the cart and checkout:

// SAVE the product meta to CART item (does not yet display it though)
add_filter( 'woocommerce_add_cart_item_data', 'fs_save_my_custom_product_field', 10, 2 );
function fs_save_my_custom_product_field( $cart_item_data, $product_id ) {
    $port = get_field( 'port', $product_id, true );
    $fs_duration_monday     = get_field( 'custom_duration_monday', $product_id, true );
    $fs_duration_tuesday    = get_field( 'custom_duration_tuesday', $product_id, true );
    $fs_duration_wednesday  = get_field( 'custom_duration_wednesday', $product_id, true );
    $fs_duration_thursday   = get_field( 'custom_duration_thursday', $product_id, true );
    $fs_duration_friday     = get_field( 'custom_duration_friday', $product_id, true );
    $fs_duration_saturday   = get_field( 'custom_duration_saturday', $product_id, true );
    $fs_duration_sunday     = get_field( 'custom_duration_sunday', $product_id, true );

    if( !empty( $port ) ) {
        $cart_item_data['port'] = $port;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    if( !empty( $fs_duration_monday ) ) {
        $cart_item_data['custom_duration_monday'] = $fs_duration_monday;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    if( !empty( $fs_duration_tuesday ) ) {
        $cart_item_data['custom_duration_tuesday'] = $fs_duration_tuesday;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    if( !empty( $fs_duration_wednesday ) ) {
        $cart_item_data['custom_duration_wednesday'] = $fs_duration_wednesday;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    if( !empty( $fs_duration_thursday ) ) {
        $cart_item_data['custom_duration_thursday'] = $fs_duration_thursday;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    if( !empty( $fs_duration_friday ) ) {
        $cart_item_data['custom_duration_friday'] = $fs_duration_friday;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    if( !empty( $fs_duration_saturday ) ) {
        $cart_item_data['custom_duration_saturday'] = $fs_duration_saturday;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    if( !empty( $fs_duration_sunday ) ) {
        $cart_item_data['custom_duration_sunday'] = $fs_duration_sunday;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

Now this function is actually displaying the new values in the Cart and Checkout:

add_filter( 'woocommerce_get_item_data', 'fs_woocommerce_get_item_data', 11, 2 );
function fs_woocommerce_get_item_data( $item_data, $cart_item ) {
    $product_id = isset( $cart_item[ 'product_id' ] ) ? $cart_item[ 'product_id' ] : 0;
    if ( YITH_WCBK_Product_Post_Type_Admin::is_booking( $product_id ) ) {
        /**  @var WC_Product_Booking $product */
        $product = wc_get_product( $product_id );

        $fs_duration_monday     = $cart_item['custom_duration_monday'];
        $fs_duration_tuesday    = $cart_item['custom_duration_tuesday'];
        $fs_duration_wednesday  = $cart_item['custom_duration_wednesday'];
        $fs_duration_thursday   = $cart_item['custom_duration_thursday'];
        $fs_duration_friday     = $cart_item['custom_duration_friday'];
        $fs_duration_saturday   = $cart_item['custom_duration_saturday'];
        $fs_duration_sunday     = $cart_item['custom_duration_sunday'];

        $booking_data           = $cart_item[ 'yith_booking_data' ];

        $from                   = $booking_data[ 'from' ];
        $to                     = $booking_data[ 'to' ];
        $duration               = $booking_data[ 'duration' ];
        $has_time               = in_array( $product->get_duration_unit(), array( 'hour', 'minute' ) );
        $date_format            = wc_date_format();
        $date_format            .= $has_time ? ' ' . wc_time_format() : '';

        // check current day of the Booking Date and see if it matches a particular day of week
        $fs_date_day = date_i18n( 'l', $from );
        // if it does, then force change the Duration display to match the length that was set in the settings
        if( $fs_date_day === 'Monday' && $fs_duration_monday) {
            $duration = $fs_duration_monday;
            $duration_display = $duration . ' hours';
        }
        elseif( $fs_date_day === 'Tuesday' && $fs_duration_tuesday) {
            $duration = $fs_duration_tuesday;
            $duration_display = $duration . ' hours';
        }
        elseif( $fs_date_day === 'Wednesday' && $fs_duration_wednesday) {
            $duration = $fs_duration_wednesday;
            $duration_display = $duration . ' hours';
        }
        elseif( $fs_date_day === 'Thursday' && $fs_duration_thursday) {
            $duration = $fs_duration_thursday;
            $duration_display = $duration . ' hours';
        }
        elseif( $fs_date_day === 'Friday' && $fs_duration_friday ) {
            $duration = $fs_duration_friday;
            $duration_display = $duration . ' hours';
        }
        elseif( $fs_date_day === 'Saturday' && $fs_duration_saturday) {
            $duration = $fs_duration_saturday;
            $duration_display = $duration . ' hours';
        }
        elseif( $fs_date_day === 'Sunday' && $fs_duration_sunday) {
            $duration = $fs_duration_sunday;
            $duration_display = $duration . ' hours';
        }
        // otherwise fallback to the default setting from YITH
        else {
            $duration_display = yith_wcbk_format_duration( $duration, $product->get_duration_unit() );
        }

        // get the original from date again
        $fs_from = $booking_data[ 'from' ];
        $fs_duration_minutes = $duration * 60;
        // get the custom end date by adding new duration in minutes
        $fs_until_new = strtotime('+'. $fs_duration_minutes .' minutes', $fs_from);

        $booking_item_data = array(
            'yith_booking_from'     => array(
                //'key'     => yith_wcbk_get_booking_meta_label( 'from' ),
                'key'     => 'Cruising on',
                'value'   => $from,
                'display' => date_i18n( 'l, F, j Y', $from ) .' from '. date_i18n( wc_time_format(), $from ) .' to '. date_i18n( wc_time_format(), $fs_until_new )
            ),
            'yith_booking_to'       => array(
                'key'     => yith_wcbk_get_booking_meta_label( 'to' ),
                'value'   => $to,
                'display' => date_i18n( $date_format, $to )
            ),
            'yith_booking_duration' => array(
                'key'     => yith_wcbk_get_booking_meta_label( 'duration' ),
                'value'   => $duration,
                'display' => $duration_display
            )
        );

        $item_data = array_merge( $item_data, $booking_item_data );
    }

    return $item_data;
}

YITH says:

If you want to edit the meta added to the order item, you can use one of these filters: woocommerce_checkout_create_order_line_item: this is a WooCommerce hook fired when creating the order line item; we use it to add meta in order items - related to booking products. yith_wcbk_order_get_booking_order_item_details: this is an YITH Booking hook you can use to filter the order item details added for booking products

I know how to add new meta to the Cart, Checkout, and Order areas... but I need to override the YITH values instead.

I found this within their plugin files, but I'm not sure how to use their filter to override the Duration value with the one I've managed to override in the Cart/Checkout:

/**
 * get booking details from booking data
 *
 * @param array $booking_data
 * @param int   $product_id
 *
 * @return array
 *
 * @since 2.0.6
 */
public function get_booking_order_item_details( $booking_data, $product_id = 0 ) {
    $details = array();
    foreach ( $booking_data as $booking_data_key => $booking_data_value ) {
        $this_title = yith_wcbk_get_booking_meta_label( $booking_data_key );

        switch ( $booking_data_key ) {
            case 'person_types':
                if ( is_array( $booking_data_value ) ) {
                    foreach ( $booking_data_value as $person_type ) {
                        if ( !empty( $person_type[ 'number' ] ) ) {
                            $details[] = array(
                                'key'   => $person_type[ 'title' ],
                                'value' => $person_type[ 'number' ],
                            );
                        }

                    }
                }
                break;
            case 'booking_services':
                if ( is_array( $booking_data_value ) ) {
                    $booking_services        = array();
                    $hidden_booking_services = array();
                    foreach ( $booking_data_value as $service ) {
                        if ( !$service[ 'hidden' ] ) {
                            $booking_services[] = $service[ 'title' ];
                        } else {
                            $hidden_booking_services[] = $service[ 'title' ];
                        }
                    }
                    if ( !!$booking_services ) {
                        $details[] = array(
                            'key'   => yith_wcbk_get_label( 'booking-services' ),
                            'value' => yith_wcbk_booking_services_html( $booking_services ),
                        );
                    }
                    if ( !!$hidden_booking_services ) {
                        $details[] = array(
                            'key'   => '_hidden_booking_services',
                            'value' => yith_wcbk_booking_services_html( $hidden_booking_services ),
                        );
                    }
                }
                break;

            case 'from':
            case 'to':
                /** @var WC_Product_Booking $product */
                $product     = wc_get_product( $product_id );
                $date_format = wc_date_format();
                if ( $product && $product->is_type( 'booking' ) && $product->has_time() ) {
                    $date_format .= ' ' . wc_time_format();
                }
                $this_value = date_i18n( $date_format, $booking_data_value );
                $details[]  = array(
                    'key'   => $this_title,
                    'value' => $this_value,
                );
                break;
            case 'duration':
                $this_value = $booking_data_value;
                $product    = wc_get_product( $product_id );
                if ( $product && $product instanceof WC_Product_Booking ) {
                    $duration_unit       = $product->get_duration_unit();
                    $duration_unit_label = yith_wcbk_get_duration_unit_label( $duration_unit, absint( $booking_data_value ) );
                    $this_value          .= ' ' . $duration_unit_label;
                }
                $details[] = array(
                    'key'   => $this_title,
                    'value' => $this_value,
                );
                break;
            default:
                $details[] = array(
                    'key'   => $this_title,
                    'value' => $booking_data_value,
                );
                break;
        }
    }

    return apply_filters( 'yith_wcbk_order_get_booking_order_item_details', $details, $booking_data, $product_id );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.