无法调用函数两次,woocommerce,重力形式
Cannot call function twice, woocommerce, gravity forms
提交重力表单后,我已成功使用以下代码创建 woocommerce 订单。
add_action( 'gform_after_submission_56', 'post_to_third_party', 10, 2 ); function post_to_third_party( $entry, $form ) { global $woocommerce; // use this to find out $entry output var_dump($entry);
// set some variables
$user_id =rgar( $entry, '97' );
$product_id = rgar( $entry, '71' );
$quantity = rgar( $entry, '73' );
$note = rgar( $entry, '53' );
$product = wc_get_product($product_id);
$address = array( 'first_name' => rgar( $entry, '98' ), 'last_name' => rgar( $entry, '99' ), 'company' => rgar( $entry, '' ), 'email' => rgar( $entry, '83' ), 'phone' => rgar( $entry, '84' ), 'address_1' => rgar( $entry, '88.1' ), 'address_2' => rgar( $entry, '88.2' ), 'city' => rgar( $entry, '88.3' ), 'state' => rgar( $entry, '88.4' ), 'postcode' => rgar( $entry, '88.5' ), 'country' => rgar( $entry, '88.6' ), );
$order = wc_create_order(); $order->set_customer_id( $user_id ); $order->add_product( wc_get_product($product_id), $quantity, $prices); foreach ($order->get_items() as $item_key => $item ) { $item->add_meta_data( 'Booking Request', $note, true ); } $order->set_address( $address, 'billing' ); $order->calculate_totals(); $order->update_status( 'pending payment', 'pending', TRUE); $order->add_order_note( $note );
$coupon_code = rgar( $entry, '105' ); $order->apply_coupon($coupon_code);
我面临的问题是我需要一个不同的表格来在提交时创建一个 woocommerce 订单。我不知道该怎么做,因为当我创建一个反映正确表单 ID 的代码时,它告诉我它不能多次调用该函数。
我该怎么办?是否还有其他功能,或者我是否以某种方式添加到这个原始代码中?
问题不是钩子 (gform_after_submission) 被回收了,而是第二部分 "post_to_third_party."
该功能无法重新定义。您可以重命名它,或者让您的 after_submission 对所有表单更加模块化。
add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
switch ($form['id']) {
case 56:
// do your code for form 56.
break;
case 12:
// do stuff if form id is 12... or whatever.
break;
}
}
提交重力表单后,我已成功使用以下代码创建 woocommerce 订单。
add_action( 'gform_after_submission_56', 'post_to_third_party', 10, 2 ); function post_to_third_party( $entry, $form ) { global $woocommerce; // use this to find out $entry output var_dump($entry);
// set some variables
$user_id =rgar( $entry, '97' );
$product_id = rgar( $entry, '71' );
$quantity = rgar( $entry, '73' );
$note = rgar( $entry, '53' );
$product = wc_get_product($product_id);
$address = array( 'first_name' => rgar( $entry, '98' ), 'last_name' => rgar( $entry, '99' ), 'company' => rgar( $entry, '' ), 'email' => rgar( $entry, '83' ), 'phone' => rgar( $entry, '84' ), 'address_1' => rgar( $entry, '88.1' ), 'address_2' => rgar( $entry, '88.2' ), 'city' => rgar( $entry, '88.3' ), 'state' => rgar( $entry, '88.4' ), 'postcode' => rgar( $entry, '88.5' ), 'country' => rgar( $entry, '88.6' ), );
$order = wc_create_order(); $order->set_customer_id( $user_id ); $order->add_product( wc_get_product($product_id), $quantity, $prices); foreach ($order->get_items() as $item_key => $item ) { $item->add_meta_data( 'Booking Request', $note, true ); } $order->set_address( $address, 'billing' ); $order->calculate_totals(); $order->update_status( 'pending payment', 'pending', TRUE); $order->add_order_note( $note );
$coupon_code = rgar( $entry, '105' ); $order->apply_coupon($coupon_code);
我面临的问题是我需要一个不同的表格来在提交时创建一个 woocommerce 订单。我不知道该怎么做,因为当我创建一个反映正确表单 ID 的代码时,它告诉我它不能多次调用该函数。
我该怎么办?是否还有其他功能,或者我是否以某种方式添加到这个原始代码中?
问题不是钩子 (gform_after_submission) 被回收了,而是第二部分 "post_to_third_party." 该功能无法重新定义。您可以重命名它,或者让您的 after_submission 对所有表单更加模块化。
add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
switch ($form['id']) {
case 56:
// do your code for form 56.
break;
case 12:
// do stuff if form id is 12... or whatever.
break;
}
}