显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

huangapple go评论72阅读模式
英文:

Display checkout ACF Advanced Custom Fields and save the value in WooCommerce

问题

我正在尝试向结账页面添加额外的字段。我已经设置了我的ACF字段并可以在结账页面上显示它们(通过acf_form),但当我下订单时,我在字段中输入的值不会传递到创建的订单。

这是我用来显示和更新字段的代码:

add_action('woocommerce_after_order_notes', 'my_acf_checkout_display');

function my_acf_checkout_display() {
    acf_form(array('form' => false, 'fields' => array('inseam')));
}

add_action('woocommerce_checkout_update_order_meta', 'acf_update_field_at_checkout');

function acf_update_field_at_checkout($order_id) {

    // acf custom field id
    $my_field = 'inseam';

    $acf_form_value = $_POST['acf'][$my_field];
    update_field($my_field, $acf_form_value, $order_id);

}

在这里,你可以看到我如何设置这些字段:显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

这是我的结账页面的样子:显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

这是订单中的“更新后”的字段(仍然为空):显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

当我将值硬编码为12时,它可以工作:

add_action('woocommerce_after_order_notes', 'my_acf_checkout_display');

function my_acf_checkout_display() {
    acf_form(array('form' => false, 'fields' => array('inseam')));
}

add_action('woocommerce_checkout_update_order_meta', 'acf_update_field_at_checkout');

function acf_update_field_at_checkout($order_id) {

    // acf custom field id
    $my_field = 'inseam';
    $acf_form_value = 12;
    //$acf_form_value = $_POST['acf'][$my_field];
    update_field($my_field, $acf_form_value, $order_id);

}

有人知道为什么它不会将inseam的输入值填充到订单中吗?

非常感谢你提前的帮助!

英文:

I am trying to add additional fields to the checkout page. I have setup my ACF fields and can display them on the checkout page (via acf_form), but when I place the order, the values I input in the field does not carry over to the created order.

This is the code I am using to display and update the fields:

    add_action( 'woocommerce_after_order_notes', 'my_acf_checkout_display' ); 

function my_acf_checkout_display() { 
    acf_form(array('form' => false,'fields' => array('inseam'))); 
}

add_action( 'woocommerce_checkout_update_order_meta','acf_update_field_at_checkout' );
  
function acf_update_field_at_checkout( $order_id ) {
        
        // acf custom field id
        $my_field = 'inseam';
	
        $acf_form_value = $_POST['acf'][$my_field];
        update_field($my_field,$acf_form_value,$order_id);

}

Here you can see how I set up the fields:
显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

Here's what my checkout page looks like:
显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

And here is the "updated" field in the order (which remains empty):

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

When I hardcode the value to, say 12, then it works!

    add_action( 'woocommerce_after_order_notes', 'my_acf_checkout_display' ); 

function my_acf_checkout_display() { 
    acf_form(array('form' => false,'fields' => array('inseam'))); 
}

add_action( 'woocommerce_checkout_update_order_meta','acf_update_field_at_checkout' );
  
function acf_update_field_at_checkout( $order_id ) {
        
        // acf custom field id
        $my_field = 'inseam';
		$acf_form_value = 12;
        //$acf_form_value = $_POST['acf'][$my_field];
        update_field($my_field,$acf_form_value,$order_id);

}

Does anyone know why it's not populating the entered value for inseam to the order?

Thank you very much in advance!

答案1

得分: 2

以下是您要翻译的内容:

"You don't use the correct Field ID to be used with ACF.

So first in admin ACF Fields group click on 'screen Options':

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

Then select to display the field 'slug' and 'Field Key' options.

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

Now you can get the correct 'Field Key' to use in your code...

So here is the code I have tested, reproducing your settings (replace in the code the correct field Key for your 'Inseam' field):

// Displaying ACF field in Checkout after order notes
add_action( 'woocommerce_after_order_notes', 'checkout_display_acf_field' ); 
function checkout_display_acf_field() { 
    $acf_field_key = 'field_648715e6451d1'; // Here set the field Key

    acf_form( array(
        'form' => false, 
        'fields' => array( $acf_field_key )
    ) ); 
}

