【问题标题】:Close mini-cart dropdown automatically after 2 seconds in WooCommerce在 WooCommerce 中 2 秒后自动关闭迷你购物车下拉菜单
【发布时间】:2018-05-22 00:16:13
【问题描述】:

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

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

    <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;
}

【问题讨论】:

  • 您应该需要给我们一个实时网站链接(请编辑您的问题)...
  • 这里是链接link,添加一个产品到购物车,购物车会从顶部掉下来,如何添加一个2秒后关闭它的js?谢谢洛伊克:)

标签: javascript php jquery wordpress woocommerce


【解决方案1】:

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

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 应用于此 Child html 元素以使其可见(购物车弹出内容)

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

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

这应该是正确的方法。


如果不行,你可以试试(2个替代方案)

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

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

我希望它会起作用……

【讨论】:

  • 再次感谢您的帮助。不幸的是它没有用,我尝试了所有场景,有什么线索吗?谢谢
  • @itayko 所以知道问题可能是:你如何嵌入这个脚本?即使它现在根本不起作用(请记住,我无法对其进行真实测试),这个答案是否不值得被赞成,因为我的解释是真实的过程......
  • 我将这段代码嵌入到页眉中,我也在页脚中尝试过,它在检查模式下对你有用吗?购物车需要自动关闭而不用鼠标触摸,这可能吗? :(
  • 我支持你,你太棒了,不幸的是,它仍然无法正常工作:(
猜你喜欢
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 2016-12-29
  • 2020-04-05
  • 1970-01-01
相关资源
最近更新 更多