I am getting 400 bad request when clicking the button from plugin. Is there any missing function? please advise on this.
jQuery.ajax({
type : "POST",
dataType : "json",
url : "<?php echo admin_url('admin-ajax.php'); ?>",
data : {action: "get_process_payment"},
success: function(response) {
alert("Your vote could not be added");
alert(response);
}
});
function get_process_payment(){
echo "test";
wp_die();
}
In WordPress,
wp_ajax_nopriv_(action) executes for users that are not logged in.
if you want it to fire on the front-end for both visitors and logged-in users, you can do this:
add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' );
So in your example add like this
add_action( 'wp_ajax_my_get_process_payment', 'get_process_payment' );
add_action( 'wp_ajax_nopriv_get_process_payment', 'get_process_payment' );
function get_process_payment(){
echo "test";
wp_die();
}
Please check https://codex.wordpress.org/AJAX_in_Plugins for more details
Related
This code below will send a BCC:
add_filter( 'woocommerce_email_headers', 'firefog_headers_filter_function', 10, 2);
function firefog_headers_filter_function( $headers, $object ) {
if ($object == 'new_order') {
$headers .= 'BCC: NAME <name#domain.com>' . "\r\n";
}
return $headers;
}
But how can i apply the below code to the BCC email?
<script type="application/json+trustpilot">
{
"recipientName": "Dynamic code...",
"recipientEmail": "Dynamic code...",
"referenceId": "Dynamic code...",
"locale": "Dynamic code..."
}
</script>
From the trustpilot documentation,
The structured data snippet needs to be included in the source code of
the transactional email that you’re using to trigger AFS.
It’s very important to note that the snippet must be implemented with
the dynamic placeholders used by your specific platform.
It is suggested to add the snippet to the source code of the email. So you can use WooCommerce email hooks to add the snippet.
add_action( 'woocommerce_email_after_order_table', 'add_trustpilot_snippet', 20, 2 );
function add_trustpilot_snippet( $order, $sent_to_admin ) {
?>
<script type="application/json+trustpilot">
{
"recipientEmail": "<?php echo $order->billing_email; ?>",
"recipientName": "<?php echo $order->billing_first_name.' '. $order->billing_last_name; ?>",
"referenceId": "<?php echo $order->get_order_number(); ?>",
}
</script>
}
This will give you an idea about how to implement the feature. Make use of $sent_to_admin variable if you need to target admin emails.
For more WooCommerce email hooks, refer the below link
https://www.businessbloomer.com/woocommerce-visual-hook-guide-emails/
I am creating a custom plugin in WordPress admin and I want to use ajax call inside plugin. My plugin has different folders. I have written following scripts but its giving me 400 (bad request) error. I use latest wordpress.
I tried to get answer from other post but not able to find what is the issue here. When I am moving the code to function.php everything working but I dont want to use function.php.
The code below has been written in single php file ( say dahboard.php)
<?php
add_action( 'admin_footer', 'ajax_without_file' );
function ajax_without_file() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var dataVariable = {
'action': 'my_action_without_file', // your action name
'variable_name': "Some value" // some additional data to send
};
jQuery.ajax({
url: ajaxurl, // this will point to admin-ajax.php
type: 'POST',
data: dataVariable,
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
}
add_action ('wp_ajax_my_action_without_file' , 'my_action_without_file');
add_action('wp_ajax_nopriv_my_action_without_file','my_action_without_file');
function my_action_without_file(){
echo json_encode($_POST);
wp_die();
}
?>
Try this. I've changed "ajaxurl" to <?php echo admin_url( 'admin-ajax.php' ); ?>
<?php
add_action( 'admin_footer', 'ajax_without_file' );
function ajax_without_file() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var dataVariable = {
'action': 'my_action_without_file', // your action name
'variable_name': "Some value" // some additional data to send
};
jQuery.ajax({
url: <?php echo admin_url( 'admin-ajax.php' ); ?>, // this will point to admin-ajax.php
type: 'POST',
data: dataVariable,
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
}
add_action ('wp_ajax_my_action_without_file' , 'my_action_without_file');
add_action('wp_ajax_nopriv_my_action_without_file','my_action_without_file');
function my_action_without_file(){
echo json_encode($_POST);
wp_die();
}
?>
How i can use Ajax in checkout page in woocommerce. I am trying to add custom pricing just before Payment in Order Review Page.
I have a check box when user will select the checkbox and pricing should be add in order.
Here is my code
function woocommerce_update_order_review() {
global $woocommerce;
$prc = strip_tags($woocommerce->cart->get_cart_total());
$priceValue = str_replace(",","",str_replace("#36;","",substr($prc,1)));
if($priceValue<=300 && $priceValue>200){
$rate_cost = 2.70;
}
else if($priceValue>300){
$extraDvalue = 2.70;
$extraVal = ceil(round(($priceValue-300),0)/100)*.90;
$newPr = $extraDvalue+$extraVal;
$rate_cost = $newPr;
}
if($priceValue>100){
$woocommerce->cart->add_fee( 'Declared value', $rate_cost, false, '' );
}
exit;
}
add_action('wp_ajax_woocommerce_update_order_review','woocommerce_update_order_review');
add_action('wp_ajax_nopriv_woocommerce_update_order_review','woocommerce_update_order_review');
JS CODE
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
// This does the ajax request
jQuery.ajax({
type: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: 'woocommerce_update_order_review', check: 'check'
}
}).done(function(value) {
alert(value);
});
Please suggest is this possible or not ?
Thanks
I am trying to pass variable when user click on link to page single.php
Here is my code
<a href='#' id='8' class='clickme' style='font-weight:bold;color:white;font-size:16px;'>";
In function.php, I put this code:
/////////////////////////////////////////////////////////
add_action( 'wp_head', 'myajaxcall' );
function myajaxcall(){ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery(".clickme").click( function() {
// We'll pass this variable to the PHP function example_ajax_request
var id = $('.clickme').attr('id');
// This does the ajax request
$.ajax({
url: <?php echo admin_url( 'admin-ajax.php'); ?>,
data: {
'action':'get_sidebar_request',
'page_id' : id
},
// beforeSend: function (jqXHR, settings)
// {
// alert( url = settings.url);
//},
success:function(data){
alert(data);
},
error: function(errorThrown){
//alert(errorThrown);
}
});
});
});
</script>
<?php }
add_action( 'wp_ajax_get_sidebar_request', 'idAjax' );
add_action( 'wp_ajax_nopriv_get_sidebar_request', 'idAjax' );
function idAjax()
{
require( get_template_directory() . '/single.php' );
}
/////////////////////////////////////////////////////////////////////
And in file `single.php, when I want to get the passed variable, I put this code:
$page_id=0;
$page_id = $_REQUEST['page_id'];
echo "page_id=".$page_id;
But the variable isn't passing to single.php. I try to echo it, and nothing happens.
Can you help me?
You can use Network tool in Google Chrome to debug and see sent/received data.
If "id" is not null when you send it, try to use $_POST to output your data in your php file, like this :
var_dump($_POST);
check this :
$.ajax({
url: <?php echo admin_url( 'admin-ajax.php'); ?>,
data: {
'action':'get_sidebar_request',
'page_id' : id
},
and add double quote to url value
$.ajax({
url: "<?php echo admin_url( 'admin-ajax.php'); ?>",
data: {
'action':'get_sidebar_request',
'page_id' : id
},
if you don't see your ajax call, it's a javascript error in your code.
Do you have any error in browser Console ?
I have a function in functions.php file that makes json request. This is how I am calling this function:
define('IS_AJAX_REQUEST', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if (IS_AJAX_REQUEST){
loadmore();
}
Above code works just fine, but the problem is it conflicts with other AJAX request, for example, I see error message saying cannot execute loadmore() when I try to upload an image.
I tried following but none worked.
if (IS_AJAX_REQUEST && is_page()){
loadmore();
}
+
if (is_page()){
if (IS_AJAX_REQUEST){
loadmore();
}
}
+
if (IS_AJAX_REQUEST){
if (is_page()) loadmore(); }
}
And here is the loadmore function
function loadmore(){
header('Content-type: application/json');
echo json_encode (array ('rsp'=>'ok',
'payload'=> DrawPostList($_GET['tribe'], $_GET['offset']),
'lionly'=> true,
'tribe'=> $_GET['tribe'],
'offset'=> $_GET['offset']+20));
exit;
}
I only wanna execute loadmore() function when viewing a page. How can I do that ?
<?php
add_action( 'wp_head', 'my_action_javascript' );
function my_action_javascript() {
is_page() { // load script only on pages
?>
<script type="text/javascript" >
function loadmore() {
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery.ajax({
type: "POST",
url: ajaxurl,
data: { action : 'loadmore_trigger', var1 : 'test', var2 : 'one more' }
})
}.done(function( response ) {
alert(response);
}
</script>
<?php
}
}
add_action('wp_ajax_nopriv_loadmore_trigger', 'loadmore_trigger');
add_action('wp_ajax_loadmore_trigger', 'loadmore_trigger');
function loadmore_trigger(){
echo "<pre>";
print_r($_POST);
exit;
}
?>
<input type="submit" onclick="loadmore(); return false;"> // make ajax call on click of submit button