Viewing 1 replies (of 1 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi! Unfortunately it is not possible to influence the priority of Menu Cart. You may want to check the alignment setting though, if that’s set to ‘right’, this may be a simple matter of using the default alignment.

    A more complicated solution is modifying the position of the menu cart item with jQuery:

    add_action( 'wp_footer', 'wpmenucart_move_position' );
    function wpmenucart_move_position() {
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function($) {
            $(".wpmenucartli").each( function() {
                // count number of items in menu
                $parent_menu = $(this).closest('ul');
                var items_count = $parent_menu.children('li').length;
                var position = items_count - 1; // could be - 2 or 3, not sure about the WPML menu items
                $(this).insertBefore( $parent_menu.children('li').eq(position) );
            });
        });
        </script>
        <?php
    }

    If you don’t like using jQuery for this, there’s an even more complex server side/PHP solution:

    /**
     * Apply WP Menu Cart repositioning filter to all menus from the settings
     */
    add_action( 'init', 'wpmenucart_filter_nav_menus' );
    function wpmenucart_filter_nav_menus() {
        global $wpMenuCart;
        // exit if no shop class is active
        if ( !isset($wpMenuCart->shop) )
            return;
        // exit if no menus set
        if ( !isset( $wpMenuCart->options['menu_slugs'] ) || empty( $wpMenuCart->options['menu_slugs'] ) )
            return;
        if ( $wpMenuCart->options['menu_slugs'][1] != '0' ) {
            add_filter( 'wp_nav_menu_' . $wpMenuCart->options['menu_slugs'][1] . '_items', 'wpmenucart_reposition_item' , 999, 2 );
        }
    }
    
    /**
     * Modify the $items string to put Menu Cart in a predefined position
     */
    function wpmenucart_reposition_item( $items, $args ) {
        $new_position = 2; // change this to the position of your choice
        // we need to add a parent node to be able to handle this properly
        $ul = '<ul id="wpmenucart-menu">'.$items.'</ul>';
    
        // $ul is flat HTML, so we convert it to DomDocument first
        $dom = new DOMDocument();
        $dom->loadHTML($ul, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $dom_ul = $dom->getElementById('wpmenucart-menu');
        $wpmc_item = $dom->getElementById('wpmenucartli');
    
        $node_position = 1;
        foreach ($dom_ul->childNodes as $childNode) {
            // skipping non-li nodes
            if ( !isset($childNode->tagName) || $childNode->tagName != 'li' ) {
                $new_position++;
                $node_position++;
                continue;
            }
    
            // exit if we've found our node
            if ($new_position == $node_position) {
                $before = $childNode;
                break;
            }
    
            $node_position++;
        }
    
        // now that we know which node we want to put the Menu Cart before, we can move it
        $dom_ul->insertBefore(
            $wpmc_item,
            $before
        );
    
        // convert dom back to HTML
        $ul = $dom->saveHTML();
    
        // strip our ul wrapper again
        $open_ul_pos  = strpos($ul, '<ul id="wpmenucart-menu">') + strlen('<ul id="wpmenucart-menu">');
        $close_ul_pos = strrpos($ul, '</ul>');
        $snippet_length = $close_ul_pos - $open_ul_pos;
        $items = substr($ul, $open_ul_pos, $snippet_length);
    
        return $items;
    }

    But like I said, this is pretty advanced stuff…

    Hope that helps though!
    Ewout

Viewing 1 replies (of 1 total)
  • The topic ‘Determine position’ is closed to new replies.