更新: 添加了 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
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
所以一切都按您的预期工作,这是一个经过测试的示例答案。