How to trash expired and used coupons in Woocommerce automatically?

本秂侑毒 提交于 2019-12-11 08:09:55

问题


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.

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

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.


回答1:


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



回答2:


Thanks this works just fine! I should have mentioned that a separate function was also ok.



来源:https://stackoverflow.com/questions/58224336/how-to-trash-expired-and-used-coupons-in-woocommerce-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!