【问题标题】:WooCommerce Cart qty buttons only showing after reloadingWooCommerce 购物车数量按钮仅在重新加载后显示
【发布时间】:2016-08-15 19:00:50
【问题描述】:

我在我的子主题中创建了购物车模板的副本,并对购物车行为进行了一些自定义更改(CSS 侧购物车,如 hibr.com 中的)。我的 header.php 中有这些与购物车相关的 sn-ps:

<div class="close-me2"></div>
<?php echo do_shortcode('[woocommerce_cart]'); ?>




<div class="cart-outer" data-user-set-ocm="<?php echo $userSetSideWidgetArea; ?>">
        <div class="cart-menu-wrap">
            <div class="cart-menu">
                <a class="cart-contents" href="<?php //echo $woocommerce->cart->get_cart_url(); ?>"><div class="cart-icon-wrap"><i class="icon-salient-cart"></i> <div class="cart-wrap"><span><?php echo $woocommerce->cart->cart_contents_count; ?> </span></div> </div></a>
            </div>
        </div>



<div id="side-cart">
<div class="close-me2"></div>
<?php echo do_shortcode('[woocommerce_cart]'); ?>

<?php if ( is_active_sidebar( 'sidebar-4' ) ) : ?>
    <div class="widget-area">
        <?php dynamic_sidebar( 'sidebar-4' ); ?>
    </div>
<?php endif; ?>

不幸的是,似乎每当将产品添加到购物车时,都会显示“错误”的购物车,因为没有显示数量按钮“-+”。刷新页面后,购物车显示为带有数量按钮:http://screencast.com/t/gzkb6QPfz

我也安装了这个插件:wordpress.org/plugins/woocommerce-ajax-add-to-cart-for-variable-products/changelog/

这些是我添加到其中的自定义更改,因为我的边车需要被触发:

// Changes button classes
            $thisbutton.addClass( 'added' );

            var rightVal = 0; // base value
            $('#side-cart').animate({right: rightVal + 'px'}, {queue: false, duration: 500});

            $('.cart-outer a').attr("href", "javascript:void(0)");



            $('#side-cart .woocommerce').removeAttr('style');

            $('#side-cart .widget-area').removeAttr('style');


            $('.cart_empty3').attr('style', 'display:none !important;');

在我看来,将产品添加到购物车后某些脚本无法正确加载,但我不知道从哪里开始寻找错误。

如果您有任何想法,请告诉我。如果需要,我将分享该网站的链接,因为我目前正在运行 http auth。

谢谢!

【问题讨论】:

  • 尝试在 add_action() 和 add_filter() 函数中关注以 woocommerce_before_cart… 或 woocommerce_cart_… 或 woocommerce_after_cart… 开头的 woocommerce 钩子。你不在标题中使用迷你购物车?

标签: javascript php jquery wordpress woocommerce


【解决方案1】:

我已经解决了像这样覆盖quantity-input.php后的显示问题:

<div class="quantity buttons_added">
<input type="button" value="-" class="minus">
	<input type="number" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min_value ); ?>" max="<?php echo esc_attr( $max_value ); ?>" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" />
<input type="button" value="+" class="plus"></div>
</div>

并使用此 jquery 为 qty 按钮提供触发器:

jQuery(document).ready(function($){
    $('.quantity').on('click', '.plus', function(e) {
        $input = $(this).prev('input.qty');
        var val = parseInt($input.val());
        $input.val( val+1 ).change();
    });

    $('.quantity').on('click', '.minus', 
        function(e) {
        $input = $(this).next('input.qty');
        var val = parseInt($input.val());
        if (val > 0) {
            $input.val( val-1 ).change();
        } 
    });
});

但是现在问题仍然存在,将产品添加到购物车后,数量按钮“+”和“-”只有在重新加载页面后才能点击。

在我看来,jquery 没有正确加载。你对此有什么线索还是我的 jquery 错了?

谢谢!

【讨论】:

    【解决方案2】:

    更新:

    我已经通过像这样修改 jQuery 解决了这个问题:

    jQuery(document).on('click', '.quantity .plus', function(e) {
        $input = jQuery(this).prev('input.qty');
        var val = parseInt($input.val());
        $input.val(val + 1).change();
    }).on('click', '.quantity .minus', function(e) {
        $input = jQuery(this).next('input.qty');
        var val = parseInt($input.val());
        if(val > 0) {
            $input.val(val - 1).change();
        }
    });

    【讨论】:

    • 我按照这些步骤操作,确实“+”和“-”按钮在桌面设备上有效,但在移动设备上无效。知道是什么原因造成的吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-08
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多