add_action( 'woocommerce_checkout_update_order_meta', 'acf_update_checkout_field' );
function acf_update_checkout_field( $order_id ) {
    $acf_field_key = 'field_648715e6451d1'; // Here set the field Key
    
    if ( isset($_POST['acf'][$acf_field_key]) ) {
        $acf_field_value = sanitize_text_field( $_POST['acf'][$acf_field_key] );
        update_field( $acf_field_key, $acf_field_value, $order_id ); 
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Now the field is displayed in The Admin on the order page with the submitted value.


Handling multiple Advanced custom fields in a clean way

First, I have set 2 fields in ACF Field group settings:

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

Then Here is the code:

add_action( 'woocommerce_after_order_notes', 'checkout_display_acf_field' ); 
function checkout_display_acf_field() { 
    $acf_field_key_arr = array ( // Here set the all required fields Keys in the array
        'field_648715e6451d1',
        'field_64873b2d5f57d'
    );
    
    // Loop through each field key in the array
    foreach ( $acf_field_key_arr as $acf_field_key ) {
        acf_form( array(
            'form' => false, 
            'fields' => array( $acf_field_key )
        ) );
    }
}

add_action( 'woocommerce_checkout_update_order_meta', 'acf_update_checkout_field' );
function acf_update_checkout_field( $order_id ) {
    $acf_field_key_arr = array (  // Here set the all required fields Keys in the array
        'field_648715e6451d1',
        'field_64873b2d5f57d'
    );
    
    // Loop through each field key in the array
    foreach ( $acf_field_key_arr as $acf_field_key ) {
        if ( isset($_POST['acf'][$acf_field_key]) ) {
            $acf_field_value = sanitize_text_field( $_POST['acf'][$acf_field_key] );
            update_field( $acf_field_key, $acf_field_value, $order_id ); 
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

The display in checkout page:

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。"

希望这能帮助您。如果您需要更多帮助,请随时告诉我。

英文:

You don't use the correct Field ID to be used with ACF.

So first in admin ACF Fields group click on "screen Options":

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

Then select to display the field "slug" and "Field Key" options.

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

Now you can get the correct "Field Key" to use in your code...

So here is the code I have tested, reproducing your settings (replace in the code the correct field Key for your "Inseam" field):

// Displaying ACF field in Checkout after order notes
add_action( 'woocommerce_after_order_notes', 'checkout_display_acf_field' ); 
function checkout_display_acf_field() { 
    $acf_field_key = 'field_648715e6451d1'; // Here set the field Key

    acf_form( array(
        'form' => false, 
        'fields' => array( $acf_field_key )
    ) ); 
}

add_action( 'woocommerce_checkout_update_order_meta', 'acf_update_checkout_field' );
function acf_update_checkout_field( $order_id ) {
    $acf_field_key = 'field_648715e6451d1'; // Here set the field Key
    
    if ( isset($_POST['acf'][$acf_field_key]) ) {
        $acf_field_value = sanitize_text_field( $_POST['acf'][$acf_field_key] );
        update_field( $acf_field_key, $acf_field_value, $order_id ); 
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Now the field is displayed in The Admin on the order page with the submitted value.


Handling multiple Advanced custom fields in a clean way

First, I have set 2 fields in ACF Field group settings:

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

Then Here is the code:

add_action( 'woocommerce_after_order_notes', 'checkout_display_acf_field' ); 
function checkout_display_acf_field() { 
    $acf_field_key_arr = array ( // Here set the all required fields Keys in the array
        'field_648715e6451d1',
        'field_64873b2d5f57d'
    );
    
    // Loop through each field key in the array
    foreach ( $acf_field_key_arr as $acf_field_key ) {
        acf_form( array(
            'form' => false, 
            'fields' => array( $acf_field_key )
        ) );
    }
}

add_action( 'woocommerce_checkout_update_order_meta', 'acf_update_checkout_field' );
function acf_update_checkout_field( $order_id ) {
    $acf_field_key_arr = array (  // Here set the all required fields Keys in the array
        'field_648715e6451d1',
        'field_64873b2d5f57d'
    );
    
    // Loop through each field key in the array
    foreach ( $acf_field_key_arr as $acf_field_key ) {
        if ( isset($_POST['acf'][$acf_field_key]) ) {
            $acf_field_value = sanitize_text_field( $_POST['acf'][$acf_field_key] );
            update_field( $acf_field_key, $acf_field_value, $order_id ); 
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

The display in checkout page:

显示结账 ACF 高级自定义字段并将值保存在 WooCommerce 中。

huangapple
  • 本文由 发表于 2023年6月12日 20:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定