Search
 
SCRIPT & CODE EXAMPLE
 

PHP

WooCommerce quantity field to ajax add to cart button archive page

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' : '',
        ) ) );

        // Adding embeding <form> tag and the quantity field
        $html = sprintf( '%s%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>%s',
        '<form class="cart">',
        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() ),
        '</form>'
        );
    }
    return $html;
}

add_action( 'wp_footer' , 'archives_quantity_fields_script' );

function archives_quantity_fields_script(){
    //if( is_shop() || is_product_category() || is_product_tag() ): ?>
    <script type='text/javascript'>
        jQuery( document ).ready( function( $ ) {
        $( document ).on( 'change', '.quantity .qty', function() {
            $( this ).parent( '.quantity' ).next( '.add_to_cart_button' ).attr( 'data-quantity', $( this ).val() );
            //alert("Changed");
        });
    });
        
        jQuery(function($) {
            // Update quantity on 'a.button' in 'data-quantity' attribute (for ajax) 
            $(".add_to_cart_button.product_type_simple").on('click', function() {
                var $button = $(this);
                $button.data('quantity', $button.parent().find('input.qty').val());
            });
            // remove old "view cart" text, only need latest one thanks!
            $(document.body).on("adding_to_cart", function() {
                $("a.added_to_cart").remove();
            });
        });
    </script>
    <?php //endif;
}
Comment

PREVIOUS NEXT
Code Example
Php :: 0.01 bnb to php 
Php :: php pdo check if record exists before insert 
Php :: edit order of columns for wordpress 
Php :: Obtener rol de usuario registrado en WordPress 
Php :: PHP strtr — Translate characters or replace substrings 
Php :: how to depreciate a class in php comments 
Php :: view codeigniter 4 
Php :: sail laravel mix hot 
Php :: subdomain ajax request fail in php 
Php :: launch new tab and refresh original page codeigniter 
Php :: install tinymce php 
Php :: Removing the additional information tab using PHP code snippet 
Php :: php echo comand 
Php :: yii2 label of ActiveField 
Php :: $this-uri-uri_to_assoc(5); 
Php :: wordpress add menu frontend 
Php :: seed specific seeder laravel 
Php :: Dein Benutzer-Profil um weitere Social Media Accounts erweitern 
Php :: php how to check if page is accessed via post 
Php :: docker php-fpm 
Php :: laravel retry failed transactions 
Php :: phpunit-watcher 
Php :: remove public laravel 
Php :: magento 2 isSetFlag() 
Php :: Available excel column formatting 
Php :: php objects 
Php :: infoplist codepush key 
Php :: create procedure with pdo php 
Php :: php decrement variable 
Php :: php on page query 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =