【问题标题】:Display the chosen selected CF7 from admin on Woocommerce single products在 Woocommerce 单品上显示从管理员选择的 CF7
【发布时间】:2018-09-04 07:13:08
【问题描述】:

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

  1. 后端

    • 用户可以选择产品是否可以有产品查询表
    • 用户可以从下拉列表中的单个产品中选择 CF7 表单。
  2. 前端

    • 单个产品页面显示标准产品信息,但如果选中“产品查询表”,则单个产品页面显示一个按钮,显示“产品查询”
    • 单击该按钮时,会显示一个包含产品属性(尺寸、颜色等)的 CF7 表单

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

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

感谢所有帮助。

【问题讨论】:

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


    【解决方案1】:

    下面的代码将显示在:

    1) 后端“高级选项卡”下的单个产品设置选择字段与您的不同 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)前端下面的单品页面添加到购物车按钮,选择CF7表单

    下面的代码将显示一个“产品查询”按钮。如果客户单击该按钮,它将消失,并显示选定的联系表 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 文件中。经过测试并且有效。

    【讨论】:

    • 哇!惊人的!这正是我要找的!
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多