php:if $ is null隐藏表

时间:2017-06-09 15:30:04

标签: php if-statement woocommerce html-table conditional-statements

我有来自Woocommerce的这个邮件模板,

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$text_align = is_rtl() ? 'right' : 'left';

do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>

<?php if ( ! $sent_to_admin ) : ?>
    <h2><?php printf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ); ?></h2>
<?php else : ?>
    <h2><a class="link" href="<?php echo esc_url( admin_url( 'post.php?post=' . $order->get_id() . '&action=edit' ) ); ?>"><?php printf( __( 'Order #%s', 'woocommerce'), $order->get_order_number() ); ?></a> (<?php printf( '<time datetime="%s">%s</time>', date_i18n( 'c', strtotime( $order->get_date_created() ) ), date_i18n( wc_date_format(), strtotime( $order->get_date_created() ) ) ); ?>)</h2>
<?php endif; ?>

<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
    <thead>
        <tr>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Product', 'woocommerce' ); ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Price', 'woocommerce' ); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php echo wc_get_email_order_items( $order, array(
            'show_sku'      => $sent_to_admin,
            'show_image'    => false,
            'image_size'    => array( 32, 32 ),
            'plain_text'    => $plain_text,
            'sent_to_admin' => $sent_to_admin,
        ) ); ?>
    </tbody>
    <tfoot>
        <?php
            if ( $totals = $order->get_order_item_totals() ) {
                $i = 0;
                foreach ( $totals as $total ) {
                    $i++;
                    ?><tr>
                        <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>; <?php if ( $i === 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php if ( $i === 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
                    </tr><?php
                }
            }
        ?>
    </tfoot>
</table>

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

<h3 style="margin: 20px 0;"><?php _e( 'Additional Tour Information', 'citytours' ) ?></h3>

<table class="td" cellspacing="0" cellpadding="6" style="width: 108%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
    <thead>
        <tr>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Product', 'woocommerce' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Booking No', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Date', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Pin Code', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Adults', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Kids', 'citytours' ) ?></th>
        </tr>

    </thead>
    <tbody>
        <?php 
        global $wpdb;
        $items = $order->get_items(); 

        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            $hotel_tour_id = get_post_meta( $product_id, '_ct_post_id', true );

            if ( $hotel_tour_id ) { 
                $booking_info = $wpdb->get_row( 'SELECT A.booking_no, A.pin_code, A.date_from, A.total_adults, A.total_kids , B.post_content
                FROM ' . CT_ORDER_TABLE . ' as A INNER JOIN ' .wp_posts. ' AS B ON A.POST_ID = B.ID
                WHERE other = ' . $order->get_order_number(), ARRAY_A );
                $newDate = date("d-m-Y", strtotime($booking_info['date_from']));
                ?>
                <tr>
                    <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $item['name'] ?></th>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['booking_no'] ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $newDate ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['pin_code'] ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['total_adults'] ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['total_kids'] ?></td>
                </tr>
                <tr>
                <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Description', 'citytours' ) ?></th>
                <td class="td" scope="col" colspan="5" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['post_content'] ?></td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>

</table>


<?php endif; ?>

<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>

我添加了一些自定义后期数据,但现在我真的需要隐藏&#34;其他旅游信息&#34;当我的$ booking_info为空时,因为如果没有,则客户获得一个带有标题的空表。

2 个答案:

答案 0 :(得分:0)

你应该做的是存储来自

的所有html数据
<h3 style="margin: 20px 0;"><?php _e( 'Additional Tour Information','citytours' ) ?></h3>

</table>

在php变量内部而不是平面显示,并设置一个变量,表明你的表中是否有数据,例如

$content_filled = 0; // says you havent parsed your array so far

$table = '<h3 style="margin: 20px 0;"><?php _e( 'Additional Tour Information','citytours' ) ?></h3>';

/ *这里的一些代码* /

$content_filled = 1; // says now you parsed your array
$table  .='<tr>
                    <th class="td" scope="col" style="text-align:'.$text_align'.>'.$item['name'].'</th>
                    <td class="td" scope="col" style="text-align:'.$text_align'.">'.$booking_info['booking_no'].'</td>';

然后你最后做了

if($content_filled)
    echo $table;

答案 1 :(得分:0)

无需重写太多,最简单但最不优雅的修复方法是使用输出缓冲区:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$text_align = is_rtl() ? 'right' : 'left';

do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>

<?php if ( ! $sent_to_admin ) : ?>
    <h2><?php printf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ); ?></h2>
<?php else : ?>
    <h2><a class="link" href="<?php echo esc_url( admin_url( 'post.php?post=' . $order->get_id() . '&action=edit' ) ); ?>"><?php printf( __( 'Order #%s', 'woocommerce'), $order->get_order_number() ); ?></a> (<?php printf( '<time datetime="%s">%s</time>', date_i18n( 'c', strtotime( $order->get_date_created() ) ), date_i18n( wc_date_format(), strtotime( $order->get_date_created() ) ) ); ?>)</h2>
<?php endif; ?>

<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
    <thead>
        <tr>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Product', 'woocommerce' ); ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Price', 'woocommerce' ); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php echo wc_get_email_order_items( $order, array(
            'show_sku'      => $sent_to_admin,
            'show_image'    => false,
            'image_size'    => array( 32, 32 ),
            'plain_text'    => $plain_text,
            'sent_to_admin' => $sent_to_admin,
        ) ); ?>
    </tbody>
    <tfoot>
        <?php
            if ( $totals = $order->get_order_item_totals() ) {
                $i = 0;
                foreach ( $totals as $total ) {
                    $i++;
                    ?><tr>
                        <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>; <?php if ( $i === 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php if ( $i === 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
                    </tr><?php
                }
            }
        ?>
    </tfoot>
</table>

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

<?php ob_start(); ?>

<h3 style="margin: 20px 0;"><?php _e( 'Additional Tour Information', 'citytours' ) ?></h3>

<table class="td" cellspacing="0" cellpadding="6" style="width: 108%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
    <thead>
        <tr>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Product', 'woocommerce' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Booking No', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Date', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Pin Code', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Adults', 'citytours' ) ?></th>
            <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Kids', 'citytours' ) ?></th>
        </tr>

    </thead>
    <tbody>
        <?php 
        global $wpdb;
        $items = $order->get_items(); 

        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            $hotel_tour_id = get_post_meta( $product_id, '_ct_post_id', true );

            if ( $hotel_tour_id ) { 
                $booking_info = $wpdb->get_row( 'SELECT A.booking_no, A.pin_code, A.date_from, A.total_adults, A.total_kids , B.post_content
                FROM ' . CT_ORDER_TABLE . ' as A INNER JOIN ' .wp_posts. ' AS B ON A.POST_ID = B.ID
                WHERE other = ' . $order->get_order_number(), ARRAY_A );
                $newDate = date("d-m-Y", strtotime($booking_info['date_from']));
                ?>
                <tr>
                    <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $item['name'] ?></th>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['booking_no'] ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $newDate ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['pin_code'] ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['total_adults'] ?></td>
                    <td class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['total_kids'] ?></td>
                </tr>
                <tr>
                <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html__( 'Description', 'citytours' ) ?></th>
                <td class="td" scope="col" colspan="5" style="text-align:<?php echo $text_align; ?>;"><?php echo $booking_info['post_content'] ?></td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>

</table>

<?php 

$output = ob_get_clean();

if(isset($booking_info)){
    echo $output;
}?>

<?php endif; ?>

<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>