woocommerce - Woo 自定义徽章/气泡条件
问题描述
我创建了一个自定义徽章/气泡“新!” 基于发布时间:
add_action( 'woocommerce_before_shop_loop_item', function() {
#add_action( 'woocommerce_single_product_image_html', function() {
$postdate = get_the_time( 'Y-m-d' ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}, 20 );
但我想添加条件以显示是否未设置“特价”。应该怎么看?
谢谢!
解决方案
WC_Product::is_on_sale() – 返回产品是否在销售。
function my_callback_function() {
global $product;
$postdate = get_the_date( 'Y-m-d', $product->get_id() ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
$onsale = $product->is_on_sale();
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp && !$onsale ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'my_callback_function', 10, 0 );
推荐阅读
- php - 将日期与laravel中mysql json数组的每个值进行比较
- r - 如何绘制每个属性的边际分布?
- c# - 如何在 LINQ 的 where 子句中传递动态条件?
- laravel - 如何在 Laravel 中获取相关列值的总和?
- angular - 如何从代码运行 Angular CLI 命令
- c - 修改字符串值导致访问冲突
- c++ - 当从对象引用创建时,来自向量的指针会导致 BAD_ACCESS 错误
- azure-application-insights - Azure Application Insights Kusto 查询能否获取用户的当前时区?
- ios - 无法加载模块 Razorpay
- go - 如何将 JSON 对象验证为无序列表?