You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress WooCommerce购物车函数本地正常AWS生产环境失效

WooCommerce购物车数据在AWS生产环境显示异常(本地正常)

我在购物车下拉菜单中展示商品总数量与总金额,相关功能在本地环境运行正常,但部署到AWS生产实例后全部失效,购物车数量和总价均显示为0。

测试过以下WooCommerce购物车相关方法,均在本地正常但生产环境无效:

WC()->cart->cart_contents_count
WC()->cart->get_cart_contents_count()
WC()->cart->get_total()
WC()->cart->get_cart_total()
WC()->cart->cart_contents_total
WC()->cart->total 

同时尝试了两段基于woocommerce_add_to_cart_fragments钩子的代码,同样本地正常、生产环境无效:

第一段代码

add_filter( 'woocommerce_add_to_cart_fragments', 'header_add_to_cart_fragment' );
function header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <a class="cart-customlocation" id="from-function" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>">
        <?php echo sprintf(_n('%d item', '%d items', WC()->cart->cart_contents_count), WC()->cart->cart_contents_count);?> - 
        <?php echo WC()->cart->get_total(); ?>
    </a>
    <?php
    $fragments['a.cart-customlocation'] = ob_get_clean();
    return $fragments;
}

第二段代码

add_filter( 'woocommerce_add_to_cart_fragments', 'header_add_to_cart_fragment' );
function header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <a class="cart-customlocation" id="from-function" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>">
        <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count), $woocommerce->cart->cart_contents_count);?> - 
        <?php echo $woocommerce->cart->get_total(); ?>
    </a>
    <?php
    $fragments['a.cart-customlocation'] = ob_get_clean();
    return $fragments;
}

内容的提问来源于stack exchange,提问作者MD TAREK HOSSEN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:20:37