php - 条件 Woocommerce 结帐字段逻辑
问题描述
我想创建一个关于字段的自定义逻辑结帐页面。如果名称为“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步
第2步
解决方案
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" />
推荐阅读
- javascript - 为什么当我打印数组的元素时,它以 undefined 开头,然后是我插入的数字
- c# - Google Play 控制台“文件格式无效”无法上传我的 apk
- r - R:查找满足条件的行,从下一行求和并删除它
- java - 将自动计数超出范围月份到下个月
- html - 在 Django 中获取 Post 值
- javascript - 使用 jest 测试节点公用文件夹中的 javascript
- ansible - 使用 with_items 和 vmware_guest for vSphere 访问 Ansible 清单变量
- json - 如何将json文本写入excel,但仍然显示json格式?
- python - 如何通过 conda 在 databricks 中安装库(cfgrib)?
- swift - 强大而廉价的数组算法