[英]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:我尝试过的有效方法:
Things I've tried that did not work:我试过但没有用的东西:
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&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
$('button[name="update_cart"]' ).removeProp( 'disabled');
$('button[name="update_cart"]' ).removeProp( 'disabled');
However this did not work either. The reason why your javascript isn't working is because of two reasons:您的 javascript 不工作的原因有两个:
The attribute you need to change is not "disable", it should be "disabled".您需要更改的属性不是“禁用”,而是“禁用”。
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.