woocommerce - 购物车计数未在移动设备 woocommerce 中更新
问题描述
我正在使用此代码更新 woocommerce 中的购物车计数以更新 woocommerce 购物车。它在较大的设备中正常工作。但在移动设备中不工作。刷新后它工作。我在下面附上代码。
在functions.php中这是代码
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 $cart_count;
?></span>
<?php
$fragments['#cart-count'] = ob_get_clean();
return $fragments;
}
在大型设备和移动设备的 header.php 购物车部分
<li>
<a href="<?php echo get_template_directory_uri(); ?>/cart"
><i class="uil uil-shopping-cart-alt"></i> Cart</a
>
<span class="counter" id="cart-count-mobile"><?php
$cart_count = WC()->cart->get_cart_contents_count();
echo $cart_count;
?></span>
</li>
这是在大型设备和移动设备中对购物车重复 2 次的相同代码。
解决方案
在functions.php 中使用类名而不是id.it 会起作用。
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 $cart_count;
?></span>
<?php
$fragments['span.counter'] = ob_get_clean();
return $fragments;
}
推荐阅读
- python - .numpy() 函数是什么意思以及为什么这里有错误
- python - 我如何声明一个变量并在 python 的 2 个函数中访问它
- orientdb - 将元素添加到 OrientDB 上的嵌入集
- .net - .NET WebBrowser 控件不接收 OAuth2 重定向 URL
- oracle-apex - APEX IG 未正确渲染网格
- here-api - 如何在 HERE api 的大规模矩阵中获取令牌请求的参数?
- c++ - 存储指向固定大小向量内的元素的指针
- python - 如何计算 pandas DataFrame 相关函数中使用的非 NA/null 值的数量?
- ruby-on-rails - Yard doc 和常量、实例和类方法的动态分配
- python - 比较 pandas 数据帧,从其他数据帧捕获数据