我使用下面的代码来显示一些基本的通知,但是在购物车页面上我使用了 AJAX 自动更新购物车插件,当产品数量改变/删除时,它会动态更新购物车。
如何使通知代码在购物车动态更新时也能正常工作?就像当达到动态金额时显示成功消息一样。现在,当金额通过时,我得到一个负数,而不是看到成功消息。成功消息仅在页面刷新后显示,负数也仅在页面刷新后消失。
//Free shipping reminder
add_action( 'woocommerce_before_cart', 'show_left_amount_for_free_shipping' );
function show_left_amount_for_free_shipping() {
$total_cart_amount = WC()->cart->cart_contents_total;
$free_shipping_amount = 50;
$left_amount = $free_shipping_amount - $total_cart_amount;
if ( $left_amount > 0 ) {
echo '<p class="free-shipping-left-amount">Добавете допълнително продукти на стойност <strong>' . wc_price( $left_amount ) . '</strong>, за да получите безплатна доставка.</p>';
// Add fragments
add_filter('woocommerce_add_to_cart_fragments', 'free_shipping_left_amount_fragment');
function free_shipping_left_amount_fragment( $fragments ) {
ob_start();
?>
<p class="free-shipping-left-amount">Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите безплатна доставка.</p>
<?php
$fragments['p.free-shipping-left-amount'] = ob_get_clean();
return $fragments;
}
}
}
//Refresh the left amount for free shipping when the cart is updated
add_filter('woocommerce_add_to_cart_fragments', 'refresh_left_amount_for_free_shipping');
function refresh_left_amount_for_free_shipping($fragments) {
$total_cart_amount = WC()->cart->cart_contents_total;
$free_shipping_amount = 50;
$left_amount = $free_shipping_amount - $total_cart_amount;
ob_start();
?>
<p class="free-shipping-left-amount">Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите безплатна доставка.</p>
<?php
$fragments['p.free-shipping-left-amount'] = ob_get_clean();
return $fragments;
}
// Show success message when free shipping amount is reached
add_filter('woocommerce_add_to_cart_fragments', 'show_success_message_when_free_shipping_amount_is_reached');
function show_success_message_when_free_shipping_amount_is_reached($fragments) {
$total_cart_amount = WC()->cart->cart_contents_total;
$free_shipping_amount = 50;
$left_amount = $free_shipping_amount - $total_cart_amount;
ob_start();
if($left_amount <= 0){
?>
<p class="free-shipping-success-message">Благодарим Ви! Вие достигнахте сумата за безплатна доставка.</p>
<?php
}
$fragments['p.free-shipping-success-message'] = ob_get_clean();
return $fragments;
}
//Remaining amount for a discount
add_action( 'woocommerce_before_cart', 'show_left_amount_for_discount' );
function show_left_amount_for_discount() {
$total_cart_amount = WC()->cart->cart_contents_total;
$discount_amount = 100;
$left_amount = $discount_amount - $total_cart_amount;
if ( $total_cart_amount > 50 && $total_cart_amount < 100 ) {
echo '<p class="discount-left-amount">Добавете допълнително продукти на стойност <strong>' . wc_price( $left_amount ) . '</strong>, за да получите 10% отсъпка.</p>';
// Add fragments
add_filter('woocommerce_add_to_cart_fragments', 'discount_left_amount_fragment');
function discount_left_amount_fragment( $fragments ) {
ob_start();
?>
<p class="discount-left-amount">Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите 10% отсъпка.</p>
<?php
$fragments['p.discount-left-amount'] = ob_get_clean();
return $fragments;
}
}
}
//Refresh the left amount for discount when the cart is updated
add_filter('woocommerce_add_to_cart_fragments', 'refresh_left_amount_for_discount');
function refresh_left_amount_for_discount($fragments) {
$total_cart_amount = WC()->cart->cart_contents_total;
$discount_amount = 100;
$left_amount = $discount_amount - $total_cart_amount;
ob_start();
?>
<p class="discount-left-amount">Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите 10% отсъпка.</p>
<?php
$fragments['p.discount-left-amount'] = ob_get_clean();
return $fragments;
}
1条答案
按热度按时间zwghvu4y1#
你的计算和其他东西在你的代码中是可以的,但是你的片段逻辑是错误的。我已经最小化了你的代码并纠正了逻辑问题。
我已经创建了一个函数
vh_wc_get_custom_cart_messages()
,它将验证和创建我用div.custom-wc-cart-messsages
Package 的所有消息,这个类也用于片段。在您的旧代码中,您将添加片段,但问题是您的消息HTML类不是常量类。
vh_wc_get_custom_cart_messages()
函数将被调用以在页面加载时在购物车页面上显示消息,并且还将与片段一起使用,以便您不必再次写入相同的消息和逻辑。请检查下面的代码,让我知道它是如何为您工作的。我已经测试了开发网站上的代码,它为我工作的罚款。