оператор if — php: if $ null скрыть таблицу

У меня есть этот почтовый шаблон от 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 ); ?>

Я добавил некоторые пользовательские пост-данные, но теперь мне действительно нужно скрыть «Дополнительная информация о туре», когда моя $ booking_info равна нулю, потому что если ее нет, клиент получает пустую таблицу только с заголовками.

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;
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 ); ?>
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector