<?php

add_filter( 'wdp_save_cart_item_keys', function ( $keys ) {
  return array_merge( $keys, array(
    '_booking_price',
    '_booking_duration',
    '_ebs_start',
    '_ebs_end',
    'addons'
  ) );
} );

add_filter( 'wdp_wc_cart_item_before_clear_from_rules_keys', function ( $wc_cart_item, $wc_cart_item_key ) {
  if ( isset( $wc_cart_item['_booking_price'], $wc_cart_item['wdp_original_price'] ) ) {
    $wc_cart_item['_booking_price'] = $wc_cart_item['wdp_original_price'];
  };

  return $wc_cart_item;
}, 10, 2 );

$replace_price = function ( $price, $product, $_product, $data, $price_type ) {
  if ( 'price' === $price_type ) {
    $price = $_product->is_on_sale() ? $_product->get_sale_price() * $data['duration'] : $price;
  }

  return $price;
};

add_filter( 'easy_booking_one_date_price', $replace_price, 10, 5 );
add_filter( 'easy_booking_two_dates_price', $replace_price, 10, 5 );

add_filter( "wdp_get_product_price", function ( $price, $product, $price_mode, $item_meta ) {
  if ( $product->is_on_sale( 'edit' ) ) {
    if ( 'sale_price' === $price_mode ) {
      $price = $product->get_sale_price( '' );
    } elseif ( 'discount_sale' === $price_mode ) {
      $price = $product->get_sale_price( '' );
    } else {
      $price = $product->get_regular_price( '' );
    }
  } else {
    $price = $product->get_price( '' );
  }

  if ( isset( $item_meta['_booking_duration'] ) ) {
    $price *= $item_meta['_booking_duration'];
  }

  return $price;
}, 10, 4 );