将选定的产品变体数据传递到Contact Form 7查询表
随着WooCommerce,我使用Contact Form 7和Product Info Request插件在单个产品页面内添加一个表单,因为我需要一个功能,允许用户发送查询请求产品(思想简单的联系表格)。将选定的产品变体数据传递到Contact Form 7查询表
你能理解看到这个截图:
我所有的产品都与变化可变产品(从属性)。
有什么办法来检索由客户选择的变化,并通过联系表格7发送?
例如:
用户选择黑色和大小S,然后填写表格,当电子邮件被发送,除了接受传统的信息(姓名,电子邮件ECC ..)我收到的还属性选择(在这种情况下black
和s
)
感谢。
更新:添加WC 3+兼容性
我已经测试它,它会不发送给所选择的变化相关的任何数据,因为它仅仅是输出下面添加选定的接触形式(单个产品页面)。另外这个插件自2年多以来一直没有更新,所以它有点过时了。
好新:工作解决方案
我发现这个相关答案:Product Name WooCommerce in Contact Form 7
它说明了如何在产品标签中设置一个接触的形式7简码和在电子邮件中显示所选产品的标题。
所以从这个答案我已经调换了代码,使用它就像插件在做(只是添加到购物车按钮的下方)。
在这里,在这个例子中/答案我在不同产品设置了变型产品2个属性:Color
和Size
。
这是我的设置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
的PHP代码定制funtion在woocommerce_after_add_to_cart_form
行动挂钩钩:
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文件或也以任何插件文件。
你会得到什么插件与该additionals特点做:
- 定制产品称号,作为邮件的主题。
- 选定的变体属性名称标签+值在额外的fiels(将被隐藏)。
这里是屏幕从我的测试服务器笋:
我得到的形式是什么(我不隐藏特殊文本字段向你展示jQuery提供的数据):
正如你所看到的,你会得到你需要发送的数据电子邮件...
一旦我选择了产品的属性,并填写表格的其他领域,当我提出这个形式我得到的,这个电子邮件消息:
From: John Smith <[email protected]> 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
所以正如你所期望的那样,永恒的工作正在进行,这是一个经过测试的示例答案。
嗨LoicTheAztec,首先非常感谢你。我尝试并遵循所有步骤,但是当我填写表格时,我没有收到邮件的变化。我在模块选项卡中添加[text your-product class:product_details],并在邮件选项卡中添加Product:[your-product]。 – emiliano