Support

Account

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

Solving

Save WooCommerce OrderData in ACF Field

  • My goal is to save WooCommerce order data into an ACF Field in the backend of WordPress. For this project every order which is completed generates a new post in my custom post type named ‘groeiprocessen’. The code for this function looks like this and works perfectly.

    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();
        $content.= $item_data->get_quantity();
        $content.= $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);
    }
    add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );

    Right now the product name and quantity gets saved in the post content. I have created two ACF Fields under the content field. Now my question is: How can I save the quantity data for the right product in the ACF Fields under my post?

    Field ID’s
    acf-field_6163f2e1542e1
    acf-field_61640103002c9

    Field names
    acf[field_6163f2e1542e1]
    acf[field_61640103002c9]

    I would really appreciate the help. Thanks in advance!

  • You need to get the value from the order, I do not know how to do that. Once you have the value you want to save then you call update field using that field’s key

    For example:

    
    update_field('field_6163f2e1542e1', $value, $post_id)
    
  • 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!

  • 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.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.