WordPress.org

Ready to get started?Download WordPress

Forums

If Menu
plugin custom logic (3 posts)

  1. jimlongo
    Member
    Posted 10 months ago #

    I have a variable that I can echo
    global $woocommerce; var_dump($woocommerce->cart->cart_contents_count);
    that returns 0 if the cart is empty and an integer greater than 0 if it has contents.

    In my custom plugin I put the following

    add_filter( 'if_menu_conditions' , 'my_cart_condition' );
    	function my_cart_condition() {
    	global $woocommerce;
    	return( $woocommerce->cart->cart_contents_count > 0 );
    	}

    this empties the if drop down in the admin section of the plugin.

    even an empty function does the same.

    add_filter( 'if_menu_conditions' , 'my_cart_condition' );
    	function my_cart_condition() {
    	}

    What am I doing wrong?

    Thanks for your help.

    http://wordpress.org/extend/plugins/if-menu/

  2. jimlongo
    Member
    Posted 10 months ago #

    I read the readme.txt which made the formatting a little clearer and I tried the following in my custom plugin

    add_filter( 'if_menu_conditions' , 'my_cart_condition' );
    	function my_cart_condition( $conditions ) {
    		$conditions[] = array(
    			'name'		=>	 'Cart has Contents',
    			'condition'        =>	function() {
    				global $woocommerce;
    				return( $woocommerce->cart->cart_contents_count > 0 );
    			}
    		);
    	}

    this fixed the admin menu, but still doesn't work.
    I also tried formatting like the code in your plugin, which also didn't work.

    'name'		=>	__( 'Cart has Contents', 'if-menu' ),

    I finally edited your conditions.php to add this to the conditions and THIS WORKS.

    $conditions[] = array(
    		'name'		=>	__( 'Cart has Contents', 'if-menu' ),
    		'condition'	=>	'if_menu_cart_has_contents'
    	);

    and then this at the bottom

    function if_menu_cart_has_contents() {
    	global $woocommerce;
    	if( $woocommerce->cart->cart_contents_count > 0 ) return true;
    	return false;
    }

    Would be much better if I could put it in my custom plugin, but i couldn't get it to work with that method.

    Thank you, hth

  3. BramNL
    Member
    Posted 9 months ago #

    The key is that you guys missed the return $conditions;. This is a flaw in the FAQ.

Reply

You must log in to post.

About this Plugin

About this Topic