Adding a shortcode form in WooCommerce Category pages only

61 views Asked by At

I'm looking to add a contact form to appear under the contents of the WooCommerce Category pages only - not to appear in the product pages. I'm using the following code to display the form:

add_action( 'woocommerce_after_main_content', function(){
    // Title
    $header = "<br><b>Contact Us</b><br>";
    
    // Message
    $text = "<p>Please do not hesitate to contact us with queries you may have</p><br>";

    // WPForms shortcode
    $wpforms_shortcode = do_shortcode('[wpforms id="1385"]');

    // Output combined message and form
    echo '
' . $header .'
' . $text .'
' . $wpforms_shortcode .'
';
}, 9 );

The result can be viewed by following this link.

However, using the above code also makes the form appear at the bottom of the product pages - no surprise here.

How do I prevent the form from appearing at the bottom of the product pages?

I added the code and as expected saw the contact form at the bottom of all the shop pages. I need the form to only appear in the category pages.

1

There are 1 answers

0
LoicTheAztec On

To display your code output only in product category archives (pages) use the following:

add_action( 'woocommerce_after_main_content', 'display_product_category_contact_us_form', 9 );
function display_product_category_contact_us_form(){
    if ( is_product_category() ) {
        echo '<br>
        <b>'.__('Contact Us', 'woocommerce').'</b><br>
        <p>'.__('Please do not hesitate to contact us with queries you may have', 'woocommerce').'</p><br>
        '.do_shortcode('[wpforms id="1385"]');
    }
}

Code goes in functions.php file of your child theme (or in a plugin). Tested and works.