如何在 Woocommerce 中自动删除过期和使用过的优惠券? [英] How to trash expired and used coupons in Woocommerce automatically?

查看:30
本文介绍了如何在 Woocommerce 中自动删除过期和使用过的优惠券?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的functions.php 中,我想添加一个函数,将过期的优惠券和/或已达到使用限制的优惠券移至垃圾箱.我想丢弃使用限制为 1 且使用次数为 1 或更多的优惠券.我希望每天运行此功能.

In my functions.php I want to add a function that moves expired coupons and/or coupons that have reached their usage limit to the trash. I would like to trash coupons that have a usage limit of 1 and a usage count of 1 or more. I would like this function to run on a daily basis.

我已经找到了过期优惠券的解决方案.但是,我仍然需要将已达到使用限制的优惠券也删除到垃圾箱中.以下代码取自本网站.https://nicola.blog/2018/08/01/delete-expired-coupons-automatically/

I have already found a solution for the expired coupons. However I still need the coupons that have reached their usage limit to be removed to the trash as well. The below code is taken from this website. 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 或更多的优惠券移至垃圾箱.使用限制是每个优惠券,而不是每个用户.对此的任何帮助将不胜感激.

To the function pasted above I would like to add some code which also moves coupons with a usage limit of 1 and and a usage count of 1 or more to the trash. The usage limit is per coupon, not per user. Any help with this would be very appreciated.

推荐答案

很遗憾,我找不到修改您的查询以包含达到使用限制的优惠券的方法.但是,您可以添加以下内容来查询优惠券并循环查找达到限额的优惠券.

Unfortunately I couldn't find a way to modify your query to include the coupons that reached their usage limit. However, you can add the following to query the coupons and loop through it to find the ones that reached their limit.

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 );
            }
        }
    }
}

这篇关于如何在 Woocommerce 中自动删除过期和使用过的优惠券?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