plugin custom logic
-
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.
-
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
The key is that you guys missed the return $conditions;. This is a flaw in the FAQ.
The topic ‘plugin custom logic’ is closed to new replies.