久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Woocommerce 3 中的自定義加減數量按鈕

Custom plus and minus quantity buttons in Woocommerce 3(Woocommerce 3 中的自定義加減數量按鈕)
本文介紹了Woocommerce 3 中的自定義加減數量按鈕的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在構建自定義 WordPress 和 WooCommerce 主題,并向產品頁面數量字段添加自定義加號和減號按鈕.這些按鈕會更新數量輸入的值,并且當我提交添加到購物車時,我還會收到商品已添加到您的購物車"通知(在產品頁面上).但是購物車頁面沒有顯示任何商品,也沒有說購物車是空的.

我不知道我想掛鉤哪個 WooCommerce JS 函數,也不知道如何掛鉤.請問可以幫忙嗎?!提前致謝!

我的 HTML 布局:

<label class="quantity__label" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e('Quantity:', 'woocommerce');?></label><div class="quantity__wrapper"><input type="button" value="-" class="quantity__button數量__remove js-qty-remove"/><輸入類型=文本"id="<?php echo esc_attr( $input_id ); ?>"class="輸入文本數量 文本數量__輸入"step="<?php echo esc_attr( $step ); ?>"min="<?php echo esc_attr( $min_value ); ?>"max="<?php echo esc_attr( 0 < $max_value ? $max_value : '' ); ?>"name="<?php echo esc_attr( $input_name ); ?>"value="<?php echo esc_attr( $input_value ); ?>"title="<?php echo esc_attr_x( 'Qty', '產品數量輸入工具提示', 'woocommerce'); ?>"大小=4"模式="<?php echo esc_attr( $pattern ); ?>"inputmode="<?php echo esc_attr( $inputmode ); ?>"aria-labelledby="<?php echo esc_attr( $labelledby ); ?>"/><input type="button" value="+" class="數量__按鈕數量__add js-qty-add"/>

我的自定義 jQuery 函數:

functionquantityButtons() {var $qtyAdd = $('.js-qty-add'),$qtyRemove = $('.js-qty-remove'),$qtyInput = $('.quantity__input');$qtyAdd.on('點擊', addQty);$qtyRemove.on('點擊', removeQty);函數 addQty() {var $qtyInput = $('.quantity__input'),$qtyRemove = $('.js-qty-remove'),$i = $qtyInput.val();$i++;$qtyRemove.attr("禁用", !$i);$qtyInput.val($i);}函數 removeQty() {var $qtyInput = $('.quantity__input'),$qtyRemove = $('.js-qty-remove'),$i = $qtyInput.val();如果 ($i >= 1) {$i--;$qtyInput.val($i);} 別的 {$qtyRemove.attr("禁用", true);}}$qtyRemove.attr("禁用", !$qtyInput.val());}數量按鈕();

解決方案

您的第一個代碼部分是由 global/quantity-input.php Woocommerce 模板代碼的自定義...

因此,為了測試,我已經部分更改了 global/quantity-input.php 模板代碼,如下(非常接近您的代碼):

<代碼>?><div class="數量"><label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e('Quantity', 'woocommerce');?></label><input type="button" value="-" class="qty_button減"/><輸入類型=數字"id="<?php echo esc_attr( $input_id ); ?>"類=輸入文本數量文本"step="<?php echo esc_attr( $step ); ?>"min="<?php echo esc_attr( $min_value ); ?>"max="<?php echo esc_attr( 0 < $max_value ? $max_value : '' ); ?>"name="<?php echo esc_attr( $input_name ); ?>"value="<?php echo esc_attr( $input_value ); ?>"title="<?php echo esc_attr_x( 'Qty', '產品數量輸入工具提示', 'woocommerce'); ?>"大小=4"模式="<?php echo esc_attr( $pattern ); ?>"inputmode="<?php echo esc_attr( $inputmode ); ?>"aria-labelledby="<?php echo esc_attr( $labelledby ); ?>"/><input type="button" value="+" class="qty_button plus"/>

<?php

現在必要的 CSS 和重新審視的 jQuery 代碼功能:

