【发布时间】:2023-05-22 20:39:01
【问题描述】:
我有以下循环用于在客户仪表板的我的帐户部分的页面上获取 Woocommerce 优惠券。
目前我们有 10k+ 优惠券,仅通过执行此循环,就会大量消耗资源并且效率不高,导致超时。有什么明显的方法可以提高它的效率吗?
有没有办法可以将循环限制为仅在“允许的电子邮件”字段中搜索电子邮件(因为每张优惠券都与一个电子邮件地址相关联)?
<?php $smart_coupons = get_posts( array(
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'desc',
'post_type' => 'shop_coupon',
'post_status' => 'publish'
) );
if ( $smart_coupons ) {
foreach( $smart_coupons as $smart_coupon) {
$strcode = strtolower($smart_coupon->post_title);
$full_coupon = new WC_Coupon( $strcode ); ?>
<?php if($full_coupon->discount_type == "smart_coupon"){
$emails = $full_coupon->get_email_restrictions();
if (in_array($current_email, $emails)) {
if($full_coupon->usage_count < $full_coupon->usage_limit){ ?>
coupon content
<?php }
}
}
}
}
【问题讨论】:
标签: php sql wordpress woocommerce coupon