在 Woocommerce 单品上显示管理员选择的 CF7

Display the chosen selected CF7 from admin on Woocommerce single products

对于自定义 WooCommerce 主题,我试图实现以下目标:

  1. 后端

    • 如果产品可以有产品查询表,用户可以select
    • 用户可以 select 来自下拉列表中单个产品的 CF7 表单。
  2. 前端

    • 单个产品页面显示标准产品信息,但如果选中 "product enquiry form",则单个产品页面显示一个按钮,上面写着 "Product Enquiry"
    • 单击该按钮后,会显示一个包含产品属性(尺寸、颜色等)的 CF7 表单

我正在努力实现的示例。 http://www.blumeta.nl/tuininrichting/houtopslag/cortenstaal-houtopslag-500-x-hoogte-x-380mm

我真的不知道该如何处理。任何人都可以帮助我如何实现这一目标。

感谢所有帮助。

下面的代码将显示在:

1) 后端单品设置"Advanced tab"一个select字段与你不同的CF7表单

您必须在第一个函数中设置您的 CF7 表单简码:

// Select field options: HERE set the CF7 shortcodes and labels in the array
function product_cf7_form_options() {
    $domain = 'woocommerce';

    return array(
        ''                          => __('Choose a form to enable it', $domain ), // Disabled choice
        '[contact-form-7 id="381"]' => __( 'First Form label name (or title)', $domain ),
        '[contact-form-7 id="382"]' => __( 'Second Form label name (or title)', $domain ),
    );
}

// Admin: Add custom field to product "advanced" setting tab
add_filter( 'woocommerce_product_options_advanced', 'add_field_product_options_advanced', 20, 1 );
function add_field_product_options_advanced() {
    $domain = 'woocommerce';

    echo '<div class="option_group">';

    woocommerce_wp_select( array(
        'id'      => '_enquiry_form',
        'label'   => __( 'Display a form (CF7)', $domain ),
        'desc_tip' => true,
        'description' => __( 'This will automatically insert your slider into the single product page', $domain ),
        'options'  => product_cf7_form_options(), // Get the array of options
    ) );

    echo '</div>';
}

// Admin: Save custom field value from product "advanced" setting tab
add_action( 'woocommerce_process_product_meta', 'save_product_custom_field', 20, 1 );
function save_product_custom_field( $post_id ){
    if( isset( $_POST['_enquiry_form'] ) )
        update_post_meta( $post_id, '_enquiry_form', $_POST['_enquiry_form'] );

}

2) 前端 单个产品页面下方的添加到购物车按钮,selected CF7 表单

下面的代码将显示一个按钮 "Product Enquiry"。如果客户单击该按钮,它将消失,显示 selected 联系表 7:

// Frontend: Custom button, display the selected product enquiry form
add_action( 'woocommerce_after_add_to_cart_button', 'display_single_product_inquiry_form', 10 );
function display_single_product_inquiry_form(){
    global $product;

    // Get the selected form
    $enquiry_form = $product->get_meta('_enquiry_form');

    // Ouput button and hidden selected form hselected form
    if( ! empty($enquiry_form) ){
        echo '<div id="enquiry-form" style="padding-top:12px;">
        <p><a href="#" class="button">'.__("Product Enquiry", "woocommerce").'</a></p>
        <div style="display:none;">' . do_shortcode( "$enquiry_form" ) . '</div></div>';
    }

    // Jquery
    ?>
    <script type="text/javascript">
    jQuery(function($){
        var a = '#enquiry-form';

        // On click hide button and show form.
        $(a+' a').click(function(e){
            e.preventDefault();
            $(a+' > div').show();
            $(this).hide();
        });
    })
    </script>
    <?php
}

所有代码都在您的活动子主题(或活动主题)的 function.php 文件中。已测试并有效。