dongzong1866 2019-06-25 15:49
浏览 31

需要Woocommerce复选框有条件地显示/隐藏

I'm attempting to create a conditional checkbox that shows/hides based on the shipping and billing state of the user during checkout. Currently, I can't find a way to conditionally show this information. All I need is for the checkbox to appear when the user is checking out from California. I also have a warning text box that I need to appear based on State and was able to make this work. For some reason, I can't seem to have the checkbox ONLY show up when California is selected as the state.

I've tried using the script that's allowing the text warning to show/hide, but it isn't working for the checkbox.

**/ add_action('woocommerce_after_checkout_form', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

echo '<div id="my-new-field"><h3>'.__(' ').'</h3>'; 

woocommerce_form_field( '', array(
    'type'          => 'checkbox',
    'class'         => array('input-checkbox'),
    'label'         => __('Yes, I Understand.'),
    'required'  => true,
    ), $checkout->get_value( '' ));

echo '</div>';

}

/** * Process the checkout **/ add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() { global $woocommerce;

// Check if set, if its not set add an error.
if (!$_POST['my_checkbox'])
     $woocommerce->add_error( __('Please agree to my checkbox.') );

}

// First Step // Create Prop 65 warning message and hook it into WooCommerce After Order Review // "display:none" we have hidden the warning message by default

add_action( 'woocommerce_after_checkout_form', 'show_prop_message' );

function show_prop_message() { echo 'placeholderforpicture" />WARNING SOME PRODUCTS SOLD ON THIS STORE WEBSITE CAN CONTAIN CHEMICALS KNOWN TO THE STATE OF CALIFORNIA TO CAUSE CANCER, BIRTH DEFECTS OR OTHER REPRODUCTIVE HARM. Details

// Second Step // Show or hide warning message based on billing and shipping state // First trigger is fired on billing state selection in case the "Ship to a different address" checkbox is unselected // Second trigger is fired if the "Ship to a different address" checkbox is ticked // Initally the "display:none" hides the warning message by default

add_action( 'woocommerce_before_checkout_form', 'show_warning_message' ); function show_warning_message(){
?>

<script>
    jQuery(document).ready(function($){

        // Set the shipping state 2 char code (This selection will fire the warning message display)
        // Fires secondary if shipping state is set to California
        var stateCode = 'CA';

        $('select#shipping_state').change(function(){

            selectedState = $('select#shipping_state').val();

            if( selectedState == stateCode ){
                $('.prop65-warning').show();
            }
            else {
                $('.prop65-warning').hide();
            }
        });

    });
    </script>
    <script>

    jQuery(document).ready(function($){ 

        // Set the billing state 2 char code (This selection will fire the warning message display)
        // Fires initially if billing state is California
        var stateCode = 'CA';

        $('select#billing_state').change(function(){

            selectedState = $('select#billing_state').val();

            if( selectedState == stateCode ){
                $('.prop65-warning').show();
            }
            else {
                $('.prop65-warning').hide();
            }
        });

    });     
</script>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了