我在产品中有一个重力表单,所以当您购买产品时,必须填写该表单。我正试图从一个表单字段中获取一些数据,并将这些数据分配给woocommerce订单自定义字段。
插件,表单和自定义字段设置,我只是努力找到正确的钩子/方式自动填写自定义字段时,下订单。
使用的插件有:
- Woocommerce
- WooCommerce管理自定义订单字段 <
- 引力形式/gh>
- 重力形式产品附加组件
任何帮助将不胜感激。谢谢。
您可以尝试添加这样的东西到您的functions.php:(确保您阅读注释注释,其中显示您在哪里添加您的WooCommerce Admin自定义订单字段以及您的重力表单输入字段id
//Get Gravity Form Info into Woocoomerce custom Fields
add_action( 'woocommerce_thankyou', 'wc_add_gf_to_woo' );
function wc_add_gf_to_woo( $order_id ) {
$order = wc_get_order( $order_id );
$cart_items = $order->get_items();
foreach( $cart_items as $item => $item_meta) {
$linked_entry=$item_meta->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
if (isset($linked_entry) && !empty($linked_entry)){
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );
update_post_meta( $order_id, '_wc_acof_{id}', $entry['40'] );//replace {id} with the id of custom field (see here https://woocommerce.com/document/woocommerce-admin-custom-order-fields/#section-10) && Replace '40' with the Gravity Form field id that contains the info you need
}
}
};