I am using the ACF plugin to manage a variety of custom meta data within a WooCommerce website. One of the features I'm trying to get set is to add custom fields to the WooCommerce checkout page. I am following the general concept outlined on WC's documentation page, which allows you to append custom forms to the WC checkout form via the woocommerce_after_order_notes hook. From there I'm inputting the acf form using the acf_form () function with form set to false to allow it to not include the ACF form elements (i.e. the ACF submit button). This seams to work because I'm successfully able to get the ACF custom fields to display on the checkout field in the appropriate location.
From there I'm attempting to use the ACF update_field() function in combination with the woocommerce_checkout_update_order_met hook. As recommended with the ACF community here. Here is my code that I have so far being placed in my functions.php file:
This part seams to work.
add_action( 'woocommerce_after_order_notes', 'my_custom_budgetcenter_field' ); //This runs the my_custom_budgetcenter_field funtion within the chekout form.
function my_custom_budgetcenter_field( ) {
echo '<div id="my_custom_budget center_field"><h2>' . __('Budget Center Field') . '</h2></div>'; // This Line Adds A header Line the the bottom of the WC Checkout Page.
acf_form(array('form' => false,'fields' => array('acf_selected_budget_center'))); //This line outputs the ACF form with form value set to false so its included in the woocommerce checkout form. It then sets the fields to equal my the acf_selected_budget_center field group
}
This is the part that i'm having problems with:
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_budgetcenter_field_update_order_meta' ); //This line runs the my_custom_budgetcenter_field_update_order_meta when woocomerce updates the order meta when submiting the form.
function my_custom_budgetcenter_field_update_order_meta( $order_id ) {
$valueofbc = get_field( "field_5c005d0c5f829" );
$bcfieldkey = "field_5c005d0c5f829";
update_field( $bcfieldkey, $valueofbc, $order_id );
} ;
When I manually set the $valueofbc string (Example: $valueofbc = "001 - Activities" ;
) everything works. I can submit an order and that field is updated with the hard coded string. However when I set it to the get_field function option so that it uses the ACF custom filed (a select value) it does not save the value. After doing some digging it looks like I need(ed) to set acf_form_head() above the wordpress get_header() function on the checkout page template inorder for ACF to pull and post the data. However when I did this and go through the checkout process and then click "submit order" on woocomerce' checkout page I get a "leave site? -- Changes you made may not be saved" -- Leave -- Cancel" JavaScript notice when the page attempts to process the order. When I click "Leave" it goes to the order confirmation page but does not save the meta value. When I click cancel the order processor just spins infitinatly and never refreshes the page.
At this point i'm at my witt's end trying to get this to work. I have to use ACF because of some advanced conditional logic on what is display and the way that data will work with other data in other forms. So using a basic checkout form add on plugin in not an option. Additionally ACF's support can take several weeks, which is why I'm posting here. Any help would be greatly appreciated.
Thanks,