<input class="myclass" placeholder="Поиск" type="text" value="" name="s" id="search">
jQuery(function ($) {
$(document).on('keyup', '#search', function () {
var $val = $(this).val(),
$length = $val.length,
$data = {
action: 'wpp_search',
security: WppAjax.security,
string: $val
};
if ($length >= 3) {
$.post(WppAjax.ajax_url, $data, function ($response) {
if ($response.success) {
//$response.data.result - тут лежит результат поиска
} else {
// бла бла бла обработка ощшибок
}
});
}
});
})
add_action( 'wp_ajax_wpp_search', 'wpp_search' );
add_action( 'wp_ajax_nopriv_wpp_search', 'wpp_search' );
function wpp_search() {
check_ajax_referer( 'wpp-string', 'security' );
$string = sanitize_text_field( $_POST['string'] );
$out = '';
$errors = [];
if ( ! empty( $string ) ) {
$q = new WP_Query(
array(
's' => $string
)
);
if ( $q->have_posts() ) {
ob_start();
while ( $q->have_posts() ) : $q->the_post();
//тут стандартный вывод постов
endwhile;
$out .= ob_get_clean();
} else {
$out .= __( 'Не найдено результатов по запросу -', 'txt' ) . sprintf( '<b><i>%s</i></b>', $string );
}
} else {
$errors = __( 'Поисковый запрос пуст или не корректен', 'txt' );
}
if ( empty( $errors ) ) {
wp_send_json_success( array( 'result' => $out ) );
} else {
wp_send_json_error( array( 'result' => $errors ) );
}
}
add_action( 'wp_enqueue_scripts', 'wpp_assets' );
function wpp_assets() {
wp_enqueue_script( 'ajax', get_template_directory_uri() . '/assets/js/script.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'ajax', 'WppAjax', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'security' => wp_create_nonce( 'wpp-string' )
) );
}
if( esc_attr( $gateway->id ) !== 'тут ID метода хранящегося в мета поле оплаты для юзера' ) continue;
Может я не првильно объяснил... Имеется в виду, что например пользователь выбрал PayPal , при заказе у него будет отображатся вариант оплаты только PayPal , потом он например поменял на Карту , на следующий раз , при заказе , у него будет способо оплаты только Картой и тд.
add_action( 'wp_ajax_hello', 'f_hello' );
add_action( 'wp_ajax_nopriv_hello', 'f_hello' );
function f_hello() {
check_ajax_referer( 'wpp-string', 'security' );
$n = 1;
$sum = 0;
$error = [];
while ( $n <= 2 ) :
if ( ! empty( absint( $_POST['param1'] ) ) ) {
$sum += absint( $_POST[ 'param' . $n ] );
} else {
$error[] = sprintf( __( 'Param %d is Empty!', 'txt' ), $n );
}
$n ++;
endwhile;
if ( empty( $error ) ) {
wp_send_json_success( array( 'result' => $sum ) );
} else {
wp_send_json_error( array( 'result' => $error ) );
}
}
add_action( 'wp_enqueue_scripts', 'wpp_assets' );
function wpp_assets() {
wp_enqueue_script( 'ajax', get_template_directory_uri() . '/assets/js/script.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'ajax', 'WppAjax', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'security' => wp_create_nonce( 'wpp-string' )
) );
}
jQuery(function ($) {
$(document).on('click', '#btn', function (e) {
e.preventDefault();
var $param1 = 10,
$param2 = 20,
$data = {
action: 'hello',
security: WppAjax.security,
param1: $param1,
param2: $param2
};
$.post(WppAjax.ajax_url, $data, function ($response) {
if ($response.success) {
alert($response.data.result);
} else {
// бла бла бла обработка ощшибок
}
});
});
})