在特定类别中禁用 ajax 添加到购物车按钮 woocommerce
Disable the ajax add to cart button woocommerce in specific categories
在 WooCommerce 中,我需要禁用添加到购物车按钮的 AJAX,但仅在“类别 x”中,才能使重定向工作并直接转到结帐:
https://jeroensormani.com/redirect-users-after-add-to-cart/
我尝试删除 class 但没有成功:
$(document).ready(function(){
$(".add_to_cart_button").removeClass("ajax_add_to_cart")
});
How do I add a simple jQuery script to WordPress?
已解决:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button($button, $product){
//list category slugs where button needs to be changed
$selected_cats = array('my-category');
//get current category object
$current_cat = get_queried_object();
//get category slug from category object
$current_cat_slug = $current_cat->slug;
//check if current category slug is in the selected category list
if( in_array($current_cat_slug, $selected_cats) ){
//replace default button code with custom code
$button = '<div class="add-to-cart-button"><a href="?add-to-cart='.$product->get_id().'" data-quantity="1" class="primary is-small mb-0 button product_type_simple add_to_cart_button is-flat" data-product_id="'.$product->get_id().'" data-product_sku="" aria-label="Add to cart" rel="nofollow">Buy now</a></div>';
}
return $button;
}
在 WooCommerce 中,我需要禁用添加到购物车按钮的 AJAX,但仅在“类别 x”中,才能使重定向工作并直接转到结帐: https://jeroensormani.com/redirect-users-after-add-to-cart/
我尝试删除 class 但没有成功:
$(document).ready(function(){
$(".add_to_cart_button").removeClass("ajax_add_to_cart")
});
How do I add a simple jQuery script to WordPress?
已解决:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button($button, $product){
//list category slugs where button needs to be changed
$selected_cats = array('my-category');
//get current category object
$current_cat = get_queried_object();
//get category slug from category object
$current_cat_slug = $current_cat->slug;
//check if current category slug is in the selected category list
if( in_array($current_cat_slug, $selected_cats) ){
//replace default button code with custom code
$button = '<div class="add-to-cart-button"><a href="?add-to-cart='.$product->get_id().'" data-quantity="1" class="primary is-small mb-0 button product_type_simple add_to_cart_button is-flat" data-product_id="'.$product->get_id().'" data-product_sku="" aria-label="Add to cart" rel="nofollow">Buy now</a></div>';
}
return $button;
}