0

I am trying to place a CF7 Contact form on the product details page. I have used the WooCommerce hook woocommerce_before_add_to_cart_button for that.

add_action( 'woocommerce_before_add_to_cart_button', 'custom_before_add_to_cart_btn' );

function custom_before_add_to_cart_btn() {
    echo do_shortcode( '[contact-form-7 id="69" title="Contact form 1"]' );
}

But on submitting the form, the page is actually getting refreshed. The Ajax is no longer working. I got no errors in the Console, so i don't think it's a JS issue.

It is a fresh WooCommerce Storefront Installation, no other plug-ins or anything. Do you guys have any idea how this is happening and how can I get around this ?

This is the screencast of what's exactly happening : Screencast

1
  • is the cf7 pluign script.js bein gloaded? Likely not due to the fact that the woocommerece hook fires after the page script enqueue action. I would try to use wp_register_script to register cf7 scripts/css on page enqueue action.
    – Aurovrata
    Commented Dec 1, 2020 at 15:49

1 Answer 1

1

I am not sure when the woocommerce_before_add_to_cart_button action fires in the request sequence, but it could be worth adding the following code to your functions.php to see if it loads the CF7 js script files,

add_action('wp_enqueue_scripts', 'pre_register_cf7_files');
function pre_register_cf7_files(){
  wp_register_style('contact-form-7', wpcf7_plugin_url( 'includes/css/styles.css' ), array(), WPCF7_VERSION, 'all' );
  wp_register_script( 'contact-form-7', wpcf7_plugin_url( 'includes/js/scripts.js' ), array( 'jquery' ), WPCF7_VERSION, true);
}

If this does not work, your only solution will be to hook your custom_before_add_to_cart_btn function earlier in the request sequence.

Not the answer you're looking for? Browse other questions tagged or ask your own question.