1

I have this code and I want to add data to $meta_input_array variable. When I add single data it's ok but when I want to add more than one it doesn't work

Here is my code:

<?php

$spa_products = array(
  'post_type' => 'spa_products'
);

$spa = new WP_Query( $spa_products );

if ( $spa->have_posts() ) {

  $row = 0;

  while ( $spa->have_posts() ) { $spa->the_post();

    $product_POST = 'product_' . get_the_ID();

    $product_normal_price = get_post_meta( get_the_ID(), 'spa_normal_price', true );
    $product_gold_price = get_post_meta( get_the_ID(), 'spa_gold_price', true );

    if ( !empty( $_POST[$product_POST] ) ) {

      if ( $_POST[$product_POST] === $product_normal_price ) {
        $product_package = $product_normal_price;
        $product_package_show = 'بسته معمولی';
      } elseif ( $_POST[$product_POST] === $product_gold_price ) {
        $product_package = $product_gold_price;
        $product_package_show = 'بسته طلائی';
      }

      echo '<tr>';

      echo '<td>' . get_the_title() . ' " <small>' . $product_package_show . '<small> " </td><td>' . number_format( $product_package ) . ' ریال</td>';

      echo '</tr>';

      $invoice_total[] = $product_package;

      $meta_input_array_row = 'row_' . $row++;
      $meta_input_array[] = array( $meta_input_array_row => $product_package_show );

    }
  }
}

$spa_new_order = array(
  'post_type'     => 'spa_orders',
  'post_title'    => $_POST['product_number'],
  'post_status'   => 'draft',
  'meta_input'    => $meta_input_array,
);

wp_insert_post( $spa_new_order );

?>

@ron Here is the results:

array(2) { [0]=> array(1) { ["row_0"]=> string(19) "gold" } [1]=> array(1) { ["row_1"]=> string(21) "normal" } } 
6
  • what is your exact error? Commented May 9, 2020 at 0:05
  • @Ron I didn't get any error but My data didn't import Commented May 9, 2020 at 0:07
  • @Ron My problem is with $meta_input_array[] Commented May 9, 2020 at 0:08
  • There could be many reasons why your "data didn't import". Right before line $spa_new_order = , could you put var_dump($meta_input_array); and rerun your script, there will be output generated, can you add that output to your Question above? Commented May 9, 2020 at 0:11
  • @Ron I add it . Commented May 9, 2020 at 0:19

1 Answer 1

2

Try changing:

      $meta_input_array[] = array( $meta_input_array_row => $product_package_show );

to

      $meta_input_array[$meta_input_array_row] = $product_package_show;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.