0

I have been looking for a way to get the quantity of an order placed through WooCommerce to populate into the Gravity Form assigned to that product.

I am using these plugins: 1. Woocommerce 2. Gravity Form 3. Gravity Form Product Add-on


I know that the GF product add-on has a setting to connect the quantity field of WC to a field in GF but it is limited. It only works on the 'add to cart' submission. If someone updates the quantity on the literal cart page... the GF field for quantity is not updated.

Therefore, I tried the following function, and was expecting the total quantity from the order to be in the GF field that I assigned with this line:

 $entry['<your_quantity_field_id>'] = $quantity;

The field syntax I used was formID.FieldID so let's say 3.45 - 3 being the form ID and 45 the field.

I have a basic number field hidden on the form.

add_action('woocommerce_checkout_order_processed', 'update_gform_entry_with_quantity', 10, 2);

function update_gform_entry_with_quantity($order_id, $posted_data) {
    // Get the order object
    $order = wc_get_order($order_id);

    if (!$order) {
        return;
    }

    // Loop through order items
    foreach ($order->get_items() as $item_id => $item) {
        // Get the Gravity Forms entry ID from the order item meta
        $entry_id = $item->get_meta('_gravity_forms_history__entry_id', true);

        if ($entry_id) {
            // Get the quantity of the order item
            $quantity = $item->get_quantity();

            // Get the entry object
            $entry = GFAPI::get_entry($entry_id);

            if (!is_wp_error($entry)) {
                // Update the entry with the quantity value
                $entry['<your_quantity_field_id>'] = $quantity;
                GFAPI::update_entry($entry);
            }
        }
    }
}
starball
  • 20,030
  • 7
  • 43
  • 238
Drez
  • 1
  • The field syntax should just be the field id, i.e $entry['45'] = $quanity; – Rochelle Apr 06 '23 at 01:45
  • Thank you... It is still not appending the quantity into the GF. I decided to turn off quantity updating and built the quantity updates into GF itself. – Drez Apr 07 '23 at 11:02

0 Answers0