43. Integration with WooCommerce | Form Forge - Build Forms with AI in Seconds
Download Log in

43. Integration with WooCommerce

Developer Guide

Embed forms in WooCommerce product pages and create orders from submissions.

Product Inquiry Tab

php
add_filter( 'woocommerce_product_tabs', function( $tabs ) {
    $tabs['inquiry'] = [
        'title'    => 'Product Inquiry',
        'priority' => 50,
        'callback' => function() {
            global $product;
            // Render form and pass product ID as hidden field
            echo '<input type="hidden" name="wc_product_id" value="' . esc_attr( $product->get_id() ) . '">';
            echo FORMFORGE_Form_Renderer::instance()->render( 15 );
        },
    ];
    return $tabs;
} );

Create WooCommerce Order on Submission

php
add_action( 'formforge_after_submit', function( $submission_id, $form_id, $data ) {
    if ( $form_id !== 10 || ! function_exists( 'wc_create_order' ) ) return;

    $order = wc_create_order();
    $order->set_billing_email( $data['field_2']['value'] ?? '' );
    $order->set_billing_first_name( $data['field_1']['value'] ?? '' );
    $order->add_meta_data( '_formforge_submission_id', $submission_id );
    $order->set_status( 'processing' );
    $order->save();
}, 10, 3 );

Add Product to Order from Hidden Field

php
add_action( 'formforge_after_submit', function( $submission_id, $form_id, $data ) {
    if ( $form_id !== 10 || ! function_exists( 'wc_create_order' ) ) return;

    $product_id = (int) ( $_POST['wc_product_id'] ?? 0 );
    if ( ! $product_id ) return;

    $product = wc_get_product( $product_id );
    if ( ! $product ) return;

    $order = wc_create_order();
    $order->add_product( $product, 1 );
    $order->set_billing_email( $data['field_email']['value'] ?? '' );
    $order->calculate_totals();
    $order->set_status( 'pending' );
    $order->save();
}, 10, 3 );

Forge AI Assistant Online

Hi! I'm the Form Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs