将一些WooCommerce产品数据传递给联系表7查询表 [英] Pass some WooCommerce product data to Contact Form 7 enquiry form
问题描述
After Display a form when the selected variation is out of stock in WooCommerce answer to my previous answer, I can display a form made with Contact Form 7 plugin, on the "Out of Stock" products of my WooCommerce store. It works for simple and variable products (product variations).
现在,我想将一些产品数据传递给此查询联系表,例如此答案:
通过选择的产品变体数据进入联系表7查询表 .
Now I would like to pass some product data to this enquiry contact form like in this answer:
Pass the chosen product variations data into Contact Form 7 enquiry form.
我想传递产品名称(以及变化属性颜色"值,以我的母语为"Renk").我尝试了所有可能的方式,但没有成功.
I would like to pass the product name (and the variation attribute "Color" value, which is "Renk" in my native language). I tried my self in all possible ways, but without any success.
任何帮助将不胜感激.
推荐答案
由于联系表单7允许隐藏输入字段,因此您将在表单中为商品数据(在提交"按钮之后)设置一个隐藏输入字段,例如:
As Contact Form 7 allows hidden input field, you will set a hidden input field in your form for the product data (after the submit button) like:
<label> Your Name (required)
[text* your-name] </label>
<label> Your Email (required)
[email* your-email] </label>
<label> Your Message
[textarea* your-message] </label>
[submit "Send"]
[hidden your-product]
然后您将使用以下方法将其添加到邮件中:
Then You will add it to the mail using for example:
Product Enquiry: [your-product]
现在jQuery会将产品名称(和属性Color值)复制到此隐藏字段中.将您所有现有的相关代码替换为以下内容:
Now jQuery will copy the product name (and the attribute Color value) to this hidden field. Replace your all existing related code with the following:
add_action( 'woocommerce_single_product_summary', 'add_product_outofstock_contact_form', 30, 2 );
function add_product_outofstock_contact_form() {
global $product;
$contact_form = do_shortcode('[contact-form-7 id="14880" title="Fiyat Sorunuz"]'); // Here the contact form shortcode
if( $product->is_type('variable') ) {
echo '<div class="outofstock-form" style="display:none">' . $contact_form . '</div>';
} elseif( ! $product->is_in_stock() ) {
echo $contact_form;
?>
<script type="text/javascript">
jQuery(function($) {
var id = <?php echo $product->get_id(); ?>,
name = '<?php echo $product->get_name(); ?>';
$('input[name="your-product"]').val(name+' ('+id+')');
});
</script>
<?php
}
}
add_filter( 'woocommerce_available_variation', 'filter_available_variation_attributes', 10, 3 );
function filter_available_variation_attributes( $data, $product, $variation ){
if ( ! $data['is_in_stock'] ) {
$attribute = 'Color';
$term_name = $variation->get_attribute($attribute);
$data['name'] = $product->get_name();
$data['name'] .= $term_name ? ' - ' . $term_name : '';
}
return $data;
}
add_action('woocommerce_after_variations_form', 'outofstock_product_variation_js');
function outofstock_product_variation_js() {
?>
<script type="text/javascript">
jQuery(function($) {
var contactFormObject = $('.outofstock-form'),
addToCartButtonObj = $('.woocommerce-variation-add-to-cart'),
hiddenInputFieldObj = $('input[name="your-product"]');
$('form.variations_form').on('show_variation', function(event, data) { // On selected variation
if ( ! data.is_in_stock ) {
addToCartButtonObj.hide('fast');
contactFormObject.show('fast');
hiddenInputFieldObj.val(data.name+' ('+data.variation_id+')');
} else {
addToCartButtonObj.show('fast');
contactFormObject.hide('fast');
hiddenInputFieldObj.val('');
}
}).on('hide_variation', function() { // Not on selected variation
addToCartButtonObj.show('fast');
contactFormObject.hide('fast');
hiddenInputFieldObj.val('');
});
});
</script>
<?php
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
注意:该代码适用于简单产品和可变产品(产品变体).
Note: The code works for simple products and for variable products (product variations).
相关:
这篇关于将一些WooCommerce产品数据传递给联系表7查询表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!