Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active August 27, 2023 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/94b5d4faff15a4e6325154a6cecb3d19 to your computer and use it in GitHub Desktop.
Save damiencarbery/94b5d4faff15a4e6325154a6cecb3d19 to your computer and use it in GitHub Desktop.
Shortcode for Contact Form 7 Dynamic Text Extension - CF7DTE is great but sometimes you need a custom shortcode to get product info into the form. https://www.damiencarbery.com/2019/11/shortcode-for-contact-form-7-dynamic-text-extension/
[dynamic_text sku-from-shortcode "product_sku"]
<?php
/*
Plugin Name: CF7DTE shortcode for product categories
Plugin URI: https://www.damiencarbery.com/2019/11/shortcode-for-contact-form-7-dynamic-text-extension/
Description: Custom shortcode to retrieve product categories.. For use with WooCommerce Quote or Enquiry Contact Form 7 plugin and Contact Form 7 Dynamic Text Extension.
Author: Damien Carbery
Version: 0.2
WC tested up to: 8.0.2
*/
add_shortcode( 'product_cats', 'dcwd_product_categories_for_cf7' );
function dcwd_product_categories_for_cf7() {
$product_id = get_the_ID();
// If using "WooCommerce Quote or Enquiry Contact Form" plugin by Geek Web Solution.
if ( ( false == $product_id ) && function_exists( 'wqoecf_include_front_script' ) ) {
$product_id = isset( $_POST[ 'product_id' ] ) ? (int)sanitize_text_field( $_POST[ 'product_id' ] ) : false;
}
// If using "Get a Quote Button for WooCommerce" plugin by WpBean.
if ( ( false == $product_id ) && ( class_exists( 'WPB_Get_Quote_Button' ) ) ) {
$product_id = isset( $_POST[ 'wpb_post_id' ] ) ? (int)sanitize_text_field( $_POST[ 'wpb_post_id' ] ) : false;
}
// Strip the html tags to get a plain comma separated list.
return strip_tags( wc_get_product_category_list( $product_id ) );
}
<?php
/*
Plugin Name: CF7DTE shortcode for product sku
Plugin URI: https://www.damiencarbery.com/2019/11/shortcode-for-contact-form-7-dynamic-text-extension/
Description: Custom shortcode to retrieve product sku via the $_POST['product_id']. For use with WooCommerce Quote or Enquiry Contact Form 7 plugin and Contact Form 7 Dynamic Text Extension.
Author: Damien Carbery
Version: 0.3
WC tested up to: 8.0.2
*/
// Retrieve product SKU.
add_shortcode( 'product_sku', 'dcwd_product_sku_from_produt_id' );
function dcwd_product_sku_from_produt_id() {
$product_id = get_the_ID();
// If using "WooCommerce Quote or Enquiry Contact Form" plugin by Geek Web Solution.
if ( ( false == $product_id ) && function_exists( 'wqoecf_include_front_script' ) ) {
$product_id = isset( $_POST[ 'product_id' ] ) ? (int)sanitize_text_field( $_POST[ 'product_id' ] ) : false;
}
// If using "Get a Quote Button for WooCommerce" plugin by WpBean.
if ( ( false == $product_id ) && ( class_exists( 'WPB_Get_Quote_Button' ) ) ) {
$product_id = isset( $_POST[ 'wpb_post_id' ] ) ? (int)sanitize_text_field( $_POST[ 'wpb_post_id' ] ) : false;
}
$product_sku = 'unknown';
if ( is_int( $product_id ) ) {
$product_sku = get_post_meta( $product_id, '_sku', true );
}
return $product_sku;
}
<?php
/*
Plugin Name: CF7DTE shortcode for product name
Plugin URI: https://www.damiencarbery.com/2019/11/shortcode-for-contact-form-7-dynamic-text-extension/
Description: Custom shortcode to retrieve product name via the $_POST['product_id']. For use with WooCommerce Quote or Enquiry Contact Form 7 plugin and Contact Form 7 Dynamic Text Extension.
Author: Damien Carbery
Version: 0.1
WC tested up to: 8.0.2
*/
// Retrieve product name.
add_shortcode( 'product_name', 'dcwd_product_name_from_produt_id' );
function dcwd_product_name_from_produt_id() {
$product_id = get_the_ID();
// If using "WooCommerce Quote or Enquiry Contact Form" plugin by Geek Web Solution.
if ( ( false == $product_id ) && function_exists( 'wqoecf_include_front_script' ) ) {
$product_id = isset( $_POST[ 'product_id' ] ) ? (int)sanitize_text_field( $_POST[ 'product_id' ] ) : false;
}
// If using "Get a Quote Button for WooCommerce" plugin by WpBean.
if ( ( false == $product_id ) && ( class_exists( 'WPB_Get_Quote_Button' ) ) ) {
$product_id = isset( $_POST[ 'wpb_post_id' ] ) ? (int)sanitize_text_field( $_POST[ 'wpb_post_id' ] ) : false
}
$product_name = 'unknown';
if ( is_int( $product_id ) ) {
$product_name = get_the_title( $product_id );
}
return $product_name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment