admin管理员组文章数量:1410697
I'm trying to set up an AJAX function to clear my cart
HTML
<a onclick="clearCart(this)" data-href="/product-page/" data-productID="182">Go to Product</a>
JavaScript
function clearCart(d) {
jQuery(document).ready(function($) {
var productID = d.getAttribute("data-productID");
$.ajax({
url: "addtocart.php",
data: {productID: productID},
type: "post",
success: function(output) {
window.location = d.getAttribute("data-href");
//alert(output);
}
});
});
}
PHP
if(isset($_POST['productID']) && !empty($_POST['productID'])) {
global $woomerce;
$woomerce->cart->empty_cart();
//echo $_POST['productID'];
}
Result
- Internal Server Error caused by 3rd PHP line
- Alerting var output is working (check outmented code)
SOLUTION
I figured it out by myself and some great help from @MirzaP
JS
function clearCart(d) {
jQuery.post(
".php",
//ajaxurl,
{
"action": "clearcart",
"data": d.getAttribute("data-productid")
},
function(){
window.location = d.getAttribute("data-href");
}
);
}
PHP
add_action('wp_ajax_nopriv_clearcart',function(){
global $woomerce;
$woomerce->cart->empty_cart();
});
I'm trying to set up an AJAX function to clear my cart
HTML
<a onclick="clearCart(this)" data-href="/product-page/" data-productID="182">Go to Product</a>
JavaScript
function clearCart(d) {
jQuery(document).ready(function($) {
var productID = d.getAttribute("data-productID");
$.ajax({
url: "addtocart.php",
data: {productID: productID},
type: "post",
success: function(output) {
window.location = d.getAttribute("data-href");
//alert(output);
}
});
});
}
PHP
if(isset($_POST['productID']) && !empty($_POST['productID'])) {
global $woomerce;
$woomerce->cart->empty_cart();
//echo $_POST['productID'];
}
Result
- Internal Server Error caused by 3rd PHP line
- Alerting var output is working (check outmented code)
SOLUTION
I figured it out by myself and some great help from @MirzaP
JS
function clearCart(d) {
jQuery.post(
"https://dercampus.ch/wp-admin/admin-ajax.php",
//ajaxurl,
{
"action": "clearcart",
"data": d.getAttribute("data-productid")
},
function(){
window.location = d.getAttribute("data-href");
}
);
}
PHP
add_action('wp_ajax_nopriv_clearcart',function(){
global $woomerce;
$woomerce->cart->empty_cart();
});
Share
Improve this question
edited Oct 21, 2016 at 12:47
Kode Bryant
asked Oct 21, 2016 at 10:59
Kode BryantKode Bryant
5119 silver badges24 bronze badges
6
- When you set WP_DEBUG to true in your wp-config.php what is the internal error that gets shown? – MirzaP Commented Oct 21, 2016 at 11:03
- It doesn't show anything for this php file. Do I need to adjust anything as it is an AJAX call? The file is located in a subdirectory in my plugin – Kode Bryant Commented Oct 21, 2016 at 11:17
- Does it show anything in the browser console when you do right click -> Inspect Element -> Console (in chrome)? – MirzaP Commented Oct 21, 2016 at 11:26
- This is shown on click dercampus.ch/log.png – Kode Bryant Commented Oct 21, 2016 at 11:30
- When you set WP_DEBUG to true and then do the same is the error 500 expanded on? Or if with the flag set to true if you click on the addtocart.php url itself does anything happen? Also, you should really be using the admin-ajax.php file for ajax calls with WordPress. – MirzaP Commented Oct 21, 2016 at 11:51
2 Answers
Reset to default 2Please change your php code to
if(isset($_POST['data']) && !empty($_POST['data'])) {
global $woomerce;
$woomerce->cart->empty_cart();
//echo $_POST['productID'];
}
Your parameter that is passed in is data and not productID
// before addto cart, only allow 1 item in a cart
add_filter( 'woomerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
global $woomerce;
$woomerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}
本文标签: javascriptWoocommerce 160Clear cart by AjaxStack Overflow
版权声明:本文标题:javascript - Woocommerce | Clear cart by Ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745028087a2638325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论