wordpress 如何检查Woocommerce购物车页面是否动态更新并显示/更新通知

puruo6ea  于 2022-12-22  发布在  WordPress
关注(0)|答案(1)|浏览(172)

我使用下面的代码来显示一些基本的通知,但是在购物车页面上我使用了 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;
}
zwghvu4y

zwghvu4y1#

你的计算和其他东西在你的代码中是可以的,但是你的片段逻辑是错误的。我已经最小化了你的代码并纠正了逻辑问题。
我已经创建了一个函数vh_wc_get_custom_cart_messages(),它将验证和创建我用div.custom-wc-cart-messsages Package 的所有消息,这个类也用于片段。
在您的旧代码中,您将添加片段,但问题是您的消息HTML类不是常量类。
vh_wc_get_custom_cart_messages()函数将被调用以在页面加载时在购物车页面上显示消息,并且还将与片段一起使用,以便您不必再次写入相同的消息和逻辑。
请检查下面的代码,让我知道它是如何为您工作的。我已经测试了开发网站上的代码,它为我工作的罚款。

/**
 * This function will validate cart amount conditions
 * and will return text with HTML which we'll use with
 * 'woocommerce_before_cart' and 'woocommerce_add_to_cart_fragments'
 * to avoid writing code twice.
 */
function vh_wc_get_custom_cart_messages() {
    // Get cart contents total.
    $total_cart_amount = WC()->cart->get_cart_contents_total();

    $free_shipping_amount           = 50;
    $remianing_free_shipping_amount = $free_shipping_amount - $total_cart_amount;

    $discount_amount           = 100;
    $remaining_discount_amount = $discount_amount - $total_cart_amount;

    ob_start();

    // this next div class is what we'll use in fragment.
    echo '<div class="custom-wc-cart-messsages">';

    // Do validation and write messages.
    if ( $remianing_free_shipping_amount > 0 ) {
        echo '<p class="free-shipping-left-amount">Добавете допълнително продукти на стойност <strong>' . wc_price( $remianing_free_shipping_amount ) . '</strong>, за да получите безплатна доставка.</p>';
    } else {
        echo '<p class="free-shipping-success-message">Благодарим Ви! Вие достигнахте сумата за безплатна доставка.</p>';
    }

    // Do validation and write messages.
    if ( $total_cart_amount > 50 && $total_cart_amount < 100 ) {
        echo '<p class="discount-left-amount">Добавете допълнително продукти на стойност <strong>' . wc_price( $remaining_discount_amount ) . '</strong>, за да получите 10% отсъпка.</p>';
    }

    echo '</div>';

    $content = ob_get_clean();

    return $content;
}

/**
 * Display custom cart messages before the cart table.
 */
function vc_wc_custom_display_custom_cart_messages() {
    // wp_kses_post function allowed only valid HTML tags. We using it for XSS protection.
    echo wp_kses_post( vh_wc_get_custom_cart_messages() );
}
add_action( 'woocommerce_before_cart', 'vc_wc_custom_display_custom_cart_messages' );

/**
 * Add custom cart fragments
 *
 * @param array $fragments Array of fragments.
 * @return array
 */
function vc_wc_custom_cart_fragments( $fragments ) {
    $fragments['div.custom-wc-cart-messsages'] = wp_kses_post( vh_wc_get_custom_cart_messages() );

    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'vc_wc_custom_cart_fragments' );

相关问题