javascript - WooCommerce - 购物车小部件 - 删除时淡出项目
问题描述
我第一次在一个项目中使用 WooCommerce。我刚刚开始学习 JS,之前从未使用过 AJAX……
现在我的标题中有一个购物车小部件,如果我删除或添加项目,它会更新总数等。我正在使用<?php woocommerce_mini_cart() ?>
这个。
如果物品从购物车小部件中删除,它们就会消失。我想要的是这样的:
- 用户点击删除链接
- 将被移除的产品会慢慢淡出
- 如果购物车小部件中有更多商品,则这些商品会向上移动(使用过渡)。
- 如果购物车中没有更多商品,“您的购物车是空的”消息应该会慢慢淡出。
- 总计数应淡出并以新价格再次淡入
我在我的 mini-cart.php 中使用 WooCommerce 的 woocommerce_cart_item_remove_link,但更改class="remove remove_from_cart_button"
为添加我自己的脚本。
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'<a href="%s" class="sidecart-item-remove" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s">Aus dem Warenkorb entfernen</a>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
我在迷你购物车中的产品包装在这个:
<div id="sidecart-item-id-<?php echo esc_attr( $cart_item_key ); ?>" class="sidecart-item sidecart-item-<?php echo esc_attr( $product_id ); ?>" product-id="<?php echo esc_attr( $product_id ); ?>">
<div class="sidecart-item-wrapper sidecart-item-wrapper-<?php echo esc_attr( $cart_item_key ); ?>" data-cart_item_key="<?php echo esc_attr( $cart_item_key ); ?>">
到目前为止我做了什么:
/**
* Item removed from sidecart
*/
$(document).on( 'click', '.sidecart-item-remove', function(e) {
$.fade_out_subtotal();
e.preventDefault();
var cart_item_key = $(this).attr( 'data-cart_item_key' );
setTimeout(function(){
// Need a (AJAX?) function here to remove product
$('#sidecart-item-id-'+cart_item_key).fadeOut(1000);
$.fade_in_subtotal();
},500);
console.log('Item removed from sidecart');
});
/**
* Fade in/out subtotal in sidecart
*/
$.fade_out_subtotal = function(){
$('.sidecart-subtotal-amount').fadeOut(1000);
}
$.fade_in_subtotal = function(){
setTimeout(function(){
$('.sidecart-subtotal-amount').fadeIn(1000);
},1000);
}
发生了什么:小计金额淡入淡出,但缺少更新金额的功能。sidecart-item-id-* 的 div 淡出并被移除。好吧,它并没有真正被删除。如果我刷新网站。产品又回到了购物车中。cart_item_key
错了吗?我在哪里可以找到 WooCommerce 用于删除产品的功能?
有人可以给我一个关于如何实现这一目标的提示。提前致谢。
解决方案
推荐阅读
- android - View=DecorView@47b2334[Connecting] 未附加到窗口管理器
- c# - 通过反射修改DispatcherObject中的_dispatcher字段来跨线程修改DispatcherObject对象的属性?
- python - 使用 Matplotlib 绘制二元高斯曲线
- java - 为什么“page-break-inside:avoid”在 PDF 中分页后添加元素与其他元素重叠?
- java - Java如何从嵌套数组列表中删除项目
- javascript - 在链接点击时触发一个功能(似乎有效,但不是在弹出窗口中)
- mysql - 如何在 MySQL 中按 json 对象排序
- python - 如何将 tkinter 类作为对象或在按钮单击中调用?
- unity3d - 如何放大和缩小并保持纵横比?
- c - 如何为open编写一个包装函数,它接受可变数量的参数?