【问题标题】:How can I run a jQuery function after an ajax event of Woocommerce如何在 Woocommerce 的 ajax 事件后运行 jQuery 函数
【发布时间】:2020-05-08 08:29:32
【问题描述】:

我在 functions.php 中使用此过滤器来更新 Woocommerce 迷你购物车中可用商品的数量:

// Woocommerce Ajax Count

add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_count');
function wc_refresh_mini_cart_count($fragments){
    ob_start();
    ?>
    <div class="e434-15 x-bar-content-area sticky-count">
        <?php echo WC()->cart->get_cart_contents_count(); ?>
    </div>
    <?php
        $fragments['.sticky-count'] = ob_get_clean();
    return $fragments;
}

并在 header 中使用此 jQuery 将计数器数字转换为其他格式:

// Convert Numbers to Persian Format

    jQuery(document).ready( function(){
          var els = document.querySelectorAll('.woocommerce-Price-amount, .mini_cart_item span.quantity, .sticky-count');
          els.forEach(function(item) {
          item.textContent = persianJs(item.textContent).englishNumber().toString();
        });
    });

问题是,在将 ajax 添加到购物车或从购物车中删除 ajax 后,上面的查询没有运行,所以我尝试了这个脚本:

// Convert Numbers to Persian Format

jQuery(document).ready( function(){
      var els = document.querySelectorAll('.woocommerce-Price-amount, .mini_cart_item span.quantity, .sticky-count');
      els.forEach(function(item) {
      item.textContent = persianJs(item.textContent).englishNumber().toString();
    });
});

// Convert Numbers After Ajax Add or Remove Products

$( document.body ).on( 'added_to_cart removed_from_cart', function(){
      var els = document.querySelectorAll('.woocommerce-Price-amount, .mini_cart_item span.quantity, .sticky-count');
      els.forEach(function(item) {
      item.textContent = persianJs(item.textContent).englishNumber().toString();
    });
});

但它也不起作用,似乎我以错误的方式或在错误的位置调用 jQuery,请您指导我解决这个问题吗?

更新: 我也在functions.php中尝试过这段代码,但我也不起作用:

add_action('wp_header','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
    if ( is_shop() || is_product_category() || is_product_tag() ): // Only for archives pages
        ?>
            <script type="text/javascript">
            $( document.body ).on( 'added_to_cart removed_from_cart', function(){
            var els = document.querySelectorAll('.woocommerce-Price-amount, .mini_cart_item span.quantity, .sticky-count');
            els.forEach(function(item) {
            item.textContent = persianJs(item.textContent).englishNumber().toString();
            });
        })(jQuery);
            </script>
        <?php
    endif;
}

【问题讨论】:

    标签: javascript php jquery woocommerce


    【解决方案1】:

    这个脚本对我有用:

    // Convert Numbers After Ajax Add or Remove Products
    
    jQuery(document).ready(function($){
        $('body').on( 'added_to_cart removed_from_cart', function(){
          var els = document.querySelectorAll('.woocommerce-Price-amount, .mini_cart_item span.quantity, .sticky-count');
          els.forEach(function(item) {
          item.textContent = persianJs(item.textContent).englishNumber().toString();
           });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-02-05
      • 2017-01-11
      • 2010-10-15
      • 1970-01-01
      • 2012-10-19
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多