Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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: 3.5.5
*/
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]
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.1
WC tested up to: 3.5.5
*/
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="27" title="Product Enquiry"]';
}
return $content;
}
<?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: 3.5.5
*/
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

This comment has been minimized.

Copy link

commented Dec 27, 2017

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

This comment has been minimized.

Copy link
Owner Author

commented Dec 28, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.