簡體   English   中英

如何啟用 woocommerce cart.php 中的更新購物車按鈕?

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

您好,我一直在嘗試讓我的“更新購物車”按鈕在 woocommerce 制作的購物車頁面上工作。這是我自己的主題,我已經向其中添加了 cart.php 模板。 在 woocommerce 模板中,它有數量,但沒有易於使用的添加更多或更少按鈕。 所以我編輯了 global/quantity-input.php 以具有此特征。 我已經測試了單一產品頁面以使用我的新數量並添加到購物車。 這很好用。 但是在購物車頁面上,除非進行更改,否則更新購物車按鈕將被禁用,並且它無法識別我的更改。 在此處輸入圖像描述

我嘗試過的有效方法:

  • 當我手動 go 進入檢查器並從按鈕中刪除“禁用”屬性時
  • 當我按下“-”或“+”按鈕后,在數量輸入中按下鍵盤上的“enter”時。
  • 加號和減號確實會改變輸入字段中的數量
  • 輸入數量可啟用更新購物車按鈕

我試過但沒有用的東西:

  • 使用 javascript 定位按鈕並執行“update_cart.disable = false;”
  • 嘗試調用更改提交以自動調整購物車
  • 只是希望表格通過我的按鈕識別數量變化

我的代碼為 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();
    }

我在 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' ); ?>

我在瀏覽器中的 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>-->

我應該提到我的按鈕上的選擇器不工作,我不知道為什么。

另外,我對 woocommerce 很陌生,找不到關於這個主題的任何內容。 謝謝您閱讀此篇。

更新 1:

我找到了這張票並試了一下Woocommerce 2.6.2 adds disabled attribute to update cart button

  • 我將它添加到我的 javascript function 的底部(順便說一句,我確保它在 Jquery 包裝器中) $('button[name="update_cart"]' ).removeProp( 'disabled'); 但是,這也不起作用。 我使用了檢查器,它似乎沒有用選擇器返回任何東西,但是當我控制台記錄它時,我得到了一些東西。

您的 javascript 不工作的原因有兩個:

  1. 您需要更改的屬性不是“禁用”,而是“禁用”。

  2. 您正在使用 getElementsByName 從 DOM 中選擇按鈕,這實際上返回頁面中具有該名稱的所有元素的數組。 您訪問它並更改 disabled 屬性的方式(假設您只有一個具有該名稱的元素)如下所示:

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

然而,這是一種不好的做法,因為您可以擁有多個具有該名稱的元素,因此我建議為您的按鈕提供一個 ID,然后改用 getElementById。 在這種情況下,您不需要添加 [0]。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM