0

We are trying to display a cf7 form on woocommerce product pages, where users can submit the form and receive a pdf file related to the product (either as an email file attachment, as a link in the email body or even as a link that opens in a new browser tab).

So far we have tried adding the pdf link as a product attribute and returning it with a shortcode, but... shortcodes don't work on CF7 e-mails.

Does any of you know a way to return shortcodes on cf7 emails? Or maybe you have a better idea to do such a thing, because our idea doesn't seem that optimized.

And obviously we don't want to create one form per product, that would be a nightmare.

Here's the code used for our shortcode :

/**
 * Attributes shortcode callback.
 */
function so_39394127_attributes_shortcode( $atts ) {

    global $product;

    if( ! is_object( $product ) || ! $product->has_attributes() ){
        return;
    }

    // parse the shortcode attributes
    $args = shortcode_atts( array(
        'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
    ), $atts );

    // is pass an attributes param, turn into array
    if( is_string( $args['attributes'] ) ){
        $args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
    }

    // start with a null string because shortcodes need to return not echo a value
    $html = '';

    if( ! empty( $args['attributes'] ) ){

        foreach ( $args['attributes'] as $attribute ) {

            // get the WC-standard attribute taxonomy name
            $taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;

            if( taxonomy_is_product_attribute( $taxonomy ) ){

                // Get the attribute label.
                $attribute_label = wc_attribute_label( $taxonomy );

                // Build the html string with the label followed by a clickable list of terms.
                // Updated for WC3.0 to use getters instead of directly accessing property.
                $html .= get_the_term_list( $product->get_id(), $taxonomy); 
            }

        }

        // if we have anything to display, wrap it in a <ul> for proper markup
        // OR: delete these lines if you only wish to return the <li> elements

    }

    return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
3
  • 2
    Have you looked into using wpcf7_before_send_mail hook to append your content? This is a good example of a dynamic recipient - I'm sure it could be modified to append content as well -> stackoverflow.com/a/66885421/800452
    – Howdy_McGee
    Commented May 11, 2022 at 14:21
  • Hello @Howdy_McGee thanks for the resource. I'm no php dev, so it's going to take me 4 days to figure it out lol, but I'll try and see what I can do. Commented May 12, 2022 at 8:11
  • That's what development is though... ;-) Commented May 19, 2022 at 2:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.