gpt4 book ai didi

javascript - 在 WooCommerce 中 2 秒后自动关闭迷你购物车下拉菜单

转载 作者:行者123 更新时间:2023-11-29 23:29:56 26 4
gpt4 key购买 nike

在我的 woocommerce 商店中,link ,我已经更改了添加到购物车按钮,当用户点击添加到购物车按钮时,购物车下拉菜单从顶部下降,我怎么能在 2 秒后关闭它,除非被触摸,(flatsome 主题)有人可以帮我弄清楚我可以添加什么 js 或其他解决方案来使购物车下拉菜单在 2 秒后自行关闭?

如果有人想在添加到购物车旁边添加带 +/- 的数量框,这将显示此处的购物车下拉菜单是代码:enjoy。

    <script>
// JS
function addToCartLink(evt, pid) {
var x = jQuery("#quantity_" + pid);
var y = evt.closest("div");
var qty = y.getElementsByClassName("input-text")[0].value;

const addToCartUrl = '/?wc-ajax=add_to_cart';
var xWWWFormUrlencodedData = "quantity=" + qty;
xWWWFormUrlencodedData += "&product_id=" + pid;
jQuery.post(addToCartUrl, xWWWFormUrlencodedData, {
withCredentials: true,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Pragma': 'no-cache'
}
}).success(result => {
if (result.error) {
console.warn('The product has been added to the cart despite that the result object indicates an error!');
return;
}
console.log('Success.', result);
jQuery("div.widget_shopping_cart_content").replaceWith(result.fragments["div.widget_shopping_cart_content"]);
jQuery("span.mega-menu-woo-cart-total").replaceWith(result.fragments["span.mega-menu-woo-cart-total"]);
jQuery("span.mega-menu-woo-cart-count").replaceWith(result.fragments["span.mega-menu-woo-cart-count"]);
jQuery(".header .cart-icon").replaceWith(result.fragments[".header .cart-icon"]);
jQuery(".image-icon.header-cart-icon").replaceWith(result.fragments[".image-icon.header-cart-icon"]);
jQuery(".cart-price").replaceWith(result.fragments[".cart-price"]);
jQuery("li.cart-item.has-icon.has-dropdown").addClass("current-dropdown");



});

return false;

}
</script>

在 functions.php 中

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() && ! is_cart() ) {

$html = '<a rel="nofollow" data-product_id="'. $product->id .'" onclick="addToCartLink(this,' . $product->id .')" class="add_to_cart_button product_type_simple button primary is-flat mb-0 is-small">הוסף לסל</a>';
//$html = '<a onclick="addToCartLink(this,'. $product->id .')">הוסף לסל</a>';

$html .= '<div class="quantity buttons_added">';
$html .= '<input type="button" value="-" class="minus button is-form">';
$html .= '<input type="number" id="quantity_'. $product->id .'" class="input-text qty text" step="1" min="0" max="9999" name="quantity" value="1" title="כמות" size="4" pattern="[0-9]*" inputmode="numeric" >';
$html .= '<input type="button" value="+" class="plus button is-form">';
$html .= '</div>';

}
return $html;
}

最佳答案

你可以尝试这样的事情(没有保证,因为我无法测试它):

add_action( 'wp_footer', 'custom_jquery_script' );
function custom_jquery_script(){
?>
<script>
(function($){
$('body').on( 'added_to_cart', function(){
setTimeout( function(){
$('ul.nav.top-bar-nav > .cart-item').removeClass('current-dropdown');
}, 2000 );
});
})(jQuery);
</script>
<?php
}

解释:

当一个项目被添加到购物车时,类 current-dropdown 被添加到:

<li class="cart-item has-icon has-dropdown">

并将 CSS 应用于此子 html 元素以使其可见(购物车弹出内容):

<ul class="nav-dropdown nav-dropdown-default" style="">

因此,使用我的 jQuery 代码,我捕获了“body”委托(delegate)事件 added_to_cart 并使用 setTimeout() javascript 函数在 2 秒后删除了 current-dropdown(2000 毫秒)。删除类(class)时,它会自动隐藏迷你购物车内容弹出窗口......

这应该是正确的方法。


如果它不起作用,您可以尝试(2 个备选方案):

1) 用 'adding_to_cart' 事件替换代码中的 'adding_to_cart'
2) 将 $('body').on( 'added_to_cart', function(){ 替换为:

$('.add_to_cart_button').click( function(){

我希望它能奏效......

关于javascript - 在 WooCommerce 中 2 秒后自动关闭迷你购物车下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47702699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com