'Display coupon code customized fields values in edit page in Woocommerce

Eveybody, I'm sorry if the answer is very simple, i'm a student, but i've a problem with my custom coupon field in the edit page page in Woocommerce. i'd search so long time but the answer was for display coupon in order or checkout ... i've created 4 new fields in the coupon, i can store them and i can validate in the cart, ecreything is fine, except one thing. I'can't display the data already saved in the edit coupon page. For a better UX, i would display them in the input created. SomeOne can help me please ?

// This my class
public function __construct()
  {

    $this->removeHooks();
    add_action('woocommerce_coupon_options_usage_restriction', [$this, 'ajoutPeriode'], 10, 2);
    add_action('woocommerce_coupon_options_usage_restriction', [$this, 'ajoutDuree'], 20, 2);
    add_action('woocommerce_coupon_options_save', [$this, 'saveAjoutPeriode'], 10, 2);
    add_action('woocommerce_coupon_options_save', [$this, 'saveAjoutDuree'], 20, 2);
    add_action('woocommerce_coupon_is_valid', [$this, 'isValidCode'], 100, 2);
    add_action( 'add_meta_boxes', [$this, 'addCustomCouponFieldOnEdit']);
  }
  /**
   * Fonction qui ajoute un champ periode de voyage dans les codes promos
   *
   * @version 1.0.0
   * @author Marie N
   * @param [type] $coupon_get_id
   * @param WC_Coupon $coupon
   * @return void
   */
  public function ajoutPeriode($coupon_get_id, WC_Coupon $coupon)
  {
    include locate_template('templates/parts/post/champPeriodeVoyage.php');
  }

  /**
   * fonction qui ajoute un champ durée dans les codes promos 
   *
   * @version 1.0.0
   * @author Marie N
   * @param [type] $coupon_get_id
   * @param WC_Coupon $coupon
   * @return void
   */
  public function ajoutDuree($coupon_get_id, WC_Coupon $coupon)
  {
    include locate_template('templates/parts/post/champDureeVoyage.php');
    var_dump($coupon);
  }

  /**
   * Sauvegarde en base les données dans les nouveaux champs 
   *
   * @version 1.0.0
   * @author Marie N
   * @param [type] $post_id
   * @param [type] $coupon
   * @return void
   */
  public function saveAjoutPeriode($post_id, $coupon)
  {
    if (isset($_POST['restriction_time_start']))
      update_post_meta($post_id, 'restriction_time_start', $_POST['restriction_time_start']);

    if (isset($_POST['restriction_time_end']))
      update_post_meta($post_id, 'restriction_time_end', $_POST['restriction_time_end']);
  }
  /**
   * sauvegarde les données du champ en base 
   *
   * @version 1.0.0
   * @author Marie N
   * @param [type] $post_id
   * @param [type] $coupon
   * @return void
   */
  public function saveAjoutDuree($post_id, $coupon)
  {
    if (isset($_POST['time_travel_start']))
      update_post_meta($post_id, 'time_travel_start', $_POST['time_travel_start']);

    if (isset($_POST['time_travel_end']))
      update_post_meta($post_id, 'time_travel_end', $_POST['time_travel_end']);
  }
  /**
   * Verification de la validité du code promo
   *
   * @version 1.0.0
   * @author Marie N
   * @param [type] $is_valid
   * @param [type] $coupon
   * @return boolean
   */
  public function isValidCode($is_valid, $coupon)
  {
    $contenuPanier = WC()->cart->get_cart();
    // var_dump($contenuPanier);

    foreach (WC()->cart->get_cart() as $cart_item) {
      $dateReservationDebut = $cart_item['rental_data']['pickup_date'];
      $dateReservationFin = $cart_item['rental_data']['dropoff_date'];
      $nombreJoursReserves = $cart_item['rental_data']['days'];
    }

    $datePeriodeDebut = get_post_meta($coupon->get_id(), 'restriction_time_start', true);
    $datePeriodeFin = get_post_meta($coupon->get_id(), 'restriction_time_end', true);
    $dateDureeMin = get_post_meta($coupon->get_id(), 'time_travel_start', true);
    $dateDureeMax = get_post_meta($coupon->get_id(), 'time_travel_end', true);


    if ($dateReservationDebut < $datePeriodeDebut && $dateReservationDebut > $datePeriodeFin) return;
    if ($dateReservationFin < $datePeriodeDebut && $dateReservationFin > $datePeriodeFin) return;
    if ($nombreJoursReserves < $dateDureeMin && $nombreJoursReserves > $dateDureeMax) return;
    return $is_valid;
  }

// below is my field template for adding a reservation date

<div class="options-group">
    <?php  woocommerce_form_field(
      'restriction_time_start',
      [
    'type' => 'date',
    'name' => 'restriction-time_start',
    'class' => ['form-field'],
    'label' => 'Période de réservation début',
  ]
);

woocommerce_form_field(
  'restriction_time_end',
  [
    'type' => 'date',
    'name' => 'restriction-time-end',
    'class' => ['form-field'],
    'label' => 'Période de réservation fin',
    'placeholder' => '',
  ]
)
?>

It's my first question on stackoverflow, i'm a little bit scared ^^ If you need more information, ask me, especially my code is in Fran-glish, sorry about that ! Tkhs



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source