在我的woocommerce网站上,我更改了购物车页面,删除了“更新购物车”按钮并创建了2个按钮来添加和删除产品,就像我在此图片中显示的那样:
当我点击数量按钮时,如果按下按钮更新购物车,我想调用相同的功能 .
为此我使用ajax但它没有做任何事情 .
首先在我的 function.php
文件中我有这个:
function update_my_cart() {
// here update then cart
var_dump("execute");
}
add_action( 'wp_ajax_update_my_cart', 'update_my_cart' ); // If called from admin panel
add_action( 'wp_ajax_nopriv_update_my_cart', 'update_my_cart' );
add_action( 'wp_enqueue_scripts', 'rct_enqueue_scripts' );
if ( ! function_exists( 'rct_enqueue_scripts' ) ) :
function rct_enqueue_scripts() {
wp_enqueue_script( 'rct-js', get_template_directory_uri() . '/js/themeCoffee.js', array(), '1.0', true );
wp_localize_script('rct-js', 'ajax_object', array('ajax_url' => admin_url( 'admin-ajax.php' )));
}
endif;
在我的jquery文件中,我有这个:
updatecart = function(qty) {
var currentVal, data, item_hash, request;
currentVal = void 0;
data = void 0;
item_hash = void 0;
currentVal = parseFloat(qty);
request = $.ajax({
url: 'ajax_object.ajax_url',
method: 'POST',
data: {
quantity: currentVal,
action: 'update_my_cart'
},
dataType: 'html'
});
request.done(function(msg) {
alert('cart update ');
});
request.fail(function(jqXHR, textStatus) {
alert('Request failed: ' + textStatus);
});
};
我得到了这个错误:
Failed to load resource: the server responded with a status of 404 (Not Found)
因为我尝试加载 my_website/cart/ajax_object.ajax_url
.
提前致谢!
1 回答
你忘记了这个重要的过程:
然后你将检索“
ajaxurl
" and "cart_ajax
" in your javascript file in "url:
”:你的javascript函数不起作用 . Here are some functional examples 你需要做什么:
WooCommerce - auto update total price when quantity changed?
Wordpress passing ajax value to a specific page using Wordpress
Using AJAX With PHP on Your WordPress Site Without a Plugin
How to use Ajax with your WordPress Plugin or Theme?