Support

Account

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

  • Hi @nicorubrixcreations-nl

    You need to loop the product data and add it to the repeater. Something like:

    <?php
    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(); 
    	
    	$products = array();
    	
    	foreach ( $order_items as $item_id => $item_data ) {
    		$product	= $item_data->get_product();
    		$qty		.= $item_data->get_quantity();
    		$name		.= $product->get_name();
    		
    		$products[] = array(
    			'product'	=> $product,
    			'qty'		=> $qty
    		);
    	}
    
    	$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);
    
    	/*
    	$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 );
    	}
    	*/
    
    	echo '<pre>';
    	print_r($products); #check the data in the array. 
    	echo '</pre>';
    	
    	if( $products ):
    		foreach( $products as $product ):
    	
    			$row = array(
    				'field_61645b916cbd7'	=> $product[0],		#product field key
    				'field_6165450699ffa'	=> $product[1],		#qty field key
    			);
    			$i = add_row('field_61645b866cbd6', $row, $post_id); #this is the key for the main repeater
    
    	
    		endforeach;
    	endif;
    
    }
    add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );

    You need to check/change the key values. The code is also untested but hopefully will get you on the right lines.

    Basically, add the product data to a new array. Then loop that data into your repeater.