add_action( 'woocommerce_account_my-coupons_endpoint', 'coupons_content' );
function coupons_content() {
$current_user_id = get_current_user_id();
// جستجو بر اساس ID کاربر (ما کوپن را با این متا ذخیره میکنیم)
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_customer_user', // فیلد استاندارد ووکامرس برای محدودیت کاربر
'value' => $current_user_id,
'compare' => '=',
),
),
);
$coupons = get_posts( $args );
if ( $coupons ) {
echo '
لیست کدهای تخفیف شما
';
echo '
| کد تخفیف |
مقدار |
تاریخ انقضا |
وضعیت |
';
foreach ( $coupons as $coupon_post ) {
$coupon = new WC_Coupon($coupon_post->post_title);
$expiry_date = $coupon->get_date_expires();
$now = new DateTime();
$is_expired = ($expiry_date && $expiry_date->getTimestamp() < $now->getTimestamp());
// بررسی اینکه آیا این کاربر (با ID خودش) قبلاً از این کوپن استفاده کرده یا نه
$used_by_user = in_array( $current_user_id, $coupon->get_used_by() );
if ( $used_by_user ) {
$status = 'استفاده شده';
} elseif ( $is_expired ) {
$status = 'منقضی شده';
} else {
$status = 'فعال';
}
$date_display = $expiry_date ? date_i18n( get_option( 'date_format' ), $expiry_date->getTimestamp() ) : 'بدون انقضا';
echo '
| ' . $coupon_post->post_title . ' |
' . $coupon->get_amount() . ($coupon->get_discount_type() == 'percent' ? '%' : ' تومان') . ' |
' . $date_display . ' |
' . $status . ' |
';
}
echo '
';
} else {
echo 'شما در حال حاضر کد تخفیف اختصاصی ندارید.
';
}
}
تابستان – عطر لیلوا
برای دیدن محصولات که دنبال آن هستید تایپ کنید.