如何自动清除 Woocommerce 中过期和使用过的优惠券?
How to trash expired and used coupons in Woocommerce automatically?
在我的 functions.php 中,我想添加一个功能,将已达到使用限制的过期优惠券 and/or 移至垃圾箱。我想丢弃使用限制为 1 且使用次数为 1 或更多的优惠券。我希望此功能每天 运行。
我已经找到了过期优惠券的解决方案。但是,我仍然需要将已达到使用限制的优惠券也删除到垃圾箱。以下代码取自该网站。
https://nicola.blog/2018/08/01/delete-expired-coupons-automatically/
/**
* Schedule the daily event if necessary.
*/
function schedule_delete_expired_coupons() {
if ( ! wp_next_scheduled( 'delete_expired_coupons' ) ) {
wp_schedule_event( time(), 'daily', 'delete_expired_coupons' );
}
}
add_action( 'init', 'schedule_delete_expired_coupons' );
/**
* Trash all expired coupons when the event is triggered.
*/
function delete_expired_coupons() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'expiry_date',
'value' => current_time( 'Y-m-d' ),
'compare' => '<='
),
array(
'key' => 'expiry_date',
'value' => '',
'compare' => '!='
)
)
);
$coupons = get_posts( $args );
if ( ! empty( $coupons ) ) {
$current_time = current_time( 'timestamp' );
foreach ( $coupons as $coupon ) {
wp_trash_post( $coupon->ID );
}
}
}
add_action( 'delete_expired_coupons', 'delete_expired_coupons' );
对于上面粘贴的函数,我想添加一些代码,这些代码还将使用限制为 1 且使用次数为 1 或更多的优惠券移至垃圾箱。使用限制是每张优惠券,而不是每个用户。如有任何帮助,我们将不胜感激。
很遗憾,我找不到修改您的查询以包含已达到使用限制的优惠券的方法。但是,您可以添加以下内容来查询优惠券并循环查找达到其限制的优惠券。
add_action( 'delete_expired_coupons', 'delete_used_coupons' );
function delete_used_coupons() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'usage_count',
'value' => 0,
'compare' => '>'
),
array(
'key' => 'usage_limit',
'value' => 0,
'compare' => '>'
)
)
);
$coupons = get_posts( $args );
if ( ! empty( $coupons ) ) {
foreach ( $coupons as $coupon ) {
if ($coupon->get_usage_count() >= $coupon->get_usage_limit()){
wp_trash_post( $coupon->ID );
}
}
}
}
在我的 functions.php 中,我想添加一个功能,将已达到使用限制的过期优惠券 and/or 移至垃圾箱。我想丢弃使用限制为 1 且使用次数为 1 或更多的优惠券。我希望此功能每天 运行。
我已经找到了过期优惠券的解决方案。但是,我仍然需要将已达到使用限制的优惠券也删除到垃圾箱。以下代码取自该网站。 https://nicola.blog/2018/08/01/delete-expired-coupons-automatically/
/**
* Schedule the daily event if necessary.
*/
function schedule_delete_expired_coupons() {
if ( ! wp_next_scheduled( 'delete_expired_coupons' ) ) {
wp_schedule_event( time(), 'daily', 'delete_expired_coupons' );
}
}
add_action( 'init', 'schedule_delete_expired_coupons' );
/**
* Trash all expired coupons when the event is triggered.
*/
function delete_expired_coupons() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'expiry_date',
'value' => current_time( 'Y-m-d' ),
'compare' => '<='
),
array(
'key' => 'expiry_date',
'value' => '',
'compare' => '!='
)
)
);
$coupons = get_posts( $args );
if ( ! empty( $coupons ) ) {
$current_time = current_time( 'timestamp' );
foreach ( $coupons as $coupon ) {
wp_trash_post( $coupon->ID );
}
}
}
add_action( 'delete_expired_coupons', 'delete_expired_coupons' );
对于上面粘贴的函数,我想添加一些代码,这些代码还将使用限制为 1 且使用次数为 1 或更多的优惠券移至垃圾箱。使用限制是每张优惠券,而不是每个用户。如有任何帮助,我们将不胜感激。
很遗憾,我找不到修改您的查询以包含已达到使用限制的优惠券的方法。但是,您可以添加以下内容来查询优惠券并循环查找达到其限制的优惠券。
add_action( 'delete_expired_coupons', 'delete_used_coupons' );
function delete_used_coupons() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'usage_count',
'value' => 0,
'compare' => '>'
),
array(
'key' => 'usage_limit',
'value' => 0,
'compare' => '>'
)
)
);
$coupons = get_posts( $args );
if ( ! empty( $coupons ) ) {
foreach ( $coupons as $coupon ) {
if ($coupon->get_usage_count() >= $coupon->get_usage_limit()){
wp_trash_post( $coupon->ID );
}
}
}
}