Php Woocommerce中的自定义删除购物车项目功能
我有添加项功能设置,如下所示:Php Woocommerce中的自定义删除购物车项目功能,php,wordpress,woocommerce,cart,hook-woocommerce,Php,Wordpress,Woocommerce,Cart,Hook Woocommerce,我有添加项功能设置,如下所示: <div class="col s4"> <a class="addtocart_link" href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" title="<?php echo esc_attr( $product->add_to_cart_text() ); ?>"
<div class="col s4">
<a class="addtocart_link" href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" title="<?php echo esc_attr( $product->add_to_cart_text() ); ?>">
<span class="action_box fa fa-plus"></span>
</a>
</div>
是否有类似或其他方法来创建删除项目功能?是的,有
wc\u get\u cart\u remove\u url
,但您需要获取购物车项目密钥才能执行此操作,您可以使用以下方法:
<?php
$cart_item_key = WC()->cart->generate_cart_id( $product->get_ID() );
$in_cart = WC()->cart->find_product_in_cart( $cart_item_key );
if ( $in_cart ) {
$cart_item_remove_url = wc_get_cart_remove_url( $cart_item_key );
?>
<div class="col s4">
<a class="remove_from_cart" href="<?php echo esc_url( $cart_item_remove_url ); ?>" title="remove_from_cart ">
<span class=" action_box fa fa-minus "></span></a>
</div>
<?php
}
将上面的代码放到functions.php
中,就可以开始了
以上所有代码都经过测试,运行良好 是的,有
wc\u get\u cart\u remove\u url
但您需要获取购物车项目密钥,以便使用以下方法:
<?php
$cart_item_key = WC()->cart->generate_cart_id( $product->get_ID() );
$in_cart = WC()->cart->find_product_in_cart( $cart_item_key );
if ( $in_cart ) {
$cart_item_remove_url = wc_get_cart_remove_url( $cart_item_key );
?>
<div class="col s4">
<a class="remove_from_cart" href="<?php echo esc_url( $cart_item_remove_url ); ?>" title="remove_from_cart ">
<span class=" action_box fa fa-minus "></span></a>
</div>
<?php
}
将上面的代码放到functions.php
中,就可以开始了
以上所有代码都经过测试,运行良好 很好用,谢谢!但是,如果购物车里有一种数量为5的产品,它会把所有5个都拿走,而不仅仅是1个。@LukasHillebrand今天我离开办公室,明天我会弄清楚的。我相信这是可能的!让我给你买一杯啤酒/咖啡以寻求帮助:-)@LukasHillebrand当然会很高兴:),我开始处理这个请求我有一个问题你在存档页面上显示这两个按钮对吗?在产品单页面上:工作正常,谢谢!但是,如果购物车里有一种数量为5的产品,它会把所有5个都拿走,而不仅仅是1个。@LukasHillebrand今天我离开办公室,明天我会弄清楚的。我相信这是可能的!让我给你买一杯啤酒/咖啡以寻求帮助:-)@LukasHillebrand当然会很高兴:),我开始处理这个请求我有一个问题你在存档页面中显示这两个按钮对吗?在产品单页面上:
add_action( 'wp_footer', 'change_qty_script' );
function change_qty_script() {
?>
<script>
jQuery(document).ready(function ($) {
$('#remove_one_item').click(function () {
var current_qty = parseInt($(this).attr('data-in-cart-qty'));
var id = $(this).attr('data-product-id');
var cat_item_key = $(this).attr('data-cart-item-key');
var data = {
product_id: id,
quantity: current_qty - 1,
cat_item_key : cat_item_key
};
var url = wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'update_qty');
$.post(url, data, function (response) {
if (!response) {
return;
}
if (response) {
location.reload();
}
});
});
$('#add_to_cart').click(function () {
var id = $(this).attr('data-product-id');
var cat_item_key = $(this).attr('data-cart-item-key');
var data = {
product_id: id,
quantity: 1,
};
var url = wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'add_to_cart');
$.post(url, data, function (response) {
if (!response) {
return;
}
if (response) {
location.reload();
}
});
});
});
</script>
<?php
}
add_action( 'wc_ajax_update_qty', 'update_qty' );
function update_qty() {
ob_start();
$product_id = absint( $_POST['product_id'] );
$product = wc_get_product( $product_id );
$quantity = $_POST['quantity'];
$cat_item_key = $_POST['cat_item_key'];
WC()->cart->set_quantity( $cat_item_key, $quantity, true );
wp_send_json( 'done' );
}