[英]WooCommerce AJAX add to cart - simple products only
I have followed this how to guide for ajax add to cart for single products: https://aceplugins.com/ajax-add-to-cart-button-on-the-product-page-woocommerce/我遵循了如何指导 ajax 添加到购物车的单个产品: https://aceplugins.com/ajax-add-to-cart-button-on-the-product-page-woocommerce/
I have the php in my child themes function.php file and it works as it should.我的子主题 function.php 文件中有 php 文件,它可以正常工作。
The problem that i am having is that i only want to use remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
我遇到的问题是我只想使用
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
when on a simple product only so that it is available for other product types.仅在简单产品上时,以便它可用于其他产品类型。
I have tried adding it to the ace_ajax_add_to_cart_handler
function like so:我尝试将它添加到
ace_ajax_add_to_cart_handler
function 中,如下所示:
function ace_ajax_add_to_cart_handler() {
WC_Form_Handler::add_to_cart_action();
WC_AJAX::get_refreshed_fragments();
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
}
add_action( 'wc_ajax_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
add_action( 'wc_ajax_nopriv_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
I've also tried various functions with conditionals with no joy, eg:我还尝试了各种带有条件的功能,但没有任何乐趣,例如:
function remove_cart_action(){
if ( is_product() ) {
global $product;
if( $product->is_type( 'simple' ) ){
// Remove WC Core add to cart handler to prevent double-add
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
}
}
}
add_action( 'init', 'remove_cart_action', 20);
Any advice would be appreciated - thanks in advance任何建议将不胜感激 - 在此先感谢
I updated your code but not sure if it works You can check it!我更新了你的代码,但不确定它是否有效你可以检查一下!
function ace_ajax_add_to_cart_handler() {
WC_Form_Handler::add_to_cart_action();
WC_AJAX::get_refreshed_fragments();
$product_id = isset( $_REQUEST['add-to-cart'] ) ? intval( wp_unslash( $_REQUEST['add-to-cart'] ) ) : false;
if ( $product_id ) {
$product = wc_get_product( $product_id );
if ( $product->is_type( 'simple' ) ) {
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
}
}
}
add_action( 'wc_ajax_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
add_action( 'wc_ajax_nopriv_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.