【发布时间】:2021-05-17 01:20:46
【问题描述】:
根据Add a body class for specific selected shipping option in Woocommerce checkout的回答码,我使用下面的代码sn-p已经有一段时间了:
add_filter( 'wp_footer','weekend_selected' );
function weekend_selected(){
if( ! is_page( 8 ) ) return; // only on checkout
$method_id = "''";
// Find the Shipping Method ID for the Shipping Method label name 'Delivery price on request'
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_key => $rate ){
if( 'Saturdays DPD' == $rate->label ){
$method_id = $rate_key;
break;
}
}
?>
<script type="text/javascript">
(function($){
// variables initialization
var a = 'input[name^="shipping_method[0]"]',
b = a+':checked',
c = 'weekend-selected',
d = '<?php echo $method_id; ?>';
if( $(b).val() == d )
$('body').addClass(c);
else
$('body').removeClass(c);
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() == d )
$('body').addClass(c);
else
$('body').removeClass(c);
});
})(jQuery);
</script>
<?php
}
但是我注意到它在页面加载时不再触发,我必须刷新页面才能触发它,这使它无用。
用例基本上是加载一个body标签(周末选择),然后隐藏(使用CSS)日历上的某些日子。
首先我认为这是因为在添加地址之前没有加载运输选项所以我预先填写了必填的地址字段以查看这是否是问题但同样的事情发生了,我必须刷新页面才能触发代码.
【问题讨论】:
标签: php jquery wordpress woocommerce shipping-method