Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: CF7 form to all products (after short description, with add_action)
Plugin URI: http://www.damiencarbery.com
Description: Append a Contact Form 7 form to the Short Description section of all Woocommerce products.
Author: Damien Carbery
Version: 0.1
WC tested up to: 5.3.0
*/
// Add the CF7 form between the short description and the 'Add to cart' button.
add_action( 'woocommerce_single_product_summary', 'dcwd_add_cf7_form_after_single_excerpt', 25 );
function dcwd_add_cf7_form_after_single_excerpt() {
echo do_shortcode('[contact-form-7 id="73" title="Contact form 1"]'); ;
}
<?php
/*
Plugin Name: CF7 form to all products (after short description, with add_filter)
Plugin URI: http://www.damiencarbery.com
Description: Append a Contact Form 7 form to the Short Description section of all Woocommerce products.
Author: Damien Carbery
Version: 0.1
WC tested up to: 5.3.0
*/
// Add the CF7 form between the short description and the 'Add to cart' button.
add_filter('woocommerce_short_description', 'dcwd_append_cf7_form_to_short_description');
function dcwd_append_cf7_form_to_short_description( $short_description ) {
return $short_description . do_shortcode('[contact-form-7 id="73" title="Contact form 1"]');
}
<?php
/*
Plugin Name: CF7 to product page
Plugin URI: https://www.damiencarbery.com/2018/06/customise-choose-an-option-dropdown-item/
Description: Add a Contact Form 7 form to the product page.
Author: Damien Carbery
Version: 0.1
WC tested up to: 5.3.0
*/
add_action( 'woocommerce_after_add_to_cart_button', 'dcwd_add_cf7_form_under_add_to_cart' );
function dcwd_add_cf7_form_under_add_to_cart() {
// Add the hidden contact form. It will be displayed when the button is clicked.
echo '<div id="askinfo" style="display:none">' . do_shortcode( '[contact-form-7 id="73" title="Contact form 1"]' ) . '</div>';
// Button to open the popup.
echo '<button><a href="#TB_inline?height=600&width=400&inlineId=askinfo" class="thickbox">Ask for Info</a></button>';
// Add the thickbox JS and CSS for the popup.
add_thickbox();
}
[dynamichidden page-url "CF7_URL"]
[dynamichidden site-url "CF7_bloginfo show='url'"]
[dynamichidden post-id "CF7_get_post_var key='ID'"]
<p>SKU<br />
[dynamictext product-sku "CF7_get_custom_field key='_sku'"]</p>
In the email:
SKU: [product-sku]
<?php
/*
Plugin Name: CF7 to product page (specified categories)
Plugin URI: https://www.damiencarbery.com/2018/06/customise-choose-an-option-dropdown-item/
Description: Add a Contact Form 7 form to the product page of products in specified categories only.
Author: Damien Carbery
Version: 0.1
WC tested up to: 5.3.0
*/
add_action( 'woocommerce_after_add_to_cart_button', 'dcwd_add_cf7_form_under_add_to_cart' );
function dcwd_add_cf7_form_under_add_to_cart() {
// Add the contact form for products in the tshirts category.
if ( has_term( array('tshirts', 'accessories' ), 'product_cat' ) ) {
echo do_shortcode( '[contact-form-7 id="73" title="Contact form 1"]' );
}
}
Page URL: [page-url]
Edit URL: [site-url]/wp-admin/post.php?post=[post-id]&action=edit
[contact-form-7 id="27" title="Product Enquiry"]
[dynamichidden product-name "CF7_get_post_var key='title'"]
<label>Product Name
[dynamictext product-name "CF7_get_post_var key='title'"]</label>
<?php
/*
Plugin Name: CF7 form to All Products
Plugin URI: http://www.damiencarbery.com
Description: Append a Contact Form 7 form to the Product Description section of all Woocommerce products.
Author: Damien Carbery
Version: 0.2
WC tested up to: 7.0.1
*/
add_filter('the_content', 'dc_cf7_form_to_all_products');
function dc_cf7_form_to_all_products($content) {
if ( class_exists( 'woocommerce' ) && is_product() && is_main_query() ) { // Check suggested by: https://pippinsplugins.com/playing-nice-with-the-content-filter/
return $content . '[contact-form-7 id="8018" title="Product Enquiry"]';
}
return $content;
}
// Ensure the Description tab is present as it is needed by the_content filter.
add_filter( 'woocommerce_product_tabs', 'dc_cf7_add_description_tab', 20 );
function dc_cf7_add_description_tab( $tabs ) {
global $product, $post;
// Description tab - add if there is no post content (i.e. if the Description is empty)
// because woocommerce_default_product_tabs() in woocommerce/includes/wc-template-functions.php
// excludes it if the post content is empty.
if ( ! $post->post_content ) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab',
);
}
return $tabs;
}
<?php
add_filter('the_content', 'dc_cf7_form_to_all_products');
function dc_cf7_form_to_all_products($content) {
if ( class_exists( 'woocommerce' ) && is_product() && is_main_query() ) { // Check suggested by: https://pippinsplugins.com/playing-nice-with-the-content-filter/
global $post;
$product = wc_get_product($post);
// Only display the form when the product is in stock.
if ( $product->is_in_stock() ) {
return $content . '[contact-form-7 id="27" title="Product Enquiry"]';
}
}
return $content;
}
<?php
/*
Plugin Name: CF7 form to All Products
Plugin URI: http://www.damiencarbery.com
Description: Append a Contact Form 7 form to the Short Description section of all Woocommerce products.
Author: Damien Carbery
Version: 0.1
WC tested up to: 5.3.0
*/
add_filter('woocommerce_short_description', 'dc_cf7_form_to_all_products');
function dc_cf7_form_to_all_products($content) {
if ( class_exists( 'woocommerce' ) && is_product() && is_main_query() ) { // Check suggested by: https://pippinsplugins.com/playing-nice-with-the-content-filter/
return $content . '[contact-form-7 id="27" title="Product Enquiry"]';
}
return $content;
}
[03-Feb-2017 20:53:18 UTC] PHP Fatal error: Call to undefined function is_product() in /wp-content/plugins/wc-cf7-form-to-all-products.php on line 14
@sergigonza
Copy link

