【问题标题】:Pass the chosen product variations data into Contact Form 7 enquiry form将选择的产品变化数据传递到联系表 7 查询表中
【发布时间】:2017-06-29 03:29:54
【问题描述】:

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

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

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

有什么方法可以检索客户选择的变体并通过联系表 7 发送?

例如:

用户选择黑色和大小 s,然后填写表格和发送电子邮件时,除了接收经典信息(名称,电子邮件 ecc..)我还收到选择的属性(在这种情况下 blacks)

【问题讨论】:

  • 嗨 LoicTheAztec,首先非常感谢。我尝试遵循所有步骤,但是当我填写表格时,我没有收到邮件的变化。我在模块选项卡中添加 [text your-product class:product_details] 并在邮件选项卡中添加 Product: [your-product]。
  • 您的解决方案很棒,正是您想要的,但它不适用于 woocommerce 3.5.4,字段“[text your-product class: product_details]”不会出现在形式。可以根据该版本对其进行调整。谢谢。

标签: php wordpress woocommerce contact-form-7 variations


【解决方案1】:

更新: 添加了 WC 3+ 兼容性

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

好消息:可行的解决方案

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

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

因此,我从这个答案中转置了代码,就像插件一样使用它(就在添加到购物车按钮下方)。

在此示例/答案中,我在可变产品 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] 标签中的“邮件正文”,以便在您的电子邮件中获取:

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
}

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

您将准确了解该插件使用这些附加功能所做的事情:

  • 自定义产品标题,作为邮件主题。
  • 选择的变体属性名称标签+值在附加字段中(将被隐藏)。

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

具有选定属性的产品:

我在表单上得到的信息(我没有隐藏特殊的文本字段来向您展示 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

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

【讨论】:

  • 有什么方法可以获取所选版本的价格吗?
猜你喜欢
  • 2021-05-30
  • 1970-01-01
  • 2015-05-03
  • 2015-09-22
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 2021-10-15
  • 2018-12-03
相关资源
最近更新 更多