【问题标题】:Delete WooCommerce coupons 30 days after they expire过期 30 天后删除 WooCommerce 优惠券
【发布时间】:2021-06-03 07:35:39
【问题描述】:

我有以下功能,可以在 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' => '<='
      ),
      array(
        'key'     => 'date_expires',
        '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' );

【问题讨论】:

    标签: php wordpress datetime woocommerce coupon


    【解决方案1】:

    您必须将当前日期与过去 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' => '<='
                ),
                array(
                    'key'     => 'date_expires',
                    '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' );
    

    【讨论】:

    • 完美,我知道是这样的,但我无法完全正确地获取格式。谢谢!
    • 欢迎...很高兴为您提供帮助。
    【解决方案2】:

    你能试试这个代码吗

    function wp_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', 'wp_delete_expired_coupons' );
    

    【讨论】:

    • 你能解释一下你做了什么,以及它是如何工作的,好吗?我在您的代码中没有看到 +30 天的引用。
    • 这不能回答问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2020-03-20
    相关资源
    最近更新 更多