将所选产品变体数据传递到 Contact Form 7 查询表中

Pass the chosen product variations data into Contact Form 7 enquiry form

对于 WooCommerce,我使用 Contact Form 7 和 Product Info Request 插件在单个产品页面中添加一个表单,因为我需要一个允许用户发送有关产品的查询请求的功能(认为是简单的联系表单).

看到这个截图你就明白了:

我所有的产品都是可变产品 (来自属性)

有没有办法检索客户 selected 的变体并通过联系表 7 发送?

例如:

用户 select 颜色黑色和尺码 s,然后填写表格,当电子邮件发送时,除了接收经典信息(姓名、电子邮件 ecc..)我还收到属性 selected(在本例中为 blacks

UPDATED: Added WC 3+ compatibility

我已经对其进行了测试,它不会发送与所选变体相关的任何数据,因为它只是在 add-to-cart 按钮下方(在单个产品页面中)输出所选的联系表格。此外,此插件已超过 2 年未更新,因此有点过时了。

THE GOOD NEW: A WORKING SOLUTION

我找到了这个相关的答案: Product Name WooCommerce in Contact Form 7

它解释了如何在产品选项卡中设置 contact form 7 短代码并在电子邮件中显示所选的产品标题。

所以根据这个答案,我已经转换了代码,就像插件一样使用它(就在添加到购物车按钮下方)。

在这个 example/answer 中,我为产品变体设置了可变产品 2 属性:ColorSize.

这是我将在我的代码中使用的表单的设置 Contact form 7

<label> Your Name (required)
    [text* your-name] </label>

<label> Your Email (required)
    [email* your-email] </label>

<label> Subject (required)
    [text* your-subject class:product_name] </label>

<label> Your Message
    [textarea your-message] </label>

[submit "Send"]

[text your-product class:product_details]

我在这里添加了这个文本字段[text your-product class:product_details]。因此,您还需要在“邮件”设置选项卡中添加 [your-product] 标签中的“消息 body”,以便在您的电子邮件中获取:

From: [your-name] <[your-email]>
Subject: [your-subject]

Product: [your-product]

Message Body:
[your-message]

--------------
This e-mail was sent from a contact form 7

woocommerce_after_add_to_cart_form动作挂钩中挂钩的PHP代码自定义函数:

add_action( 'woocommerce_after_add_to_cart_form', 'product_enquiry_custom_form' );
function product_enquiry_custom_form() {

    global $product, $post;

    // Set HERE your Contact Form 7 shortcode:
    $contact_form_shortcode = '[contact-form-7 id="382" title="form"]';

    // compatibility with WC +3
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    $product_title = $post->post_title;

    // The email subject for the "Subject Field"
    $email_subject = __( 'Enquire about', 'woocommerce' ) . ' ' . $product_title;

    foreach($product->get_available_variations() as $variation){
        $variation_id = $variation['variation_id'];
        foreach($variation['attributes'] as $key => $value){
            $key = ucfirst( str_replace( 'attribute_pa_', '', $key ) );
            $variations_attributes[$variation_id][$key] = $value;
        }
    }
    // Just for testing the output of $variations_attributes
    // echo '<pre>'; print_r( $variations_attributes ); echo '</pre>';


    ## CSS INJECTED RULES ## (You can also remve this and add the CSS to the style.css file of your theme
    ?>
    <style>
        .wpcf7-form-control-wrap.your-product{ opacity:0;width:0px;height:0px;overflow: hidden;display:block;margin:0;padding:0;}
    </style>

    <?php


    // Displaying the title for the form (optional)
    echo '<h3>'.$email_subject.'</h3><br>
        <div class="enquiry-form">' . do_shortcode( $contact_form_shortcode ) . '</div>';


    ## THE JQUERY SCRIPT ##
    ?>
    <script>
        (function($){

            <?php
                // Passing the product variations attributes array to javascript
                $js_array = json_encode($variations_attributes);
                echo 'var $variationsAttributes = '. $js_array ;
            ?>

            // Displaying the subject in the subject field
            $('.product_name').val('<?php echo $email_subject; ?>');

            ////////// ATTRIBUTES VARIATIONS SECTION ///////////

            var $attributes;

            $('td.value select').blur(function() {
                var $variationId = $('input[name="variation_id"]').val();
                // console.log('variationId: '+$variationId);
                if (typeof $variationId !== 'undefined' ){
                    for(key in $variationsAttributes){
                        if( key == $variationId ){
                            $attributes = $variationsAttributes[key];
                            break;
                        }
                    }

                }
                if ( typeof $attributes !== 'undefined' ){
                    // console.log('Attributes: '+JSON.stringify($attributes));
                    var $attributesString = '';
                    for(var attrKey in $attributes){
                        $attributesString += ' ' + attrKey + ': ' + $attributes[attrKey] + ' — ';
                    }
                   $('.product_details').val( 'Product <?php echo $product_title; ?> (ID <?php echo $product_id; ?>): ' + $attributesString );
                }
            });

        })(jQuery);
    </script>

    <?php
}

代码进入您的活动 child 主题(或主题)的 function.php 文件或任何插件文件。

You will get exactly what the plugin was doing with that additionals features:

  • A custom product title, as subject of the mail.
  • The selected variation attributes Name label + values in the additional fiels (that will be hidden).

以下是我的测试服务器的屏幕截图:

具有所选属性的产品:

我在表格上得到了什么(我没有隐藏特殊的文本字段来向您显示 jQuery 提取的数据):

如您所见,您已获得需要在电子邮件中发送的数据……

一旦我选择了产品的属性并填写了表格的其他字段,当我提交此表格时,我收到了这封电子邮件:

From: John Smith <j.smith@themain.com>
Subject: Enquire about Ship Your Idea

Product: Product Ship Your Idea (ID 40):  Color: black —  Size: 12 —

Message Body:
I send this request about this very nice product … I send this request about this very nice product …

--
This e-mail was sent from a contact form 7

所以一切都按照您的预期工作,这是一个经过测试的有效示例答案。