简体   繁体   English

Woocommerce 购物车显示/隐藏优惠券功能

[英]Woocommerce Cart Show/Hide Coupon Functionality

Apologies for what is probably a fairly simple question, but I really hate javascript I've hit a wall here.抱歉,这可能是一个相当简单的问题,但我真的很讨厌 javascript 我在这里碰壁了。

Basically, I've been modifying the Woocommerce cart page into something more palatable, and I want to move the coupon section into the Basket Totals area so that it appears below the Subtotal and above the Shipping.基本上,我一直在将 Woocommerce 购物车页面修改为更可口的内容,并且我想将优惠券部分移动到篮子总计区域,使其显示在小计下方和运费上方。 I've got it in position easily enough, but I want it to only appear when it's clicked on, like this for mobile:我很容易在 position 中得到它,但我希望它只在点击它时出现,就像移动设备一样: 在手机上放置优惠券

The problem is I want it to be triggered whenever anywhere on the row is clicked, but setting the actual TD to activate it doesn't work and causes the error you can see in the image console.问题是我希望在单击行上的任何位置时触发它,但是将实际的 TD 设置为激活它不起作用,并导致您可以在图像控制台中看到错误。 On mobile this is the TD, on desktop it would be the TH, just to make things more complicated, as you can see from this screenshot and the following layout code:在移动设备上这是 TD,在桌面设备上是 TH,只是为了让事情变得更复杂,从这个屏幕截图和以下布局代码中可以看出: 优惠券放置在桌面上

<tr class="cart-subtotal">
        <th ><?php esc_html_e( 'Coupon', 'woocommerce' ); ?></th>
        <td data-title="<?php esc_attr_e( 'Coupon', 'woocommerce' ); ?>"><span class="accordion add-coupon">+</span>
            <!--<button class="accordion"></button>-->
            <div class="panel">
                <div class="coupon">
                <!--<label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label>-->
                <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> 
                <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>">
                    <?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>
                </button>
                <?php do_action( 'woocommerce_cart_coupon' ); ?>
            </div>
            </div>
        </td>
    </tr>

I've been using some Javascript to slide the panel up and down when it's isolated divs and buttons controlling it, but for the life of me I can't get it to play nicely using any part of the table as a trigger and I don't want to break the layout of the table by trying to work around it when it feels like I should be able to make this work.我一直在使用一些 Javascript 来上下滑动面板,当它是独立的 div 和控制它的按钮时,但对于我的生活,我无法让它很好地使用桌子的任何部分作为触发器,我不当我觉得我应该能够完成这项工作时,我不想通过尝试解决它来破坏表格的布局。 Here's the Javascript:这是 Javascript:

<script>
    var acc = document.getElementsByClassName("accordion");
    var i;

    for (i = 0; i < acc.length; i++) {
      acc[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.maxHeight) {
          panel.style.maxHeight = null;
        } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } 
    });
    }
    </script>

So basically, clicking on an element with the class "accordion" should cause an element with the class "panel" to open or close.所以基本上,单击带有 class “手风琴”的元素应该会导致带有 class “面板”的元素打开或关闭。

I've been fighting with this all day and I just can't get it to play nicely no matter what I try, so I'd really appreciate anyone's insights into how I can make this slide down when the whole row is clicked so the coupon function is only there if needed.我整天都在和这个作斗争,无论我怎么尝试,我都无法让它很好地发挥作用,所以我真的很感激任何人对我如何在点击整行时让这个幻灯片向下滑动的见解,所以优惠券 function 仅在需要时提供。

Many thanks.非常感谢。

UPDATE更新

Based on @vincenzo-di-gaetano's answer below I've got closer.根据下面@vincenzo-di-gaetano 的回答,我已经更接近了。 With the following setup I have made the entire row (so everything between the faint grey border lines) clickable and they will make the form disappear, but once it's gone it won't come back at present.通过以下设置,我使整行(因此在微弱的灰色边框线之间的所有内容)都可单击,它们将使表单消失,但一旦消失,目前就不会回来。

<tr class="subtotal-coupon">
        <th ><?php esc_html_e( 'Coupon', 'woocommerce' ); ?></th>
        <td data-title="<?php esc_attr_e( 'Coupon', 'woocommerce' ); ?>"><span class="add-coupon">+</span>
                <div class="coupon">
                <label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label>
                <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> 
                <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>">
                    <?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>
                </button>
                <?php do_action( 'woocommerce_cart_coupon' ); ?>
            </div>
        </td>
    </tr>

Slightly modified jQuery:稍作修改的jQuery:

add_action( 'wp_footer', 'show_hide_coupon_form' );
function show_hide_coupon_form() {
    ?>
    <script type="text/javascript">
        jQuery(function($){
            $('.cart_totals .subtotal-coupon td').click(function(){
                $('.cart_totals div.coupon').toggle();
                $(this).text( $(this).text() == '+' ? '-' : '+' );
            });
        });
    </script>
    <?php
}

What I don't understand is why it won't toggle and only disappear.我不明白的是为什么它不会切换而只会消失。

In case the HTML code is the following:如果 HTML 代码如下:

<tr class="subtotal-coupon">
    <th ><?php esc_html_e( 'Coupon', 'woocommerce' ); ?></th>
    <td data-title="<?php esc_attr_e( 'Coupon', 'woocommerce' ); ?>"><span class="add-coupon">+</span>
        <div class="coupon">
            <label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label>
            <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> 
            <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?></button>
            <?php do_action( 'woocommerce_cart_coupon' ); ?>
        </div>
    </td>
</tr>

And this is the CSS code to hide the coupon form by default:这是默认情况下隐藏优惠券表单的 CSS 代码:

.cart_totals .subtotal-coupon .coupon {
    display:none;
}

You can use the following jQuery script to show/hide the coupon form based on the click of the .cart_totals.subtotal-coupon td element:您可以使用以下jQuery 脚本根据.cart_totals.subtotal-coupon td元素的点击显示/隐藏优惠券表单:

add_action( 'wp_footer', 'show_hide_coupon_form' );
function show_hide_coupon_form() {
    ?>
    <script type="text/javascript">
        jQuery(function($){
            $('.cart_totals .subtotal-coupon td').click(function(){
                $('.cart_totals .subtotal-coupon .coupon').toggle();
                $('.cart_totals .subtotal-coupon .add-coupon').text( $(this).text() == '+' ? '-' : '+' );
            });
        });
    </script>
    <?php
}

The code has been tested and works.该代码已经过测试并且可以工作。 Add it to your active theme's functions.php.将其添加到您的活动主题的功能中。php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM