0

I have an action hook which simply displays a contact form for signup:

add_action('woocommerce_single_product_summary','add_product_signup', 10, 2);
function add_product_signup() {
    do_shortcode('[contact-form-7 id="20709" title="Product Sale Notification Signup"]');
}

This works ok BUT... I want to only show the form if the product is NOT on sale.

How can I access the 'on_sale' variable to test for true/false to then show/hide this contact form?

1

There is method on product class that is called is_on_sale() which actually determines if the product is on sale or not. You can access it from global $product variable. And must echo the do_shortcode. So the whole code will be like-

add_action('woocommerce_single_product_summary','add_product_signup', 10, 2);
function add_product_signup() {
    global $product;
    if( $product->is_on_sale() ) {
        echo do_shortcode('[contact-form-7 id="20709" title="Product Sale Notification Signup"]');
    }
}

The above code is tested. I tested it personally and it worked pretty well.

  • Oh, Of course. I forgot the global $product... I was getting an error on the if ( $product->is_on_sale() ) { and figured I couldn't access it. Thanks . Worked perfectly – php-b-grader Feb 2 '17 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.