Skip to content

Instantly share code, notes, and snippets.

  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save damiencarbery/f7db36de7a875b6b9b4d10ff61597f60 to your computer and use it in GitHub Desktop.
<?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: 8.0.2
*/
// 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: 8.0.2
*/
// 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: 8.0.2
*/
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();
}
[dynamic_hidden page-url "CF7_URL"]
[dynamic_hidden site-url "CF7_bloginfo show='url'"]
[dynamic_hidden post-id "CF7_get_post_var key='ID'"]
<p>SKU<br />
[dynamic_text 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: 8.0.2
*/
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"]
[dynamic_hidden product-name "CF7_get_post_var key='title'"]
<label>Product Name
[dynamic_text 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: 8.0.2
*/
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: 8.0.2
*/
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

damiencarbery commented Oct 18, 2021

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.

@Shahriq
Copy link

Shahriq commented May 29, 2023

Can anyone help me out from this problem:
I have a question, that I have created a form from "contact form 7" with some services dropdown menu and every service has their own price It calculates from the "cost calculator Contact form 7 plugin" and shows the total quite under the form and I made a add to cart label to submit button, now I want is that when I click the add to cart button in the form then it will send the total price (which is in the total field) to the cart page like as a product goes in, but I don't want to make the product in Woo-Commerce. How it can be possible? Please guide me!!!

@damiencarbery
Copy link
Author

Can anyone help me out from this problem: I have a question, that I have created a form from "contact form 7" with some services dropdown menu and every service has their own price It calculates from the "cost calculator Contact form 7 plugin" and shows the total quite under the form and I made a add to cart label to submit button, now I want is that when I click the add to cart button in the form then it will send the total price (which is in the total field) to the cart page like as a product goes in, but I don't want to make the product in Woo-Commerce. How it can be possible? Please guide me!!!

Are you using WooCommerce on your website?
If you are then I think that a variable product would be a good solution. The product's variations would be the services and they can have different prices.

@Shahriq
Copy link

Shahriq commented May 29, 2023

Thank you so much to answer me. Yes, I use Woo-Commerce on my site, I have been made form in contact form 7 below:
Contact form
I want to redirect the form to the cart page with a price which shows in the total field. You can see my form, I've attached it.

@Shahriq
Copy link

Shahriq commented May 29, 2023

Can You tell me the way to redirect to the cart page through the button which I changed submit to Add to cart.

@damiencarbery
Copy link
Author

Can You tell me the way to redirect to the cart page through the button which I changed submit to Add to cart.

Redirect to cart is not hard - the CF7 docs show how to do it: https://contactform7.com/redirecting-to-another-url-after-submissions/

This redirect will not show the price. I think that you should use a variable product. CF7 is not a good solution for this.
Why do you not use a variable product?

@Shahriq
Copy link

Shahriq commented May 29, 2023

OK, Thanks Now I will use variable product:)

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