Support » Plugins » PHP Else Statement in functions.php

  • Resolved Design Locker

    (@design-locker)


    Hi,

    I am trying to put a PHP Else Statement in functions.php in my child theme that is running woocommerce.

    It currently has the following code in the functions.php file which is working perfectly.

    // adds notice at single product page above add to cart
    
    add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
    function return_policy() {
        echo '<p id="rtrn">To Order<br>Qty 1: a fat quarter of 22 x 18 inches<br>Qty 2: 1/2 yard of 18 x 44 inches<br>Qty 3: 3/4 of a yard at 27 x 44 inches<br>Qty 4: 1 yard at 36 x 44 inches</p>';
    }

    The above adds a notice before the add to cart button. See http://www.sewcraftyfabrics.com/wip/product/handmade-traditional-teddy-bear/

    What I require is that the above notice shows on products in all categories except one category (category 10).

    I am not a PHP programmer and below is what I have tried and am failing miserably as it crashed the site;

    <?php if (is_category( '10' )) { ?>
    add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
    function return_policy() {
        echo '<p id="rtrn">&nbsp;</p>';
    } else {
       echo '<p id="rtrn">To Order<br>Qty 1: a fat quarter of 22 x 18 inches<br>Qty 2: 1/2 yard of 18 x 44 inches<br>Qty 3: 3/4 of a yard at 27 x 44 inches<br>Qty 4: 1 yard at 36 x 44 inches</p>';
    }
    <?php } ?>

    Any help would be much appreciated.

Viewing 1 replies (of 1 total)
  • Hi,

    To anyone reading this post, I somehow overlooked this in the Codex.

    The conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

    In other words, what I was requesting above can not be done.

Viewing 1 replies (of 1 total)
  • The topic ‘PHP Else Statement in functions.php’ is closed to new replies.