• Hello

    I would like to display the product name in the email body in the following format: Product Title: [productname]

    I was using the following code, which worked fine with an older version of Contact Form 7. However, after updating the plugin to a higher version, this code started causing a ‘500 Internal Server Error’ at wp-json/contact-form-7/v1/contact-forms/550/feedback.

    function add_product_names_to_contact_form( $posted_data ) {
    $product_names = array();
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product = apply_filters( ‘woocommerce_cart_item_product’, $cart_item[‘data’], $cart_item, $cart_item_key );
    $product_names[] = $_product->get_name();
    }

    // Set the product names in the contact form data
    $posted_data[‘productname’] = implode( ‘, ‘, $product_names );
    return $posted_data;
    }
    add_filter( ‘wpcf7_posted_data’, ‘add_product_names_to_contact_form’ );

    In this situation, how can I retrieve the product name and include it in the email body?”

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Where can we see the website in question?

    Thread Starter vrushalli22

    (@vrushalli22)

    Hello,

    The form is on the cart page at https://kwebmakerdigitalagency.com/staging_ult/cart/. It’s a hidden form with a popup modal.

    Currently, I’ve made some changes in the cart template and contact form. I’ve added a hidden field and JavaScript code. Despite these modifications, the product name is still not appearing in the email body. Interestingly, I can see the product name if I use an echo statement.

    contact form
    [hidden product-names-field id:product-names-field]
    
    cart template code
    <?php
    $product_names = array(); // Initialize an array to store product names
    
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
         $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
        $e_product = wc_get_product( $product_id);
    // Get the product name
            $product_name = $e_product->get_name();
            
            // Output the product name
            echo $product_name . ', ';
            
            // Store the product name in the array
            $product_names[] = $product_name;
        } 
        ?>
        
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                // Populate the hidden input field with product names
                var productNamesField = document.getElementById('product-names-field');
                if (productNamesField) {
                    productNamesField.value = '<?php echo esc_js(implode(', ', $product_names)); ?>';
                }
            });
        </script>
    </div>
    Plugin Author Takayuki Miyoshi

    (@takayukister)

    [hidden product-names-field id:product-names-field]

    Where in the form do you have this tag?

    Thread Starter vrushalli22

    (@vrushalli22)

    Above submit button, please refer code.

    <div class="col-12 mb-1">
    [text* text id:name class:form-control class:rounded-0 placeholder "Name*"]
    </div>
    <div class="col-12 mb-1">
        [email* email id:email class:form-control class:rounded-0 placeholder "Email *"]                                  
    </div>
    <div class="col-12 mb-1">
        [tel* Phone id:phone class:form-control class:rounded-0 minlength:10 maxlength:10 placeholder "Phone Number *"]  
    </div>
    <div class="col-12 mb-1">
        [textarea Message id:floatingTextarea class:form-control class:rounded-0 40x2 placeholder "Message"]                              
    </div>
    <div class="col-12 mb-1 d-none">
        [hidden product-names-field id:product-names-field]
    </div>
      <div class="blackBorderBtn d-table px-2 py-0 form-button mt-5">
    [submit "Submit"]
    </div>
    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Where on the site do you have the form having the issue?

    Thread Starter vrushalli22

    (@vrushalli22)

    The product name is not being fetched in the email that we receive as admin

    Full Name: test
    Email ID : test@gmaill.com
    
    Mobile No. : 8888888888
    
    Message : test
    
    Product Title: [product_names_field]
    • This reply was modified 1 year, 1 month ago by vrushalli22.
    • This reply was modified 1 year, 1 month ago by vrushalli22.
    Thread Starter vrushalli22

    (@vrushalli22)

    Hello,

    can anyone help me with this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to fetch product name to contact form mail body’ is closed to new replies.