//刪除輸入字段類型編號上的 +/- 默認按鈕的最小 CSSadd_action('wp_head', 'custom_quantity_fields_css');函數 custom_quantity_fields_css(){?><風格>.quantity input::-webkit-outer-spin-button,.quantity input::-webkit-inner-spin-button {顯示:無;邊距:0;}.數量輸入.數量{外觀:文本框;-webkit 外觀:無;-moz 外觀:文本字段;}</風格><?php}add_action('wp_footer', 'custom_quantity_fields_script');函數 custom_quantity_fields_script(){?><script type='text/javascript'>jQuery( 函數( $ ) {如果(!String.prototype.getDecimals){String.prototype.getDecimals = function() {var num = 這個,match = ('' + num).match(/(?:.(d+))?(?:[eE]([+-]?d+))?$/);如果(!匹配){返回0;}return Math.max( 0, ( match[1] ? match[1].length : 0 ) - ( match[2] ? +match[2] : 0 ) );}}//數量加"和減"按鈕$( document.body ).on( 'click', '.plus, .minus', function() {var $qty = $( this ).closest( '.quantity' ).find( '.qty'),currentVal = parseFloat( $qty.val() ),max = parseFloat( $qty.attr( 'max' ) ),min = parseFloat( $qty.attr( 'min' ) ),step = $qty.attr('step');//格式化值if ( !currentVal || currentVal === '' || currentVal === 'NaN' ) currentVal = 0;if ( max === '' || max === 'NaN' ) max = '';if ( min === '' || min === 'NaN' ) min = 0;if ( step === 'any' || step === '' || step === undefined || parseFloat( step ) === 'NaN' ) step = 1;//改變值if ( $( this ).is( '.plus' ) ) {if ( max && ( currentVal >= max ) ) {$qty.val(max);} 別的 {$qty.val(( currentVal + parseFloat( step )).toFixed( step.getDecimals() ) );}} 別的 {if ( min && ( currentVal <= min ) ) {$qty.val(min);} else if ( currentVal > 0 ) {$qty.val((currentVal-parseFloat(step)).toFixed(step.getDecimals()));}}//觸發變化事件$qty.trigger('改變');});});<?php}

代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效.

數量按鈕加號"和減號"工作正常并以這種方式顯示:

將正確數量的產品添加到購物車:

如果您使用加號和減號按鈕更改數量字段值,則任何數量字段更改時都會激活更新購物車"按鈕.

當您點擊更新購物車"時,數量會正確更新.

I’m building custom WordPress and WooCommerce theme and adding custom plus and minus buttons to Product page quantity field. The buttons do update quantity input's value and I also receive "Item has been added to your cart" notification (on Product page) when I submit Add to Cart. But the cart page doesn’t show any items, neither says the cart is empty.

I can not work out which WooCommerce JS function I’m suppose to hook into, neither how to hook into it. Could I ask for help please?! Thanks in advance!

My HTML layout:

<div class="quantity">
    <label class="quantity__label" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e( 'Quantity:', 'woocommerce' ); ?></label>
    <div class="quantity__wrapper">
        <input type="button" value="-" class="quantity__button quantity__remove js-qty-remove" />
        <input
            type="text"
            id="<?php echo esc_attr( $input_id ); ?>"
            class="input-text qty text quantity__input"
            step="<?php echo esc_attr( $step ); ?>"
            min="<?php echo esc_attr( $min_value ); ?>"
            max="<?php echo esc_attr( 0 < $max_value ? $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' ); ?>"
            size="4"
            pattern="<?php echo esc_attr( $pattern ); ?>"
            inputmode="<?php echo esc_attr( $inputmode ); ?>"
            aria-labelledby="<?php echo esc_attr( $labelledby ); ?>" />
        <input type="button" value="+" class="quantity__button quantity__add js-qty-add" />
    </div>
</div>

My custom jQuery function:

function quantityButtons() {
    var $qtyAdd = $('.js-qty-add'),
        $qtyRemove = $('.js-qty-remove'),
        $qtyInput = $('.quantity__input');

    $qtyAdd.on('click', addQty);
    $qtyRemove.on('click', removeQty);

    function addQty() {
        var $qtyInput = $('.quantity__input'),
        $qtyRemove = $('.js-qty-remove'),
        $i = $qtyInput.val();

        $i++;
        $qtyRemove.attr("disabled", !$i);
        $qtyInput.val($i);
    }

    function removeQty() {
        var $qtyInput = $('.quantity__input'),
        $qtyRemove = $('.js-qty-remove'),
        $i = $qtyInput.val();

        if ($i >= 1) {
            $i--;
            $qtyInput.val($i);
        } else {
            $qtyRemove.attr("disabled", true);
        }
    }

    $qtyRemove.attr("disabled", !$qtyInput.val());
}

quantityButtons();

解決方案

Your First code part is made from a customization of global/quantity-input.php Woocommerce template code…

So for testing, I have changed partially that global/quantity-input.php template code with the following (very near to your code):

