检查一个函数在另一个函数中是否为真的问题 php
Problem to check if a function is true in another function php
我使用这段代码来检查函数 1 是否正确。如果为真,则函数 2 分配一个 CSS class,否则另一个:
function first() {
return TRUE;
}
function second() {
if (first() == TRUE) {
}
}
结果有效,但我遇到了问题。它向我显示了前端站点的布尔值,即“1”(该值在 html 代码中,周围没有任何标记)。您知道如何不显示此值吗?
前端效果图如下:
HTML 代码如下所示:
<a>
<span class="onsale">-50%</span>
1
<img>
这是我的完整代码:
// Sales badge
add_action( 'woocommerce_sale_flash', 'sale_badge_percentage', 25 );
function sale_badge_percentage() {
global $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'simple' ) ) {
$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} elseif ( $product->is_type( 'variable' ) ) {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
if ( $max_percentage > 0 ) {
echo "<span class='onsale'>-" . round($max_percentage) . "%</span>";
return TRUE;
}
}
// New badge for recent products
add_action( 'woocommerce_before_shop_loop_item_title', 'new_badge', 3 );
function new_badge() {
if (sale_badge_percentage() == TRUE) {
/* la variable existe */
global $product;
$newness_days = 30; // Number of days the badge is shown
$created = strtotime( $product->get_date_created() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
echo '<span class="ct-woo-card-extra new-badge">' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
}
}
else {
/* la variable n'existe pas */
global $product;
$newness_days = 30; // Number of days the badge is shown
$created = strtotime( $product->get_date_created() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
echo '<span class="ct-woo-card-extra new-badge solobadge">' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
}
}
}
已解决!!我只在 return TRUE;
上方的 return
中更改了 echo
我使用这段代码来检查函数 1 是否正确。如果为真,则函数 2 分配一个 CSS class,否则另一个:
function first() {
return TRUE;
}
function second() {
if (first() == TRUE) {
}
}
结果有效,但我遇到了问题。它向我显示了前端站点的布尔值,即“1”(该值在 html 代码中,周围没有任何标记)。您知道如何不显示此值吗?
前端效果图如下:
HTML 代码如下所示:
<a>
<span class="onsale">-50%</span>
1
<img>
这是我的完整代码:
// Sales badge
add_action( 'woocommerce_sale_flash', 'sale_badge_percentage', 25 );
function sale_badge_percentage() {
global $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'simple' ) ) {
$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} elseif ( $product->is_type( 'variable' ) ) {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
if ( $max_percentage > 0 ) {
echo "<span class='onsale'>-" . round($max_percentage) . "%</span>";
return TRUE;
}
}
// New badge for recent products
add_action( 'woocommerce_before_shop_loop_item_title', 'new_badge', 3 );
function new_badge() {
if (sale_badge_percentage() == TRUE) {
/* la variable existe */
global $product;
$newness_days = 30; // Number of days the badge is shown
$created = strtotime( $product->get_date_created() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
echo '<span class="ct-woo-card-extra new-badge">' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
}
}
else {
/* la variable n'existe pas */
global $product;
$newness_days = 30; // Number of days the badge is shown
$created = strtotime( $product->get_date_created() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
echo '<span class="ct-woo-card-extra new-badge solobadge">' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
}
}
}
已解决!!我只在 return TRUE;
return
中更改了 echo