简体   繁体   English

WooCommerce AJAX 添加到购物车 - 仅限简单产品

[英]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.

相关问题 Ajax 加入购物车 WooCommerce 单品上的简单和可变产品 - Ajax Add to cart for simple and variable products on WooCommerce single products 禁用 AJAX 仅在私人产品上购物车 Woocommerce - Disable AJAX to cart on Private products only Woocommerce 在Woocommerce商店页面中更改用于简单产品的购物车按钮 - Change add-to-cart button for simple products in Woocommerce shop page 在 WooCommerce 简单产品的添加到购物车按钮中显示可用性 - Display availability in add to cart button for WooCommerce simple products WooCommerce AJAX放入购物车以获取可变产品,仍重定向到产品页面 - WooCommerce AJAX Add to Cart for Variable products, still redirecting to product page 在 Woocommerce 中的产品自定义循环上启用 Ajax 添加到购物车按钮 - Enable Ajax add to cart button on products custom loop in Woocommerce 在WooCommerce中仅允许从相同父类别的产品添加到购物车 - Only allow add to cart from products of the same parent category in WooCommerce WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart WooCommerce-如何使用具有相同购物车按钮的Ajax将多种不同的产品添加到购物车? - WooCommerce - How can I add multiple, different products to cart using ajax with same add-to-cart-Button? 如何仅使用ajax一次在购物车中添加产品包 - How to add products package in cart at once only using ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM