Populate cart content in to hidden form field?

Hey everyone,

Is it possible to populate cart content to hidden field in Pro Forms if the form is added to cart page?

I asked ChatGPT and it gave me this snippet, but I couldn’t make it work.

add_action( 'bricks/builder/enqueue_scripts', 'populate_cart_content_to_bricks' );
function populate_cart_content_to_bricks() {
    if ( WC()->cart->get_cart_contents_count() == 0 ) {
        return;
    }
    $cart_content = '';
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        $cart_content .= $product->get_name() . ' x ' . $cart_item['quantity'] . "\n";
    }
    echo "<script>
        document.addEventListener('DOMContentLoaded', function() {
            let hiddenField = document.querySelector('[name=\"form-field-nzpmuc\"]');
            if (hiddenField) {
                hiddenField.value = `" . $cart_content . "`;
            }
        });
    </script>";
}

“In the default value or value field of this hidden field, enter: {echo echo_cart_content_for_bricks()}. This should execute the PHP function and populate the hidden field with the cart contents when the form is rendered.”

The goal here is to make a simple Cart to Quote type of functionality for our client who doesn’t want to actually use WooCommerce for direct sales. I know that there is plugins for this functionality, but I’d rather use this method if it’s anyhow possible. Tried couple of the plugins already and they didn’t work as I wanted.

Pretty sure the the echo tag needs something returned, so end the function with return $cart_content;

function populate_cart_content_to_bricks() {
    if ( WC()->cart->get_cart_contents_count() == 0 ) {
        return;
    }
    $cart_content = '';
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        $cart_content .= $product->get_name() . ' x ' . $cart_item['quantity'] . "\n";
    }
   return $cart_content;
}

Also the echo tag is missing a : it should be {echo:populate_cart_content_to_bricks} You can omit the () if you aren’t passing anything into the function anyway.

Secondly, I’d probably put any JS into the custom code section of the site or a page instead of straight into the function. But you shouldn’t need to use any JS at all as long as the function returns the correct values and you use the echo tag in the value of a hidden field.

Lemme know how it goes, if it still doesn’t work I’ll give it a shot on my end later

1 Like

Thanks a lot for your input! That actually did the trick. Didn’t even notice the : missing in echo, so I don’t know would’ve it been working already, but I changed the snippet to the one you provided. :grimacing:

image

@Daniele do you think it’s possible also to include them in Email Designer? I’ve tried to use the fields with {{brx_id}} but for some reason it is not populating any of the data.

Also hidden fields should work. Please click on “Twig” and check if there are some formatting issues :slight_smile: