显示购物车菜单项中的购物车信息

时间:2017-08-11 作者:Louis

我在函数中使用此代码。php的主题,以便在购物车菜单项旁边添加购物车信息。

但是,此代码在<li> 我想把这些信息<li>.

您能帮我更改此代码以便在<li>?

谢谢

/**
 * This function adds the WooCommerce or Easy Digital Downloads cart icons/items to the top_nav menu area as the last item.
 */
add_filter( \'wp_nav_menu_items\', \'my_wp_nav_menu_items\', 10, 2 );
function my_wp_nav_menu_items( $items, $args, $ajax = false ) {
    // Top Navigation Area Only
    if ( ( isset( $ajax ) && $ajax ) || ( property_exists( $args, \'theme_location\' ) && $args->theme_location === \'primary\' ) ) {
        // WooCommerce
        if ( class_exists( \'woocommerce\' ) ) {
            $css_class = \'menu-item menu-item-type-cart menu-item-type-woocommerce-cart\';
            // Is this the cart page?
            if ( is_cart() )
                $css_class .= \' current-menu-item\';
            //$items .= \'<li class="\' . esc_attr( $css_class ) . \'">\';
                $items .= \'<a class="cart-contents" href="\' . esc_url( WC()->cart->get_cart_url() ) . \'">\';
                    // $items .= wp_kses_data( WC()->cart->get_cart_total() ) . \' - <span class="count">\' .  wp_kses_data( sprintf( _n( \'%d item\', \'%d items\', WC()->cart->get_cart_contents_count(), \'simple-shop\' ), WC()->cart->get_cart_contents_count() ) ) . \'</span>\';
                $items .= \' (<span class="count">\' .  wp_kses_data( sprintf( _n( \'%d\', \'%d\', WC()->cart->get_cart_contents_count(), \'simple-shop\' ), WC()->cart->get_cart_contents_count() ) ) . \'</span>)\';
                $items .= \'</a>\';
            //$items .= \'</li>\';
        }
    }
    return $items;
}
/**
 * This function updates the Top Navigation WooCommerce cart link contents when an item is added via AJAX.
 */
add_filter( \'woocommerce_add_to_cart_fragments\', \'my_woocommerce_add_to_cart_fragments\' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
    // Add our fragment
    $fragments[\'li.menu-item-type-woocommerce-cart\'] = my_wp_nav_menu_items( \'\', new stdClass(), true );
    return $fragments;
}

EDIT (new code)

 add_filter( \'wp_setup_nav_menu_item\',\'my_item_setup\' );
function my_item_setup($item) {

    if ( class_exists( \'woocommerce\' ) ) {

        global $woocommerce;

        ?>
        <!--<pre><?php //var_dump( $woocommerce->cart->get_cart_contents_count()  ); ?></pre>-->        
        <?php


        if ( $item->url == esc_url( wc_get_cart_url() ) ) {


            //$item->title = \'MY BAG(\'. \'<span class="count">\' .  wp_kses_data( sprintf( _n( \'%d\', \'%d\', WC()->cart->get_cart_contents_count(), \'simple-shop\' ), WC()->cart->get_cart_contents_count() ) ) . \'</span>)\';

            //$item->title = \'MY BAG(\'. \'<span class="count">\' .  wp_kses_data( sprintf( _n( \'%d\', \'%d\', $woocommerce->cart->cart_contents_count(), \'simple-shop\' ), $woocommerce->cart->cart_contents_count() ) ) . \'</span>)\';


            $item->title = \'MY BAG(\'. \'<span class="count">\' .  $woocommerce->cart->get_cart_contents_count() . \'</span>)\';


            //$item->title = sprintf( _n( \'%d\', \'%d\', $woocommerce->cart->cart_contents_count();, \'simple-shop\' ), $woocommerce->cart->cart_contents_count(); );


        }
    }
    return $item;
}

2 个回复
最合适的回答,由SO网友:Louis 整理而成

我正在发布我使用的完整解决方案(使用两种语言,放在functions.php中):

add_filter( \'wp_setup_nav_menu_item\',\'my_item_setup\' );
function my_item_setup($item) {

    if ( ! is_admin() ) {
        if ( class_exists( \'woocommerce\' ) ) {

            global $woocommerce;

            if ( $item->url == esc_url( wc_get_cart_url() ) ) {

                if (is_null($woocommerce->cart)){

                } else {
                    if( get_locale() == \'fr_FR\' ) {
                        $item->title = \'PANIER (\'. \'<span class="count">\' .  $woocommerce->cart->get_cart_contents_count() . \'</span>)\';
                    } else {
                        $item->title = \'MY BAG (\'. \'<span class="count">\' .  $woocommerce->cart->get_cart_contents_count() . \'</span>)\';
                    }
                }

            }
        }
    }
    return $item;
}


/**
 * This function updates the cart link contents when an item is added via AJAX.
 */
add_filter( \'woocommerce_add_to_cart_fragments\', \'my_woocommerce_add_to_cart_fragments\' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
    // Add our fragment
    $fragments[\'li.menu-item-type-woocommerce-cart\'] = my_item_setup( \'\');
    return $fragments;
}

SO网友:Christian Lescuyer

$fragments数组中的项目将替换指定为键的元素的html内容。在您的示例中

$fragments[\'li.menu-item-type-woocommerce-cart\'] = \'some html\';
将在DOM元素中插入一些html;li class=“菜单项类型woocommerce cart”>;

下面是Javascript:

$( \'li.menu-item-type-woocommerce-cart\' ).replaceWith( \'some html\' );
你的菜单中已经有这样的元素了吗?

结束

相关推荐

Testing hooks callback

我正在开发一个使用TDD的插件,有一件事我完全没有测试出来,那就是。。。挂钩。我的意思是好的,我可以测试钩子回调,但我如何测试钩子是否真的触发了(自定义钩子和WordPress默认钩子)?我认为一些嘲弄会有所帮助,但我就是想不出我错过了什么。我用WP-CLI安装了测试套件。根据this answer, init 钩子应该触发,但。。。事实并非如此;此外,该代码在WordPress内部工作。根据我的理解,引导程序是最后加载的,所以不触发init是有意义的,所以剩下的问题是:如果触发了挂钩,我该如何测试?谢谢