简体   繁体   English

添加数量单选字段到 Ajax 添加到 WooCommerce 商店页面上的购物车按钮

[英]Add a quantity radio selector field to Ajax add to cart button on WooCommerce shop page

My product has a maximum quantity of 10. Customers often choose to buy many types at the same time.我的产品最大数量为 10 个。客户经常选择同时购买多种类型。 Hence I want to have a radio quantity selector (1 - 10) at the shop page.因此我想在商店页面上有一个无线电数量选择器 (1 - 10)。

I already know how to show quantity picker at the shop page and how to use WooCommerce radio form.我已经知道如何在商店页面显示数量选择器以及如何使用 WooCommerce 无线电表格。 But I don't know how to make a quantity radio selector.但我不知道如何制作数量无线电选择器。

Based on Add a quantity field to Ajax add to cart button on WooCommerce shop page answer code, this is my code attempt:基于Add a quantity field to Ajax add to cart button on WooCommerce shop page answer code,这是我的代码尝试:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 99, 2 );
function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        // Get the necessary classes
        $class = implode( ' ', array_filter( array(
            'button',
            'product_type_' . $product->get_type(),
            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
            $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
        ) ) );

        $args = array(
            'type' => 'radio',
            'class' => array( 'form-row-wide', 'update_totals_on_change' ),
            'options' => array(
                '0' => '0',
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
                '6' => '6',
                '7' => '7',
                '8' => '8',
                '9' => '9',
                '10' => '10'
            ),
            'default' => '0'
        );
        // Embedding the quantity field to Ajax add to cart button
        $html = sprintf( '%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
            woocommerce_form_field( 'radio_choice', $args, '0' ),
//             woocommerce_quantity_input( array(), $product, false ),
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->get_id() ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        );
    }
    return $html;
}

Any suggestion to get the code working?有什么建议可以使代码正常工作吗?

It looks like your missing the additional jQuery required to make this work.看起来您缺少完成这项工作所需的额外 jQuery。 I know you requested to make this work with a radio select but maybe to save space you want to consider a select ?我知道你要求用收音机 select 来完成这项工作,但也许为了节省空间你想考虑使用select

The following code does just that.下面的代码就是这样做的。

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 );
function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        // Get the necessary classes
        $class = implode( ' ', array_filter( array(
            'button',
            'product_type_' . $product->get_type(),
            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
            $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
        ) ) );

        $args = array(
            'type' => 'select',
            'class' => array( 'form-row-wide', 'quantity-select' ),
            'options' => array(
                '0' => '0',
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
                '6' => '6',
                '7' => '7',
                '8' => '8',
                '9' => '9',
                '10' => '10'
            ),
            'default' => '0'
        );

        // Embedding the quantity field to Ajax add to cart button
        $html = sprintf( '%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
            woocommerce_form_field( '', $args ),
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->get_id() ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        );
    }
    return $html;
}

add_action( 'wp_footer', 'archives_quantity_fields_script' );
function archives_quantity_fields_script(){
    ?>
    <script type='text/javascript'>
        jQuery(function($){
            // Update data-quantity
            $(document.body).on('change', '.quantity-select select', function() {
                let selected_quantity = $(this).find(":selected").text();
                $(this).closest('li.product').find('a.ajax_add_to_cart').attr('data-quantity', selected_quantity);
                $(".added_to_cart").remove(); // Optional: Removing other previous "view cart" buttons
            }).on('click', '.add_to_cart_button', function(){
                var button = $(this);
                setTimeout(function(){
                    console.log( button.siblings('.quantity-select').find('select option[value="0"]') );
                    button.siblings('.quantity-select').find('select').val('0'); // reset quantity to 0
                }, 1000); // After 1 second
            });
        });
    </script>
    <?php
}

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

相关问题 将数量字段添加到 WooCommerce 商店页面上的 Ajax 添加到购物车按钮 - Add a quantity field to Ajax add to cart button on WooCommerce shop page WooCommerce商店页面:添加到购物车按钮上的数量输入 - WooCommerce Shop page: Quantity input on add to cart button WooCommerce添加数量按钮到商店 - WooCommerce add quantity button to shop 在 WooCommerce 商店页面上添加数量字段并添加到购物车按钮 - Adding quantity fields and add to cart buton on WooCommerce Shop page 在 Woocommerce 3 商店页面上的添加到购物车下添加额外按钮 - Add Extra button under Add to cart on shop page of Woocommerce 3 Woocommerce商店页面(类别页面模板)添加到购物车按钮行为 - Woocommerce shop page (category page template) Add to cart button behavior 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce 在Woocommerce商店页面中更改用于简单产品的购物车按钮 - Change add-to-cart button for simple products in Woocommerce shop page 更改商店页面中 woocommerce 商店中“添加到购物车”按钮的重定向 - Changing the redirect of the “add to cart” button in woocommerce store in Shop page 在 Woocommerce 商店页面上自定义添加到购物车按钮 - Customizing add to cart buttons on Woocommerce shop page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM