更新自定义标题菜单购物车图标上的Woocommerce购物车项目计数

时间:2019-03-11 06:16:16

标签: php wordpress woocommerce count cart

我已经在标题菜单中添加了woocommerce购物车图标,但是我希望它在添加项目时(通过ajax)得到更新。

这是我的代码:

function am_append_cart_icon( $items, $args){ 
    $cart_item_count = WC()->cart->get_cart_contents_count();
    $cart_count_span = ''; 

    if ( $cart_item_count ){ 
        $cart_count_span = ''.$cart_item_count.''; 
    }

    $cart_link = ''.$cart_count_span.''; // Add the cart link to the end of the menu. 
    $items = $items . $cart_link;

    return $items;      
} 
add_filter( 'wp_nav_menu_main-menu_items', 'am_append_cart_icon', 10, 2 );

0 个答案:

没有答案