I have successfully used the following code to create a woocommerce order after gravity form submission.
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);
The issue I am faced with is that I need a different form to also create a woocommerce order upon submission. I don't know how to do this as when I create a code reflecting the correct form id it tells me it cannot call the function more than once.
What should I do? is there another function or do I somehow add to this original code?
The problem isn't the hook (gform_after_submission) being recycled, but the second part, "post_to_third_party." That function can't be redefined. You can either rename that, or just make your after_submission a bit more modular for all forms.
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;
}
}