在购物车中使用ajax在woocommerce中移除产品
发布时间:2020-12-16 01:34:55 所属栏目:百科 来源:网络整理
导读:我想使用ajax在woocommerce购物车中删除产品,而不点击链接. 如果您遇到这种功能,请帮助我们. add_action( 'wp_footer','add_js_to_wp_wcommerce');function add_js_to_wp_wcommerce(){ ? script type="text/javascript" jQuery('.remove-product').click(fun
我想使用ajax在woocommerce购物车中删除产品,而不点击链接.
如果您遇到这种功能,请帮助我们. add_action( 'wp_footer','add_js_to_wp_wcommerce'); function add_js_to_wp_wcommerce(){ ?> <script type="text/javascript"> jQuery('.remove-product').click(function(){ var product_id = jQuery(this).attr("data-product_id"); jQuery.ajax({ type: 'POST',dataType: 'json',url: "/wp-admin/admin-ajax.php",data: { action: "product_remove",product_id: product_id },success: function(data){ console.log(data); } }); return false; }); </script> <?php } add_action( 'wp_ajax_product_remove','product_remove' ); add_action( 'wp_ajax_nopriv_product_remove','product_remove' ); function product_remove() { global $wpdb,$woocommerce; session_start(); foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item){ if($cart_item['product_id'] == $_POST['product_id'] ){ // Remove product in the cart using cart_item_key. $woocommerce->cart->get_remove_url($cart_item_key); } } print_r($woocommerce->cart->get_cart()); //echo json_encode(array('status' => 0)); exit(); }
您可以使用WC_Cart set_quantity方法
并在您的php中做这个: $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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |