如果不重新加载Woocommerce,自定义购物车数量不会更新 [英] Custom cart count is not updating without reloading in Woocommerce

查看:107
本文介绍了如果不重新加载Woocommerce,自定义购物车数量不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经排队了ajax脚本,但是如果不刷新页面,我似乎无法更新购物车计数。

I have the ajax script enqueued, but I can't seem to get the cart items count updated without refreshing the page.

功能:

// Add scripts and stylesheets
function startwordpress_scripts() {
    wp_enqueue_style( 'reset', get_template_directory_uri() . '/reset.css' );
    wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'style', get_template_directory_uri() . '/veggiee.css');
    wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', array(), null, true);
}

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;

    ob_start();

    $fragments['a.cart-customlocation'] = ob_get_clean();
    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat', 10, 3 );

HTML:

<ul>
<li>
<a href="/cart" id="cart_icon"></a></li><li><span class="counter"> 
<?php echo sprintf ( _n( '%d', '%d', WC()->cart>get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span></li>
<li id="access"><?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?></li>
</ul>

我已经研究了这个问题,据我所知,篮子总额应该立即更新而不会刷新。

I have researched the problem and as far as I can see the basket total should update straight away without a refresh.

有人知道我在这里缺少什么吗?

Does anyone have any idea what I'm missing here?

推荐答案

您的代码中存在一些错误和遗漏的东西。对于标题中的购物车数量,以下将解决此问题。

There is some mistakes and missing things in your code. For the cart item count in your header the following will solve the problem.

1) header.php 文件:

<ul>
    <li>
        <a href="/cart" id="cart_icon"></a>
    </li>
    <li>
        <span class="counter" id="cart-count"><?php
        $cart_count = WC()->cart->get_cart_contents_count();
        echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
        ?></span>
    </li>
    <li id="access"><?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?></li>
</ul>

2)您相关的挂钩函数代码,用于启用购物车商品计数刷新了Ajax

2) Your related hooked function code to enable cart item count Ajax refreshed:

add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
    ob_start();
    ?>
    <span class="counter" id="cart-count"><?php
    $cart_count = WC()->cart->get_cart_contents_count();
    echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
    ?></span>
    <?php
     $fragments['#cart-count'] = ob_get_clean();

    return $fragments;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试和工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

相关:对Woocommerce中的购物车计数进行Ajaxize

这篇关于如果不重新加载Woocommerce,自定义购物车数量不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