我正在尝试根据所选的送货方式显示/隐藏结帐页面中的某些元素。我试图显示/隐藏的页面元素来自另一个插件,因此我尝试更改它们的显示属性。我看过很多类似的话题:
Show or hide checkout fields based on shipping method in Woocommerce 3
但它是针对结帐字段的,我不确定如何对页面元素执行此操作。
然后根据this answer thread到目前为止,这是我的代码:
add_action( 'wp_footer', 'conditionally_hidding_order_delivery_date' );
function conditionally_hidding_order_delivery_date(){
// Only on checkout page
if( ! is_checkout() ) return;
// HERE your shipping methods rate ID "Home delivery"
$home_delivery = 'distance_rate_shipping';
?>
<script>
jQuery(function($){
// Choosen shipping method selectors slug
var shipMethod = 'input[name^="shipping_method"]',
shipMethodChecked = shipMethod+':checked';
// Function that shows or hide imput select fields
function showHide( actionToDo='show', selector='' ){
if( actionToDo == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen shipping method is "Home delivery"
if( $(shipMethodChecked).val() != '<?php echo $home_delivery; ?>' ) {
showHide('hide','#e_deliverydate_field' );
showHide('hide','#time_slot_field' );
}
// Live event (When shipping method is changed)
$( 'form.checkout' ).on( 'change', shipMethod, function() {
if( $(shipMethodChecked).val() == '<?php echo $home_delivery; ?>' ) {
showHide('show','#e_deliverydate_field' );
showHide('show','#time_slot_field' );
}
else {
showHide('hide','#e_deliverydate_field' );
showHide('hide','#time_slot_field' );
}
});
});
</script>
<?php
}
但它并没有完全起作用(初始化函数不起作用。)
非常感谢任何帮助。
附加编辑:
<p class="form-row form-row-wide validate-required" id="e_deliverydate_field" data-priority="" style="display: block;"><label for="e_deliverydate" class="">Date<abbr class="required" title="required">*</abbr></label><span class="woocommerce-input-wrapper"><input class="input-text hasDatepicker" name="e_deliverydate" id="e_deliverydate" placeholder="Choose a Date" value="" style="cursor:text !important;" type="text"></span></p>
我的目标是将 p 元素的显示属性从 block 更改为无,反之亦然。
最佳答案
所需的代码比我在其他答案中使用的代码简单得多,但您需要确保目标选择的运输方式是'distance_rate_shipping'
因为我无法测试它。
For initialization problem, see the solution exposed at the end of my answer.
简化的所需代码:
// Embedded jQuery script
add_action( 'wp_footer', 'checkout_delivery_date_script' );
function checkout_delivery_date_script() {
// Only checkout page
if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
?>
<script type="text/javascript">
jQuery( function($){
var a = 'input[name^="shipping_method"]', b = a+':checked',
c = 'distance_rate_shipping',
d = '#e_deliverydate_field,#time_slot_field';
// Utility function that show or hide the delivery date
function showHideDeliveryDate(){
if( $(b).val() == c )
$(d).show();
else
$(d).hide('fast');
console.log('Chosen shipping method: '+$(b).val()); // <== Just for testing (to be removed)
}
// 1. On start
showHideDeliveryDate();
// 2. On live event "change" of chosen shipping method
$('form.checkout').on('change', a, function(){
showHideDeliveryDate();
});
});
</script>
<?php
}
代码进入您活跃的子主题(或主题)的 function.php 文件。它应该有效。
初始化问题:
肯定是因为你正在使用的生成交货日期输出的插件在初始化后延迟
The solution can be to add some delay on initialization execution.
所以应该尝试替换这个:
// 1. On start
showHideDeliveryDate();
通过我的代码中的以下内容(将执行延迟调整为高于或低于 500
):
// 1. On start
setTimeout(function(){
showHideDeliveryDate();
}, 500);
关于php - 在 Woocommerce 中选择的运输方式更改时显示或隐藏 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51739847/