Created plugin, which showing form on shopping cart page after submit i am sending value to third party server.
jQuery.ajax({
type: 'POST',
url: 'http://www.yoursitename.com/wp-content/plugin/my-ajax.php',
How can i call woocommerce data on 'my-ajax.php'?
$subtotal = $woocommerce->cart->subtotal;
$shipping = $woocommerce->cart->shipping_total;
$orderTotal = $woocommerce->cart->total;
Above value i want to first call on my.ajax.php and then redirect page to shopping cart with some message etc.
Please help me i am really stuck here.
You'll need to start by using the $woocommerce global! You're on the right track.
function () {
global $woocommerce;
$subtotal = $woocommerce->cart->subtotal;
$shipping = $woocommerce->cart->shipping_total;
$orderTotal = $woocommerce->cart->total;
}
Related
I have been trying to update the cart drawer using my upsell product button. I have added product variant add to cart button in the cart drawer liquid code, which is using ajax cart to add the item to cart drawer but i am not sure what to put in success of the ajax request i am making.
here's my example code, please let me know what should I put in the success so it refreshes the cart drawer contents upon success. website address is: https://sailpak.myshopify.com/
$('#cart-btn-2').click(function(){
addItemToCart2( 41162793713847, 1);
});
function addItemToCart2(variant_id, qty) {
jQuery.ajax({
type: 'POST',
url: '/cart/add.js',
data: {
id: variant_id,
quantity: qty
},
dataType: 'json',
success: // what to put here so cart contents refresh with newly added item
})
}
thanks
Some premium themes have a refresh cart object that you can call to, but most themes don't.
Try to check your theme's script if it has one, if not, then you have to get content from the cart using their API and re-render contents to the DOM.
Display buy now button on product page which redirects to checkout page. Cart is automatically empty. I Want just that product in the checkout cart on which "Buy Now" button is clicked.
function check()
{
console.log("hi");
$( ".dropdown-menu .previewCart .previewCartItem .mini-cart-item-actions .cart-remove" ).each(function() {
console.log("product-id",$(this).attr('data-cart-itemid'));
$.ajax({url: "/cart.php?action=remove&item="+$(this).attr('data-cart-itemid'),async: false, success: function(result){
console.log("success");
}});
});
});
}
It looks like you are calling the URL to remove a product from the cart (/cart.php?action=remove), which would explain why the cart is empty. I'd recommend using the Add-to-Cart URLs documented here:
https://developer.bigcommerce.com/api-docs/cart-and-checkout/add-to-cart-url#add-cart-url_add-specific-sku-cart
For example, this URL will add a specific URL to the cart and redirect to the cart page:
/cart.php?action=add&sku=INSERT-SKU-HERE
I'm trying to make a custom page in the adminpanel of Prestashop where the shopowner can fill in his upcoming events that will appear in a column in the header.tpl page. The templates and controller are working so far, with a structure based on an answer here at Stack Overflow:
How to create a new page in prestashop admin panel?
Now I have made in the content.tpl (with the added custom JavaScript and CSS files) the form with the input fields. The next step is to send it to the controller to save it in the database. But I'm stuck this part. I can't find how I can nicely submit the form to the controller. First I tried it with an Ajax function but I couldn't find the right way. Also without Ajax no success.
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType : "json",
data:{
processEvents: true,
ajax: 'true',
controller: 'AdminEvents',
token: static_token
},
//success: function(jsonData){
//}
});
This is an example of an Ajax function that I tried. My questions:
How does other tpl or js files receive the baseUri, where is that
variable set?
What is the function of the ?rand date and time in that line? A kind
of security token?
What is the url of the controller? Also the url when I use
I guess the processEvents : true and Ajax : true is for security
reasons and to check if the form is submitted by Ajax or not?
Why is it necessary to send the controller name?
Where does the token come from?
Questions about the controller:
Which (Prestashop default functions) can or do need to use? For
example:
if (Tools::isSubmit('name')){
etc.
if (Tools::getValue('create_account')){
etc.
Can I use that functions anywhere or maybe only in an Init function?
A lot of questions, feel free to answer only a part of it, I just need a good push in the right direction, searching and reading in the online documentation and on the internet doesn't brought me the solution and brainwashed me a little.
EDIT:
I made a little progress by myself:
Where does the token come from?
What is the url of the controller? Also the url when I use
With the tools getAdminTokenLite and the controller name I generated the controller url:
$token = '?controller=AdminEvents&token='.Tools::getAdminTokenLite('AdminEvents');
The url to post to is the token plus the domain, admin directory and index.php.
With the tool getValue I get the POST data like in PHP with $_POST["name"].
Tools::getValue('event_name')
So its working but I guess it can be better with other Presta default tools.
I know that it's very late to answer you, but for sure it will help other mates with same problem.
Here is an example about how to implement ajax calls in Prestashop 1.6 on Admin panel using ANY Controller from BackOffice (if you want also, you can use ajax.php controller, but I'm using for this AdminImportController() )
tpl part:
$('#mybtn').click(function(e) {
var data = $('#datalist').val();
// Ajax call with secure token
$.post( "{$current|escape:'html':'UTF-8'}&token= {$token|escape:'html':'UTF-8'}",
{ ajax: true, action: "MyFunction", mydata: data } );
});
And in admin controller side:
public function ajaxProcessMyFunction()
{
// Get param
$mydata = (int)Tools::getValue('mydata');
$answer = 0;
if( $mydata > 0 ) {
$this->importProfList = Db::getInstance()->executeS(
"SELECT * FROM .... LIMIT 1"
);
...
$answer = $someOperationResult;
}
// Response
die(Tools::jsonEncode(array(
'answer' => htmlspecialchars($answer)
)));
}
Tested and working like a charm.
Regards
I'm developing e-commerce project using codeigniter..i'm new to codeigniter.now i'm working shopping cart module..i want create a shopping cart popup window in codeigniter..i have used bootstrap modal window but i couldn't able to pass the product values so i didn't view cart details.how to pass values to jquery pop-up window for shopping cart.whenever i click the add to cart button the values to be stored in cart table and also display values in jquery pop-up window.i have spent more time but i can't get proper solution..
Call a ajax function on onclick like this :-
<a value-id='1' href='#myModal' class='marker' title='Edit' onclick="functionname(passifvalueisdynamichere)">click on me</a>
Now if required get value from server side then you can call like :-
function functionname(id) {
xmlhttp = getobject();
var query = "id="+id+"&action=setdraftMessage";
var base_url = document.getElementById("baseurlval").value;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var newArray = new Array();
////get value from server side///////////////
var result = xmlhttp.responseText;
///setvalue like///
document.getElementById("mailid").value =newArray[0];
}
};
xmlhttp.open("GET", base_url + "index.php/pass/index?type=setdraftMessage&"+ query, true);
xmlhttp.send(null);
}
i am making a simple site in php. I have a product page and there is a link on product page that says add to wishlist so when a user clicks on that link the product is posted to the server and the page is redirected from the backend. but I want to do it using jquery ajax so that my page is not reloaded. can somebody please provide a snippet of code on how to do that ?
$('#anchorId').click(function(){
$.ajax({
url:"foo",
data : "the query string",
...
...
success: function(result){
// success code.
}
});
return false; // prevents the default behavior of anchor click.
});
The best way to learn jQuery, is to visit the API site. (Which seems to be down at the moment)
The ajax category
Update:
$('body').on('click', 'a.foo', function(){
// What you want here.
return false;
}
This will catch any click on anchors with the foo class under <body> no matter when they were created("runtime" or with the page load) .