gpt4 book ai didi

php - WooCommerce Checkout 上的高级自定义字段 ( ACF )

转载 作者:行者123 更新时间:2023-12-02 06:30:48 41 4
gpt4 key购买 nike

我正在使用ACF plugin管理 WooCommerce 网站内的各种自定义元数据。我尝试设置的功能之一是将自定义字段添加到 WooCommerce 结账页面。我遵循 WC's documentation page 上概述的一般概念,它允许您通过 woocommerce_after_order_notes Hook 将自定义表单附加到 WC 结帐表单。从那里我使用acf_form ()输入acf表单。将 form 设置为 false 的函数,以允许其不包含 ACF 表单元素(即 ACF 提交按钮)。这看起来很有效,因为我成功地让 ACF 自定义字段显示在结账字段的适当位置。

从那里我尝试使用 ACF update_field()函数与 woocommerce_checkout_update_order_met Hook 结合使用。按照 ACF community here 的建议。这是迄今为止放置在我的functions.php 文件中的代码:

这部分工作正常。

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
}

这是我遇到问题的部分:

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 );

} ;

当我手动设置 $valueofbc 字符串(例如: $valueofbc = "001 - Activity"; )时,一切正常。我可以提交订单,该字段将使用硬编码字符串进行更新。但是当我将其设置为get_field时函数选项,以便它使用 ACF 自定义字段(选择值),但不保存该值。经过一番挖掘后,看起来我需要设置 acf_form_head()在结帐页面模板上的 wordpress get_header() 函数上方,以便 ACF 提取和发布数据。但是,当我执行此操作并完成结帐流程,然后在 woocomerce 结帐页面上单击“提交订单”时,我收到“离开网站? - 您所做的更改可能不会保存” - 离开 - 取消”JavaScript 通知该页面尝试处理订单。当我单击“离开”时,它会转到订单确认页面,但不会保存元值。当我单击取消时,订单处理器只会无限旋转,并且从不刷新页面。

此时我已无能为力,试图让它发挥作用。我必须使用 ACF,因为关于显示内容以及数据与其他形式的其他数据的工作方式有一些高级条件逻辑。因此,使用基本的结账表单添加插件不是一个选项。此外,ACF 的支持可能需要几周时间,这就是我在这里发帖的原因。任何帮助将不胜感激。

谢谢

最佳答案

想通了。acf_form_head() 函数是多个操作的组合。其中一个操作(acf/submit_form)包含一个重定向功能,该功能在通过其他表单提交时显然不起作用。解决方案是使用 acf/save_post 操作而不是 Submit_form。

关于php - WooCommerce Checkout 上的高级自定义字段 ( ACF ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53658380/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com