?>
<div class="quantity">
    <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></label>
    <input type="button" value="-" class="qty_button minus" />
    <input
        type="number"
        id="<?php echo esc_attr( $input_id ); ?>"
        class="input-text qty text"
        step="<?php echo esc_attr( $step ); ?>"
        min="<?php echo esc_attr( $min_value ); ?>"
        max="<?php echo esc_attr( 0 < $max_value ? $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' ); ?>"
        size="4"
        pattern="<?php echo esc_attr( $pattern ); ?>"
        inputmode="<?php echo esc_attr( $inputmode ); ?>"
        aria-labelledby="<?php echo esc_attr( $labelledby ); ?>" />
    <input type="button" value="+" class="qty_button plus" />
</div>
<?php

Now the necessary CSS and revisited jQuery code functions:

// Minimum CSS to remove +/- default buttons on input field type number
add_action( 'wp_head' , 'custom_quantity_fields_css' );
function custom_quantity_fields_css(){
    ?>
    <style>
    .quantity input::-webkit-outer-spin-button,
    .quantity input::-webkit-inner-spin-button {
        display: none;
        margin: 0;
    }
    .quantity input.qty {
        appearance: textfield;
        -webkit-appearance: none;
        -moz-appearance: textfield;
    }
    </style>
    <?php
}


add_action( 'wp_footer' , 'custom_quantity_fields_script' );
function custom_quantity_fields_script(){
    ?>
    <script type='text/javascript'>
    jQuery( function( $ ) {
        if ( ! String.prototype.getDecimals ) {
            String.prototype.getDecimals = function() {
                var num = this,
                    match = ('' + num).match(/(?:.(d+))?(?:[eE]([+-]?d+))?$/);
                if ( ! match ) {
                    return 0;
                }
                return Math.max( 0, ( match[1] ? match[1].length : 0 ) - ( match[2] ? +match[2] : 0 ) );
            }
        }
        // Quantity "plus" and "minus" buttons
        $( document.body ).on( 'click', '.plus, .minus', function() {
            var $qty        = $( this ).closest( '.quantity' ).find( '.qty'),
                currentVal  = parseFloat( $qty.val() ),
                max         = parseFloat( $qty.attr( 'max' ) ),
                min         = parseFloat( $qty.attr( 'min' ) ),
                step        = $qty.attr( 'step' );

            // Format values
            if ( ! currentVal || currentVal === '' || currentVal === 'NaN' ) currentVal = 0;
            if ( max === '' || max === 'NaN' ) max = '';
            if ( min === '' || min === 'NaN' ) min = 0;
            if ( step === 'any' || step === '' || step === undefined || parseFloat( step ) === 'NaN' ) step = 1;

            // Change the value
            if ( $( this ).is( '.plus' ) ) {
                if ( max && ( currentVal >= max ) ) {
                    $qty.val( max );
                } else {
                    $qty.val( ( currentVal + parseFloat( step )).toFixed( step.getDecimals() ) );
                }
            } else {
                if ( min && ( currentVal <= min ) ) {
                    $qty.val( min );
                } else if ( currentVal > 0 ) {
                    $qty.val( ( currentVal - parseFloat( step )).toFixed( step.getDecimals() ) );
                }
            }

            // Trigger change event
            $qty.trigger( 'change' );
        });
    });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

The quantity buttons "plus" and "minus" work perfectly and are displayed this way:

Products are added to cart with the correct quantity:

if you change the quantity field value with plus and minus buttons, the "Update cart" button is activated when any quantity field change.

When you click on "Update cart", the quantities as correctly updated.

這篇關于Woocommerce 3 中的自定義加減數量按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 一区二区三区四区在线 | 青春草国产 | 亚洲伊人精品酒店 | 一区二区欧美在线 | 国产日韩电影 | 国外成人免费视频 | 黄色av免费网站 | 日韩电影免费在线观看中文字幕 | 成人一区二区三区在线观看 | 综合久久99| 免费观看一级毛片 | 污书屋 | 成人av免费 | 久久精品中文字幕 | 国产欧美综合在线 | 在线免费观看黄a | 欧美一区二区三区四区视频 | 国产精品无码专区在线观看 | 国产一区二区欧美 | 午夜视频免费网站 | 综合久久网 | 天天操天天拍 | 中文字幕在线播放不卡 | 国产精品高潮呻吟久久av黑人 | 黄片毛片免费观看 | 少妇精品亚洲一区二区成人 | 日韩欧美在| 亚洲一区二区三区在线 | 人人爱干 | 久久不射网 | 精品亚洲永久免费精品 | 久久久久久国模大尺度人体 | 日本国产高清 | 久久九九99 | 日韩精品色网 | 国产日韩一区 | 黄一级| 欧美色性 | 成人毛片在线视频 | www.亚洲一区二区三区 | 久久国产一区二区 |