【问题标题】:Jquery trigger for remove product from woocommerce cart ajax用于从 woocommerce 购物车 ajax 中删除产品的 Jquery 触发器
【发布时间】:2018-01-01 15:27:23
【问题描述】:

希望在 woocommerce 中从购物车中删除产品时运行一些 jQuery。我认为这会做到这一点,但它没有发生。

有什么想法吗?

jQuery( document.body ).on( 'updated_wc_div', function(){
//magic
});

【问题讨论】:

标签: jquery wordpress woocommerce


【解决方案1】:

购物车更新时也可以触发updated_cart_totals 事件。因此,您可以使用以下解决方法,因为在更新购物车时不会触发一个全局事件:

jQuery( document.body ).on( 'updated_wc_div', do_magic );
jQuery( document.body ).on( 'updated_cart_totals', do_magic );

function do_magic() {
    // do magic
}

【讨论】:

    【解决方案2】:

    您可以使用此代码从购物车中删除产品:

    main.js

     $.ajax({
        type: "POST",
        url: 'http://localhost/your_site/wp-admin/admin-ajax.php',
        data: {action : 'remove_item_from_cart','product_id' : '4'},
        success: function (res) {
            if (res) {
                alert('Removed Successfully');
            }
        }
    });
    

    functions.php

    function remove_item_from_cart() {
    $cart = WC()->instance()->cart;
    $id = $_POST['product_id'];
    $cart_id = $cart->generate_cart_id($id);
    $cart_item_id = $cart->find_product_in_cart($cart_id);
    
    if($cart_item_id){
       $cart->set_quantity($cart_item_id, 0);
       return true;
    } 
    return false;
    }
    
    add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
    add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');
    

    【讨论】:

    • 问题是触发 jQuery 动作,当任何物品被移除 从购物车中不是如何从购物车中移除物品 .
    【解决方案3】:

    在你的js中给出这组代码 文件

    jQuery(document.body)
      .on(
        'removed_from_cart updated_cart_totals',
        function() {
          // your code...
        }
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 2017-01-10
      • 2018-08-22
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      相关资源
      最近更新 更多