有条件的Woocommerce结帐字段逻辑

时间:2018-07-18 19:52:11

标签: php wordpress woocommerce field checkout

我想创建一个有关字段的自定义逻辑结帐页面。 如果名称为“ step”且值为“ 1”或“ 2”,我想隐藏字段。

那是我的代码。

function wpb_custom_billing_fields( $fields = array()) {
    if($_POST['post_data']){
        parse_str( $_POST['post_data'], $post_data );
        $_SESSION['post_data'] = $post_data;
    };
    if( $_SESSION['post_data']["step"] == '1' ) {
        unset( $fields['billing']['billing_company'] );
        unset( $fields['billing']['billing_address_1'] );
        unset( $fields['billing']['billing_address_2'] );
        unset( $fields['billing']['billing_city'] );
        unset( $fields['billing']['billing_postcode'] );
        unset( $fields["billing"]["billing_country"] );
        unset( $fields['billing']['billing_state'] );
        unset( $fields['billing']['billing_phone'] );
        unset( $fields['billing']['billing_email'] );
        unset( $fields["billing"]["billing_country"] );
        unset($fields['shipping_address_1']);
        unset($fields['shipping_address_2']);
        unset($fields['shipping_city']);
        unset($fields['shipping_state']);
        unset($fields['shipping_postcode']);

    };
    if( $_SESSION['post_data']["step"] == '2' ) {

        unset( $fields['billing']['billing_company'] );
        unset( $fields['billing']['billing_address_1'] );
        unset( $fields['billing']['billing_address_2'] );
        unset( $fields['billing']['billing_city'] );
        unset( $fields['billing']['billing_postcode'] );
        unset( $fields['billing']['billing_country'] );
        unset( $fields['billing']['billing_state'] );
        unset( $fields['billing']['billing_phone'] );
        unset( $fields['billing']['billing_email'] );
        unset( $fields['last_name'] );
    }
    return $fields;  
}
add_filter('woocommerce_checkout_fields','wpb_custom_billing_fields');

function sv_unrequire_wc_phone_field( $fields ) {
    if( $_SESSION['post_data']["step"] == '1' ) {
        $fields['billing_phone']['required'] = false;
    }
    if( $_SESSION['post_data']["step"] == '2' ) {
        $fields['billing_phone']['required'] = false;
    }
    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'sv_unrequire_wc_phone_field' );

第一个功能有效,但是第二个功能无效。

我们非常感谢您的帮助。

步骤-1

Step - 1

步骤-2

Step - 2

1 个答案:

答案 0 :(得分:0)

        function wpb_custom_billing_fields( $fields = array()) {
        if($_POST['post_data']){
    parse_str( $_POST['post_data'], $post_data );
    // $_SESSION['post_data'] = $post_data;
    // Need create session woocommerce:) usual $_SESSION don't worked:)
    WC()->session->set( 'post_data' , $post_data );
        };
    if( $post_data["step"] == '1' ) {
          unset( $fields['billing']['billing_company'] );
          unset( $fields['billing']['billing_address_1'] );
    // and more....
    };
    if( $post_data["step"] == '2' ) {

              unset( $fields['billing']['billing_company'] );
          unset( $fields['billing']['billing_address_1'] );
          unset( $fields['billing']['billing_address_2'] );
          // and more....
    };


        return $fields;

    }
    add_filter('woocommerce_checkout_fields','wpb_custom_billing_fields');

    function sv_unrequire_wc_phone_field( $fields ) {
 // Get step session
        $step = WC()->session->get( 'post_data' );
            if( $step["step"] == '1' ) {
        $fields['billing_email']['required'] = false;
        return $fields;
    };
        if( $step["step"] == '2' ) {

        return $fields;
    };

    }
    add_filter( 'woocommerce_billing_fields', 'sv_unrequire_wc_phone_field' );

谢谢:)

更新:它如何工作

要使此代码正常工作,您需要调用更新事件。

    add_action( 'wp_footer', 'artabr_add_script_update_shipping_method' );
function artabr_add_script_update_shipping_method() {
   if (is_checkout()) {
      ?>
      <script>
        (function( $ ) {
           $(document.body).on('click', '.step ul.reception-tabs li a',  function () {
 $('body').trigger('update_checkout');
});
           })( jQuery );
      </script>
      <?php
   }
}

并以隐藏的形式创建输入。

	$(".step .reception-tabs li:first").click(function(){
		$('.hidden-step').val(1);
		$('#ship-to-different-address-checkbox').val(0);
		$('.shipping_address').hide();

	});
  	$(".step .reception-tabs li:last").click(function(){
		$('.hidden-step').val(2);
		$('#ship-to-different-address-checkbox').val(1);
		$('.shipping_address').show();
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="step">
				<p class="title">Step 1: Select delivery</p>
				<ul class="reception-tabs">
					<li><a class="active" href="#">Local pickup</a></li>
					<li><a href="#">Delivery</a></li>
				</ul>
			</div>
<input class="hidden-step" type="hidden" name="step" value="1" />