Hello, I want show form in specific category product, but i don´t know how do it ...

I know I have to modify the following if, but I do not know how to filter by category:

if ( $product->is_in_stock() ) {

Can you help me?

@damiencarbery
Copy link
Author

add_action( 'woocommerce_after_shop_loop', 'dc_cf7_form_to_category_archive', 40 );
function dc_cf7_form_to_category_archive() {
	if ( is_product_category( array( 'category_1', 'category_2' ) ) ) {
		echo do_shortcode( '[contact-form-7 id="65" title="Contact form 1"]' );
	}
}

To apply to all categories you can remove the is_product_category() check.
The 'woocommerce_after_shop_loop' is only on category pages so the function doesn't need to check whether it is in a product category page.

@KalayaanCrypto
Copy link

Hey there,

Thanks so much for this, it's very helpful.
I have one question:

How can I show form in specific category in the short description?

Thanks!
Cheers,
KalayaanCrypto.

@damiencarbery
Copy link
Author

@KalayaanCrypto, Use the first gist and add something like:
if ( has_term( array('tshirts', 'accessories' ), 'product_cat' ) ) { echo do_shortcode('[contact-form-7 id="73" title="Contact form 1"]'); }

@KalayaanCrypto
Copy link

Thanks for this man!

@skasterbg
Copy link

Hi is it possible to hide the form when product is "out of stock'. Thanks in advance

@damiencarbery
Copy link
Author

Hi is it possible to hide the form when product is "out of stock'. Thanks in advance

Look at the wc-cf7-form-to-in-stock-products.php snippet - it uses
if ( $product->is_in_stock() ) { }
to see if the product is in stock and only includes the shortcode if it is in stock.

Which snippet are you using?

@skasterbg
Copy link

Hi thanks for response. Im using this snippet to show the form in all products:

add_action( 'woocommerce_single_product_summary', 'dcwd_add_cf7_form_after_single_excerpt', 25 );
function  dcwd_add_cf7_form_after_single_excerpt() {
  echo do_shortcode('[contact-form-7 id="4421" title="Бърза поръчка"]'); ;
}

But when insert the wc-cf7-form-to-in-stock-products.php snippet the form is disappeared. I don't know what is wrong because the snippet is in my function.php file in child theme.

@skasterbg
Copy link

Hi again I saw the problem :) the form is displayed in main description tab but my snippet is for product short description. Where should I edit the code to appear in the short description ? Tnks in advance.

@skasterbg
Copy link

I found the solution ! In my case i'm using "action", the sniper is with 'filter" when im change the code the form is appeared short description,
Thanks for the snippets they were very useful to me !

@damiencarbery
Copy link
Author

I found the solution ! In my case i'm using "action", the sniper is with 'filter" when im change the code the form
is appeared short description, Thanks for the snippets they were very useful to me !
I'm glad you got it fixed.
I use the the_content filter in wc-cf7-form-to-all-products-php. It adds a check to verify that the Description tab is available. You can add the
if ( $product->is_in_stock() ) { }
check to that snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment