403Webshell
Server IP : 104.171.130.249  /  Your IP : 216.73.216.146
Web Server : nginx/1.25.5
System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64
User : root ( 0)
PHP Version : 8.4.23
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : ON  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /var/www/html/wp-content/themes/matreshka/inc/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/wp-content/themes/matreshka/inc/first-order-discount.php
<?php
/**
 * Скидка 500₽ на первый заказ от 1500₽
 * - Автоматически применяется в корзине
 * - После использования блок на главной скрывается
 * - В админке можно сбросить для тестирования
 */

// === DISCOUNT LOGIC ===

add_action( 'woocommerce_cart_calculate_fees', 'matreshka_first_order_discount' );
function matreshka_first_order_discount( $cart ) {
	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
	if ( ! is_user_logged_in() ) return;

	$user_id = get_current_user_id();
	if ( get_user_meta( $user_id, '_matreshka_first_order_used', true ) ) return;

	// Проверяем что у пользователя нет завершённых заказов
	$orders = wc_get_orders( [
		'customer_id' => $user_id, 'status' => [ 'completed', 'processing' ],
		'limit' => 1, 'return' => 'ids',
	] );
	if ( ! empty( $orders ) ) return;

	$subtotal = $cart->get_subtotal();
	if ( $subtotal >= 1500 ) {
		$cart->add_fee( 'Скидка на первый заказ', -500 );
	}
}

// Помечаем что скидка использована после оформления заказа
add_action( 'woocommerce_checkout_order_processed', 'matreshka_mark_first_order_used' );
function matreshka_mark_first_order_used( $order_id ) {
	$order = wc_get_order( $order_id );
	if ( ! $order ) return;

	foreach ( $order->get_fees() as $fee ) {
		if ( strpos( $fee->get_name(), 'первый заказ' ) !== false ) {
			update_user_meta( $order->get_customer_id(), '_matreshka_first_order_used', time() );
			break;
		}
	}
}

// === HELPER: проверка доступности скидки ===

function matreshka_first_order_discount_available() {
	if ( ! is_user_logged_in() ) return true; // для гостей показываем блок (привлекаем зарегаться)

	$user_id = get_current_user_id();
	if ( get_user_meta( $user_id, '_matreshka_first_order_used', true ) ) return false;

	$orders = wc_get_orders( [
		'customer_id' => $user_id, 'status' => [ 'completed', 'processing' ],
		'limit' => 1, 'return' => 'ids',
	] );

	return empty( $orders );
}

// === ADMIN: сброс скидки для тестирования ===

add_action( 'show_user_profile', 'matreshka_first_order_admin_field' );
add_action( 'edit_user_profile', 'matreshka_first_order_admin_field' );
function matreshka_first_order_admin_field( $user ) {
	if ( ! current_user_can( 'edit_users' ) ) return;
	$used = get_user_meta( $user->ID, '_matreshka_first_order_used', true );
	?>
	<h3>Скидка на первый заказ</h3>
	<table class="form-table">
		<tr>
			<th>Статус</th>
			<td>
				<?php if ( $used ) : ?>
					<span style="color:red;font-weight:bold;">Использована</span>
					<em>(<?php echo date( 'd.m.Y H:i', $used ); ?>)</em>
					<br><br>
					<label>
						<input type="checkbox" name="matreshka_reset_first_order" value="1">
						Сбросить (для тестирования)
					</label>
				<?php else : ?>
					<span style="color:green;font-weight:bold;">Доступна</span>
				<?php endif; ?>
			</td>
		</tr>
	</table>
	<?php
}

add_action( 'personal_options_update', 'matreshka_first_order_admin_save' );
add_action( 'edit_user_profile_update', 'matreshka_first_order_admin_save' );
function matreshka_first_order_admin_save( $user_id ) {
	if ( ! current_user_can( 'edit_users' ) ) return;
	if ( isset( $_POST['matreshka_reset_first_order'] ) ) {
		delete_user_meta( $user_id, '_matreshka_first_order_used' );
	}
}

// === УВЕДОМЛЕНИЕ В КОРЗИНЕ ===

add_action( 'woocommerce_before_cart', 'matreshka_first_order_cart_notice' );
function matreshka_first_order_cart_notice() {
	if ( ! is_user_logged_in() ) return;
	if ( ! matreshka_first_order_discount_available() ) return;

	$subtotal = WC()->cart->get_subtotal();
	if ( $subtotal < 1500 ) {
		$diff = 1500 - $subtotal;
		wc_print_notice(
			sprintf( 'Добавьте товаров ещё на <strong>%s ₽</strong> и получите скидку <strong>500 ₽</strong> на первый заказ!', number_format( $diff, 0, '', ' ' ) ),
			'notice',
			[ 'notice_class' => 'first-order-notice' ]
		);
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit