• Resolved ariom06

    (@ariom06)


    Hi Tessa,
    thank you for your job, i do really appreciate it
    my question:
    we use this shortcode on a cf7 form for user to ask for one or more of our programs

    [dynamic_checkbox inputname use_label_element "au_dtx_demo_get_viaggi_di_gruppo_checkbox_options"]
    /**
     * Contact Form 7 - richiesta programma viaggio di gruppo
     * Get WordPress Product Options for DTX Select Field
     * Get Woo Product Options for DTX checkbox Field
     * @return string JSON encoded checkbox options of key/value pairs to represent value/label pairs.
     */
    function au_dtx_demo_get_viaggi_di_gruppo_checkbox_options()
    {
        $options = array();
        $products = get_posts(array(
            'fields' => 'ids',
            'post_type' => 'product',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => 'ASC',
            'ignore_sticky_posts' => false, // move sticky posts to the start of the set
            'tax_query' => array(array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'viaggi-di-gruppo',
                'include_children' => true,
                'operator' => 'IN'
            ))
        ));
        if (is_array($products) && count($products)) {
            foreach ($products as $product_id) {
                $options[$product_id] = get_the_title($product_id);
            }
        }
        return json_encode($options);
    }
    add_shortcode('au_dtx_demo_get_viaggi_di_gruppo_checkbox_options', 'au_dtx_demo_get_viaggi_di_gruppo_checkbox_options');
    //-----------------------------------------------

    it is working fine as it should, and it is correctly retrieving the titles of the products with checkboxes on the form, but in the email we receice only the ID’s of the products, but we would need the title or the SKU instead of ID.
    Tried to change fields value “ids” with “names” but everything was messed up….
    Can you help please?
    thank you in advance for your time
    Mario

Viewing 2 replies - 1 through 2 (of 2 total)
  • Sure! So the form is going to remain the same, you’ll want to keep the product ID as the saved reference for your checkboxes. What you’ll want to do instead is add a function to modify what is displayed in the email, to look up the product name/SKU from the product ID that was selected. You can do that by hooking into wpcf7_before_send_mail like this:

    /**
     * Custom Email Behavior on Form Submission
     *
     * Send Product Name and SKU instead of ID
     *
     * @param WPCF7_ContactForm $form The current contact form
     *
     * @return void
     */
    function ariom06_display_product_info($form)
    {
        $field_name = 'inputname'; // Matches what you named the dynamic_checkbox form tag
        if (array_key_exists($field_name, $_POST) && is_array($product_ids = $_POST[$field_name]) && count($product_ids)) {
    
            // Get the Product Details
            $product_html = '<ol>'; // Open ordered list
            foreach ($product_ids as $product_id) {
                // Sanitize and validate product ID
                if (($product_id = intval(sanitize_text_field($product_id))) > 0) {
                    // Add product information as list item, assuming you're using WooCommerce
                    $product_html .= sprintf(
                        '<li>%s - %s</li>',
                        esc_html(get_the_title($product_id)),
                        esc_html(get_post_meta($product_id, '_sku', true))
                    );
                }
            }
            $product_html .= '</ol>'; // Close ordered list
    
            // Display the Product Details in the Emails
            $shortcode = sprintf('[%s]', $field_name);
            $mail = $form->prop('mail'); // Get the first mail property
            if ($mail['active']) {
                // If the first email is active, replace the shortcode with the value in the body of the email
                $mail['body'] = str_replace($shortcode, $product_html, $mail['body']);
                $mail['use_html'] = true; // Force the email to encode HTML so the list appears correctly
            }
            $mail2 = $form->prop('mail_2'); // Get the second mail property
            if ($mail2['active']) {
                // If the second email is active, replace the shortcode with the value
                $mail2['body'] = str_replace($shortcode, $product_html, $mail2['body']);
                $mail2['use_html'] = true; // Force the email to encode HTML so the list appears correctly
            }
    
            // Update the form with the updated values
            $form->set_properties(array(
                'mail' => $mail,
                'mail_2' => $mail2
            ));
        }
    }
    add_action('wpcf7_before_send_mail', 'ariom06_display_product_info');

    This snippet should replace all instances of [inputname] in the mail template with an ordered list of product names and their SKUs that were selected. Let me know if you have any issues!

    Thread Starter ariom06

    (@ariom06)

    Hi Tessa,
    Just to tell you that your snippet it is working fine just out of the box, no issue at all!
    And your Cf7 Dtx plugin is absolutly a must!
    Thank you very much for your help and for your effort on mantaining this awesome plugin….
    Mario

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get products titles or COD/SKU instead of ID’s on submitted email’ is closed to new replies.