Jquery 更新的\u购物车\u总计触发器在购物车清空后不工作

Jquery 更新的\u购物车\u总计触发器在购物车清空后不工作,jquery,wordpress,woocommerce,Jquery,Wordpress,Woocommerce,当前,我想在单击购物车页面上的更新购物车按钮后更改div中的文本 现在,我使用jQuery触发一个操作,使其正常工作 jQuery( document.body ).on( 'updated_cart_totals', function(){ var sum = 0; jQuery(".qty").each(function(){ sum += +jQuery(this).val(); }); if(sum &

当前,我想在单击购物车页面上的更新购物车按钮后更改div中的文本

现在,我使用jQuery触发一个操作,使其正常工作

jQuery( document.body ).on( 'updated_cart_totals', function(){
    var sum = 0;
    jQuery(".qty").each(function(){
        sum += +jQuery(this).val();
    });
        
    if(sum > 0 || sum != ''){   
        jQuery('#text_block-14-124, #text_block-179-47').text(sum);
    }else{
        jQuery('#text_block-14-124, #text_block-179-47').text('0'); // does not run
    }
});
它的工作原理类似于charms,但是当我将数量更改为0并单击update_cart按钮并将我的cart设置为空时,我的脚本不仅会在有产品的情况下运行。单击“更新购物车”按钮,然后单击“我的购物车清空”,我可以运行脚本来更改文本


我不使用购物车碎片使其工作,完全是手动编码。

更新的购物车总计
仅在
时触发。woocommerce购物车表单
在您更新数量0时发现,所以在这种情况下,您需要使用
wc购物车清空
触发器

function add_custom_js(){
    ?>
    <script type="text/javascript">
        (function($){
            jQuery( document.body ).on( 'updated_cart_totals', function(){
                var sum = 0;
                jQuery(".qty").each(function(){
                    sum += +jQuery(this).val();
                });
                if(sum > 0 || sum != ''){   
                    jQuery('#text_block-14-124, #text_block-179-47').text(sum);
                }else{
                    jQuery('#text_block-14-124, #text_block-179-47').text('0'); // does not run
                }
            });

            jQuery( document.body ).on( 'wc_cart_emptied', function(){
                var sum = 0;
                jQuery(".qty").each(function(){
                    sum += +jQuery(this).val();
                });
                if(sum > 0 || sum != ''){   
                    jQuery('#text_block-14-124, #text_block-179-47').text(sum);
                }else{
                    jQuery('#text_block-14-124, #text_block-179-47').text('0'); // does not run
                }
            });
        })(jQuery);
    </script>
    <?php
}

add_action( 'wp_footer', 'add_custom_js', 10, 1 );
/**
 * Update the .woocommerce div with a string of HTML.
 *
 * @param {String} html_str The HTML string with which to replace the div.
 * @param {bool} preserve_notices Should notices be kept? False by default.
 */
var update_wc_div = function( html_str, preserve_notices ) {
    var $html       = $.parseHTML( html_str );
    var $new_form   = $( '.woocommerce-cart-form', $html );
    var $new_totals = $( '.cart_totals', $html );
    var $notices    = $( '.woocommerce-error, .woocommerce-message, .woocommerce-info', $html );

    // No form, cannot do this.
    if ( $( '.woocommerce-cart-form' ).length === 0 ) {
        window.location.reload();
        return;
    }

    // Remove errors
    if ( ! preserve_notices ) {
        $( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
    }

    if ( $new_form.length === 0 ) {
        // If the checkout is also displayed on this page, trigger reload instead.
        if ( $( '.woocommerce-checkout' ).length ) {
            window.location.reload();
            return;
        }

        // No items to display now! Replace all cart content.
        var $cart_html = $( '.cart-empty', $html ).closest( '.woocommerce' );
        $( '.woocommerce-cart-form__contents' ).closest( '.woocommerce' ).replaceWith( $cart_html );

        // Display errors
        if ( $notices.length > 0 ) {
            show_notice( $notices );
        }

        // Notify plugins that the cart was emptied.
        $( document.body ).trigger( 'wc_cart_emptied' );
    } else {
        // If the checkout is also displayed on this page, trigger update event.
        if ( $( '.woocommerce-checkout' ).length ) {
            $( document.body ).trigger( 'update_checkout' );
        }

        $( '.woocommerce-cart-form' ).replaceWith( $new_form );
        $( '.woocommerce-cart-form' ).find( ':input[name="update_cart"]' ).prop( 'disabled', true ).attr( 'aria-disabled', true );

        if ( $notices.length > 0 ) {
            show_notice( $notices );
        }

        update_cart_totals_div( $new_totals );
    }

    $( document.body ).trigger( 'updated_wc_div' );
};

/**
 * Update the .cart_totals div with a string of html.
 *
 * @param {String} html_str The HTML string with which to replace the div.
 */
var update_cart_totals_div = function( html_str ) {
    $( '.cart_totals' ).replaceWith( html_str );
    $( document.body ).trigger( 'updated_cart_totals' );
};