Support » Plugin: Code Snippets » Feature request and Debug

  • Resolved someonebutwhere

    (@someonebutwhere)


    Hi Shea,

    Thanks for such a useful plugin, I love how it takes all my additions away from the theme’s functions file and it all becomes so much more useable =)

    First I have a feature request – the ability to Reorder and Tag snippets would be super useful!

    Please can you help me debug an error that one of my snippets is producing:
    Notice: Undefined variable: menu_item in /.../wp-content/mu-plugins/code-snippets/php/snippet-ops.php(384) : eval()'d code on line 32

    Cheers!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hey,

    I’m glad you’re finding the plugin useful!

    An ordering feature is in the works, but were you aware a tagging feature already exists? When adding or editing a snippet, you should be able to assign some tags to a snippet, and then filter snippets based on their tags on the manage menu.

    I can help you debug the snippet – but first, you’ll need to post the code here. Unfortunately, I can’t tell you the name of the snippet causing the error, but if you go looking through the code, you’re looking for one that has $menu_item somewhere on like 32 of the snippet.

    someonebutwhere

    (@someonebutwhere)

    Hi Shea,

    Wow, thanks for the wonderfully fast response! I will check out that tagging feature too, thanks.

    The snippet in culprit is quite a long one, and it was fine yesterday! So not sure what’s happened as I’m getting a bit snowed under with gateway issues…

    It’s fine as long as there is something in the cart but if it’s empty wp throws the error.

    Thanks so much for helping! =)

    add_filter('wp_nav_menu_items','sk_wcmenucart', 10, 2);
    function sk_wcmenucart($menu, $args) {
    
    	// Check if WooCommerce is active and add a new item to a menu assigned to Primary Navigation Menu location
    	if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'primary' !== $args->theme_location )
    		return $menu;
    
    	ob_start();
    		global $woocommerce;
    		$viewing_cart = __('View your shopping cart', 'your-theme-slug');
    		$start_shopping = __('Start shopping', 'your-theme-slug');
    		$cart_url = $woocommerce->cart->get_cart_url();
    		$shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
    		$cart_contents_count = $woocommerce->cart->cart_contents_count;
    		$cart_contents = sprintf(_n('%d item', '%d items', $cart_contents_count, 'your-theme-slug'), $cart_contents_count);
    		$cart_total = $woocommerce->cart->get_cart_total();
    		// Comment only the line below to show nav menu cart item when there are no items in the cart
    		if ( $cart_contents_count > 0 ) {
    		if ($cart_contents_count == 0) {
    				$menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $shop_page_url .'" title="'. $start_shopping .'">';
    			} else {
    				$menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $cart_url .'" title="'. $viewing_cart .'">';
    			}
    
    			$menu_item .= '<i class="fa fa-shopping-cart"></i> ';
    
    /*			$menu_item .= $cart_contents.' - '. $cart_total; */
    			$menu_item .= $cart_contents; // removed dollar amount in line above (cart_total)
    			$menu_item .= '</a></li>';
    		// Comment only the line below to show nav menu cart item when there are no items in the cart
    		}
    		echo $menu_item; // ••• LINE 32 •••
    	$social = ob_get_clean();
    	return $menu . $social;
    
    }
    Plugin Author Shea Bunge

    (@bungeshea)

    Ah okay, thanks for providing the snippet code.

    What’s happening here is you have a variable, $menu_item, which is only defined inside an if statement – if ( $cart_contents_count > 0 ). But then this variable is used outside the if statement, on line 32.

    In cases where this if statement doesn’t execute ($cart_contents_count is not greater than zero), the variable $menu_item is not defined – causing the error.

    The simple fix is to move the echo $menu_item statement inside the if statement. Like so:

    add_filter('wp_nav_menu_items','sk_wcmenucart', 10, 2);
    function sk_wcmenucart($menu, $args) {
    
    	// Check if WooCommerce is active and add a new item to a menu assigned to Primary Navigation Menu location
    	if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'primary' !== $args->theme_location ) {
    		return $menu;
    	}
    
    	ob_start();
    	global $woocommerce;
    	$viewing_cart = __('View your shopping cart', 'your-theme-slug');
    	$start_shopping = __('Start shopping', 'your-theme-slug');
    	$cart_url = $woocommerce->cart->get_cart_url();
    	$shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
    	$cart_contents_count = $woocommerce->cart->cart_contents_count;
    	$cart_contents = sprintf(_n('%d item', '%d items', $cart_contents_count, 'your-theme-slug'), $cart_contents_count);
    	$cart_total = $woocommerce->cart->get_cart_total();
    	// Comment only the line below to show nav menu cart item when there are no items in the cart
    	if ( $cart_contents_count > 0 ) {
    		if ($cart_contents_count == 0) {
    			$menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $shop_page_url .'" title="'. $start_shopping .'">';
    		} else {
    			$menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $cart_url .'" title="'. $viewing_cart .'">';
    		}
    
    		$menu_item .= '<i class="fa fa-shopping-cart"></i> ';
    
    /*			$menu_item .= $cart_contents.' - '. $cart_total; */
    		$menu_item .= $cart_contents; // removed dollar amount in line above (cart_total)
    		$menu_item .= '</a></li>';
    	// Comment only the line below to show nav menu cart item when there are no items in the cart
    		echo $menu_item;
    	}
    	$social = ob_get_clean();
    	return $menu . $social;
    }
    JarekM

    (@jmilewski)

    What regards re-ordering of the snippet list:

    The plugin provides a filter hook for the default sort order. If you would prefer e.g. “by name” instead of the default “by ID”, you need the following:


    function snippets_order(){ return 'name'; }
    add_filter( 'code_snippets/list_table/default_orderby', 'snippets_order' );

    And you can define it as a snippet!

    someonebutwhere

    (@someonebutwhere)

    @bungeshea You are a seriously kind person to have given me that explanation Shea, thank you so much! And of course it works now that I have made that change, so double thanks =) So snowed under right now hence delayed response, you really helped me…

    @jmilewski I might try that when I get a moment – thanks for posting! Nice one @bungeshea

    Plugin Author Shea Bunge

    (@bungeshea)

    No problem @someonebutwhere, glad to help out 🙂

    And @jmilewski’s snippet will definitely work if you want to change what column is used to order the snippets. It’s a really good solution if that’s what you’re looking for, and props to JarekM for discovering the filter hook.

    What I’m referring to is a feature I’m planning on including in the next minor version where you can drag and drop snippets in the list to arbitrarily change their order. It’s been a pretty often requested feature.

    JarekM

    (@jmilewski)

    @bungeshea, that’s probably an overkill, especially when the number of snippets goes beyond a single screen listing…

    In my view, it would be perfectly enough if you give the snippets an additional Order attribute (resulting in an extra integer column on the snippet listing) and make this attribute editable. This is how e.g. the Pages are ordered on the default WordPress pages listing.

    Then, to round up the whole thing, you could also provide an extra Settings option on the control panel, called e.g. “Default sorting order of the snippet list”, with a choice of ID / Name / Order.

    Plugin Author Shea Bunge

    (@bungeshea)

    That’s a good point @jmilewski; I hadn’t thought about pagination making a drag-and-drop ordering less useful.

    I think that your implementation idea sounds good, at the very least for an initial release. I’ll start working on it when I next have time.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Feature request and Debug’ is closed to new replies.