过期 30 天后删除 WooCommerce 优惠券

问题描述

我有以下功能,它会在到期当天删除 WooCommerce 优惠券。

如何让它在到期日期 30 天后删除它们?

function delete_expired_coupons() {
  $args = array(
    'posts_per_page' => -1,'post_type'      => 'shop_coupon','post_status'    => 'publish','meta_query'     => array(
      'relation'   => 'AND',array(
        'key'     => 'date_expires','value'   => current_time( 'timestamp' ),'compare' => '<='
      ),'value'   => '','compare' => '!='
      )
    )
  );

  $coupons = get_posts( $args );

  if ( ! empty( $coupons ) ) {
    foreach ( $coupons as $coupon ) {
      wp_trash_post( $coupon->ID );
    }
  }
}
add_action( 'delete_expired_coupons','delete_expired_coupons' );

解决方法

您必须将当前日期与过去 30 天的日期进行比较。检查下面的代码。

function delete_expired_coupons() {

    $args = array(
        'posts_per_page' => -1,'post_type'      => 'shop_coupon','post_status'    => 'publish','meta_query'     => array(
            'relation'   => 'AND',array(
                'key'     => 'date_expires','value'   => strtotime( '-30 days',current_time( 'timestamp' ) ),'compare' => '<='
            ),'value'   => '','compare' => '!='
            )
        )
    );

    $coupons = get_posts( $args );

    if ( ! empty( $coupons ) ) {
        foreach ( $coupons as $coupon ) {
            wp_trash_post( $coupon->ID );
        }
    }
    
}
add_action( 'delete_expired_coupons','delete_expired_coupons' );
,

你能试试这个代码吗

function wp_delete_expired_coupons() {
    $args = array(
        'posts_per_page' => -1,array(
                'key'     => 'expiry_date','value'   => current_time( 'Y-m-d' ),'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','wp_delete_expired_coupons' );

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...