简体   繁体   English

如何启用 woocommerce cart.php 中的更新购物车按钮?

[英]How to enable update cart button in woocommerce cart.php?

Hi I've been trying to get my "update cart" button to work on the cart page made by woocommerce. It is on my own theme and I have added the cart.php template to it.您好,我一直在尝试让我的“更新购物车”按钮在 woocommerce 制作的购物车页面上工作。这是我自己的主题,我已经向其中添加了 cart.php 模板。 In the woocommerce template it had quantity but it didn't have an easy to use add more or less buttons.在 woocommerce 模板中,它有数量,但没有易于使用的添加更多或更少按钮。 So I edited the global/quantity-input.php to have this trait.所以我编辑了 global/quantity-input.php 以具有此特征。 I have tested the single-products pages to use my new quantity and adding to cart.我已经测试了单一产品页面以使用我的新数量并添加到购物车。 This works great.这很好用。 But on the cart page the update cart button is disabled unless a change is made, and it doesn't recognize my changes.但是在购物车页面上,除非进行更改,否则更新购物车按钮将被禁用,并且它无法识别我的更改。 在此处输入图像描述

Things I've tried that worked:我尝试过的有效方法:

  • When I manually go into the inspector and remove the "disable" attribute from the button当我手动 go 进入检查器并从按钮中删除“禁用”属性时
  • When I press "enter" on my keyboard in the quantity input after i press the "-" or "+" button.当我按下“-”或“+”按钮后,在数量输入中按下键盘上的“enter”时。
  • Plus and minus do change the quantity in the input field加号和减号确实会改变输入字段中的数量
  • Typing in the quantity enables the update cart button输入数量可启用更新购物车按钮

Things I've tried that did not work:我试过但没有用的东西:

  • Using javascript to target the button and do 'update_cart.disable = false;'使用 javascript 定位按钮并执行“update_cart.disable = false;”
  • Tried calling a submit on change to automatically adjust the cart尝试调用更改提交以自动调整购物车
  • Just simply hope that the form recognizes the quantity change via my buttons只是希望表格通过我的按钮识别数量变化

My code for the Javascript我的代码为 Javascript

   function minus_quantity(e) {
        var this_input = this.parentNode.querySelector('input[type=number]');
        var current_val = this_input.value;
        var new_val = parseInt(current_val) - 1;
        this_input.value = new_val;
        document.getElementsByName('update_cart').disable = false;
        e.preventDefault();
    }
    function add_quantity(e) {
        var current_val = this.parentNode.querySelector('input[type=number]').value;
        var new_val = parseInt(current_val) + 1;
        this.parentNode.querySelector('input[type=number]').value = new_val;
        document.getElementsByName('update_cart').disable = false;
        e.preventDefault();
    }

My code inside cart.php我在 cart.php 中的代码

                            <button type="submit" class="button" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'woocommerce' ); ?>"><?php esc_html_e( 'Update cart', 'woocommerce' ); ?></button>
                            <?php do_action( 'woocommerce_cart_actions' ); ?>
                            <?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>

My HTML code inside the browser我在浏览器中的 HTML 代码

<button type="submit" class="button" name="update_cart" value="Update cart" disabled="">Update cart</button>
<input type="hidden" id="woocommerce-cart-nonce" name="woocommerce-cart-nonce" value="6090857223">
<input type="hidden" name="_wp_http_referer" value="/cart/?purge_success=1&amp;cache_type=all"><!--                        </div>-->

I should mention that my selector on the button isn't working and I don't know why.我应该提到我的按钮上的选择器不工作,我不知道为什么。

Also, I am very new to woocommerce and can't find anything on this topic.另外,我对 woocommerce 很陌生,找不到关于这个主题的任何内容。 Thank you for reading this.谢谢您阅读此篇。

UPDATE 1:更新 1:

I found this ticket and tried it Woocommerce 2.6.2 adds disabled attribute to update cart button我找到了这张票并试了一下Woocommerce 2.6.2 adds disabled attribute to update cart button

  • I added this to the bottom of my javascript function ( I did make sure it's in a Jquery wrapper btw) $('button[name="update_cart"]' ).removeProp( 'disabled');我将它添加到我的 javascript function 的底部(顺便说一句,我确保它在 Jquery 包装器中) $('button[name="update_cart"]' ).removeProp( 'disabled'); However this did not work either.但是,这也不起作用。 I used the inspector and it appears to not get anything back with the selector, but when I console log it, I get something back.我使用了检查器,它似乎没有用选择器返回任何东西,但是当我控制台记录它时,我得到了一些东西。

The reason why your javascript isn't working is because of two reasons:您的 javascript 不工作的原因有两个:

  1. The attribute you need to change is not "disable", it should be "disabled".您需要更改的属性不是“禁用”,而是“禁用”。

  2. You are selecting your button from the DOM using getElementsByName, which actually returns an array of all the elements with that name in your page.您正在使用 getElementsByName 从 DOM 中选择按钮,这实际上返回页面中具有该名称的所有元素的数组。 The way you would access it and change the disabled attribute (assuming you only have one element with that name) would be like this:您访问它并更改 disabled 属性的方式(假设您只有一个具有该名称的元素)如下所示:

    document.getElementsByName('update_cart')[0].disabled = false; document.getElementsByName('update_cart')[0].disabled = false;

However that is bad practice since you could have more than one element with that name and thus I would suggest giving your button an id and then using getElementById instead.然而,这是一种不好的做法,因为您可以拥有多个具有该名称的元素,因此我建议为您的按钮提供一个 ID,然后改用 getElementById。 You wouldn't need to add the [0] in this case.在这种情况下,您不需要添加 [0]。

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

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