Home Using multiple WooCommerce conditional tags together in an if statement
Reply: 1

Using multiple WooCommerce conditional tags together in an if statement

Trisha
1#
Trisha Published in 2017-12-07 23:05:19Z

I'm having an issue with is_cart(), is_checkout(), is_account_page() functions.

What I'm trying to do:

  • if it's the Cart page, then echo this shortcode.
  • if it's NOT the Cart page, then do nothing.

This needs to happen at the top of the page and at the bottom, both outside of the page's container.

When I view the cart, checkout, account pages, it works flawlessly. But as soon as I view any other page that is not the cart/checkout/account page, the shortcode still shows!

The site is using the Divi theme (by ElegantThemes) with WooCommerce. Here is are the snippets I'm working with:

<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
 <?php echo do_shortcode('[et_pb_section global_module="218"][/et_pb_section]'); ?>
 <?php endif; ?>

<?php endif; ?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
 <?php echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]'); ?>
 <?php endif; ?>

Here is the entire page.php code:

<?php

get_header();

$is_page_builder_used = et_pb_is_pagebuilder_used( get_the_ID() );
?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
 <?php echo do_shortcode('[et_pb_section global_module="218"][/et_pb_section]'); ?>
 <?php endif; ?>

    <div id="main-content">


<?php if ( ! $is_page_builder_used ) : ?>

    <div class="container">
        <div id="content-area" class="clearfix">
            <div id="left-area">

<?php endif; ?>

            <?php while ( have_posts() ) : the_post(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <?php if ( ! $is_page_builder_used ) : ?>

                    <h1 class="entry-title main_title"><?php the_title(); ?></h1>
                <?php
                    $thumb = '';

                    $width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );

                    $height = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
                    $classtext = 'et_featured_image';
                    $titletext = get_the_title();
                    $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
                    $thumb = $thumbnail["thumb"];

                    if ( 'on' === et_get_option( 'divi_page_thumbnails', 'false' ) && '' !== $thumb )
                        print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height );
                ?>

                <?php endif; ?>

                    <div class="entry-content">
                    <?php
                        the_content();

                        if ( ! $is_page_builder_used )
                            wp_link_pages( array( 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'Divi' ), 'after' => '</div>' ) );
                    ?>
                    </div> <!-- .entry-content -->

                <?php
                    if ( ! $is_page_builder_used && comments_open() && 'on' === et_get_option( 'divi_show_pagescomments', 'false' ) ) comments_template( '', true );
                ?>

                </article> <!-- .et_pb_post -->

            <?php endwhile; ?>

<?php if ( ! $is_page_builder_used ) : ?>

            </div> <!-- #left-area -->

            <?php get_sidebar(); ?>
        </div> <!-- #content-area -->
    </div> <!-- .container -->

<?php endif; ?>
<?php if ( ! (function_exists('is_cart') && is_cart()) || ( ! function_exists('is_account_page') && is_account_page()) || ( ! function_exists('is_checkout') && is_checkout() ) ) : ?>
 <?php echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]'); ?>
 <?php endif; ?>
</div> <!-- #main-content -->

<?php get_footer(); ?>
LoicTheAztec
2#
LoicTheAztec Reply to 2017-12-07 23:42:19Z

If I understand well, you would like to output this shortcode content only in cart, checkout and account WooCommerce pages

Then you should better try this shorter way:

<?php  
    if ( class_exists( 'WooCommerce' ) && ( is_cart() || is_account_page() || is_checkout() ) ) :
        echo do_shortcode('[et_pb_section global_module="247"][/et_pb_section]');
    endif; 
?>

Using class_exists( 'WooCommerce' ) will ensure that woocommerce plugin is loaded and that the conditional tags will work properly…

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299434 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO