我的 Wordpress 网站上有一个横幅,我想在 Ajax 不为购物车为空时更新该横幅。
这是非 Ajaxified 版本:
function dynamic_banner() {
$atts = '<div class="woofc-shipping">' . alg_wc_get_left_to_free_shipping('<span style="color:#F79917"><i class="fas fa-shipping-fast" style="margin-left: 5px;"></i>Add %amount_left_for_free_shipping% more for Free shipping</span> <div class="woofc-total-right"></div>') . '</div>';
if ( ! WC()->cart->is_empty() ) {
?>
<script>
jQuery(function($){
$(".ban-text").replaceWith('<?php echo $atts; ?>')});
</script>
<?php
}
}
add_action('wp_head', 'dynamic_banner');
这是 HTML
<div class="head_strip over--hide" style="display: block;">
<p><strong>Free fast delivery*</strong> in purchases over <span class="special-price">295 NIS</span> even during the closure period!</span></p>
</div>
答案 0 :(得分:1)
感谢 those threads 的 LoicTheAztec 找到了解决方案
Functions.php 代码:
// Shortcode for the banner
function display_free_shipping_progress() {
return get_free_shipping_progress();
}
add_shortcode('fs_progress', 'display_free_shipping_progress');
// Function that makes calculations and display
function get_free_shipping_progress() {
$atts = '<div class="woofc-shipping">' . alg_wc_get_left_to_free_shipping('<span style="color:#F79917"><i class="fas fa-shipping-fast" style="margin-left: 5px;"></i>הוסיפי עוד %amount_left_for_free_shipping% למשלוח חינם</span> <div class="woofc-total-right"></div>') . '</div>';
$prodgress = WC()->cart->total;
//Check if cart has something else show default
if( $prodgress > 0 ) {
return '<div class="head_strip over--hide" style="display: block;">' . $atts . '</div>';
} else {
return '<div class="head_strip over--hide" style="display: block;"><span class="ban-text"><strong>*משלוח מהיר חינם</strong > בקניה מעל<span class="special-price">295₪</span> גם בתקופת הסגר!</span></div>';
}
}
// Refreshing "free shipping" progress on cart ajax events
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_free_shipping_progress' );
function refresh_free_shipping_progress( $fragments ){
$fragments['.head_strip'] = get_free_shipping_progress();
return $fragments;
}
为了简单起见,已将所有需要的 HTML 传递给简码,就像推荐的那样。