Support

Account

Home Forums Backend Issues (wp-admin) Save WooCommerce OrderData in ACF Field Reply To: Save WooCommerce OrderData in ACF Field

  • Thanks John, I was able to upgrade my code using an array eventually.

    function create_post_after_order( $order_id ) {
      if (  $order_id instanceof WC_Order ){
        return;
      }
    	
      $order = wc_get_order( $order_id );
      $order_items = $order->get_items(); 
    	
    	foreach ( $order_items as $item_id => $item_data ) {
        $product = $item_data->get_product();
    	$qty.= $item_data->get_quantity();
        $name.= $product->get_name();
      }
    	
    	$new_post = array(
        'post_title' => "Order {$order_id}",
        'post_content' => $content,
        'post_status' => 'private',
        'post_date' => date('Y-m-d H:i:s'),
        'post_author' => $user_ID,
        'post_type' => 'groeiproces',
        'post_status' => 'publish',
      );    
      $post_id = wp_insert_post($new_post);
    
    // repeater field with multiple subfields
    $class_field_key = 'field_61645b866cbd6';
    $class_subfield_name = 'field_61645b916cbd7';
    $class_subfield_colour = 'field_6165450699ffa';
    $class_names = array($name,$name);
    $class_colours = array($qty,$qty);
    
    foreach ($class_names as $index => $class_names) {
    	$class_value[] = array($class_subfield_name => $class_names, $class_subfield_colour => $class_colours[$index]);
    	update_field( $class_field_key, $class_value, $post_id );
    }
        
    }
    add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );

    Right now the only problem is that this combines the values in one field. e.g.

    Name
    Product1Product2

    Quantity
    11

    Does anyone know how to split those values and save them separated. e.g.

    Product1 1
    Product2 1

    Thanks in advance!