【问题标题】:Refresh WooCommerce Minicart Menu Item count and totals刷新 WooCommerce Minicart 菜单项计数和总数
【发布时间】:2021-01-20 05:53:57
【问题描述】:

在这个很棒的社区的帮助下,我能够创建一个 WC minicart 链接作为最后一个菜单项。

查看官方 WC 文档后,我意识到我需要添加购物车片段,启用购物车 Ajax 事件的“自动更新”。

预期输出:用户点击添加到购物车和菜单项更新。

这是完整的代码,它或多或少地破坏了整个事情。 我需要帮助结合这两个功能。

add_filter( 'wp_nav_menu_header-menu_items', 'minicart_link_with_product_count_subtotal_and_shipping', 10, 2 );
function minicart_link_with_product_count_subtotal_and_shipping( $items, $args ) {
    $order_total = WC()->cart->total;
    $order_subtotal = WC()->cart->get_subtotal();
    $shipping_total = wc_price($order_total - $order_subtotal);

    $link_url = wc_get_cart_url(); // cart url 

    // icon, product count, subtotal and shipping (based on if statement)
    if (WC()->cart->is_empty()){
        $link_text = sprintf( __( '<i class="shopping-cart"></i>', 'woocommerce' ));
    } else {
        $link_text = sprintf(__(
        '<i class="shopping-cart"></i> %d - %s (%s)', 'woocommerce'),
        WC()->cart->cart_contents_count,
        wc_price(WC()->cart->get_subtotal()),
        $shipping_total);
    }

    // link
    $minicart_link = '<a title="View my cart" class="wcminicart" href="' . $link_url . '">' . $link_text . '</a>';

    // return the link as last menu item
    return $items . $minicart_link;
}

add_filter( 'woocommerce_add_to_cart_fragments', 'atc_fragments' );
function atc_fragments( $fragments ) {
    ob_start();

    $count = WC()->cart->cart_contents_count;
        if ( $count > 0 ) { ?>
            <span class="wcminicart-count"><?php echo esc_html( $count ); ?></span>
        <?php
    }
    ?></a><?php
 
    $fragments['a.wcminicart'] = ob_get_clean();

    return $fragments;
}

【问题讨论】:

    标签: php wordpress woocommerce cart menu-items


    【解决方案1】:

    您的代码中有一些错误。请改用以下优化代码:

    // Custom function that display minicart link with count and totals
    function get_minicart_menu_item(){
        $html_output = '<i class="shopping-cart"></i>';
    
        // icon, product count, subtotal and shipping (based on if statement)
        if ( ! WC()->cart->is_empty() ){
            $cart_subtotal  = WC()->cart->get_subtotal();
            $shipping_total = WC()->cart->total - $cart_subtotal;
            $item_count     = WC()->cart->cart_contents_count;
    
            $html_output .= sprintf( ' <span class="count">%d</span> - %s',
                esc_html( $item_count ),
                strip_tags( wc_price( $cart_subtotal ) )
            );
    
            if ( $shipping_total > 0 ) {
                $html_output .= ' (' . strip_tags( wc_price( $shipping_total ) ) . ')';
            }
        }
    
        return '<a title="View my cart" class="wcminicart" href="' . wc_get_cart_url() . '">' . $html_output . '</a>';
    }
    
    // Add mincart item link as last menu item on navigation menu
    add_filter( 'wp_nav_menu_header-menu_items', 'minicart_link_with_product_count_subtotal_and_shipping', 10, 2 );
    function minicart_link_with_product_count_subtotal_and_shipping( $items, $args ) {
        return $items . get_minicart_menu_item(); // return the link as last menu item
    }
    
    // Refresh minicart menu item data on Ajax cart ajax events
    add_filter( 'woocommerce_add_to_cart_fragments', 'ajax_refreshed_minicart_menu_item_data' );
    function ajax_refreshed_minicart_menu_item_data( $fragments ) {
        $fragments['a.wcminicart'] = get_minicart_menu_item();
    
        return $fragments;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    注意:

    此外,我已将购物车商品计数的 CSS 类选择器重命名为 class="count" 而不是 wcminicart-count,因为您可以通过 CSS 规则的父类来定位它,例如:

    .wcminicart > .count { text-align:center; }
    

    【讨论】:

    • 谢谢。您是否有任何可以更改数量的迷你车代码/插件(免费/销售)?
    • @HaroldAldersen,不抱歉,我是开发人员,所以我使用手工代码。
    • 好的。制作一个需要多少钱?
    猜你喜欢
    • 2018-10-01
    • 2021-11-26
    • 2017-02-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多