Support » Plugin: WooCommerce » [add_to_cart] with quantity and AJAX – quantity not working properly

  • Hi, I was googling for many hours and didn´t find the working solution for using Add_to_cart shortcode with quantity and AJAX.

    I am now using this in functions.php

    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // END ENQUEUE PARENT ACTION
    
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );
    remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
    remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
    add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
    add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);
    
    function my_theme_wrapper_start() {
     echo '<div class="sitecontainer">';
     echo '<div class="pagecontent no-caption">';
     echo '<div class="container blog-sidebar">';
     echo '<div class="eleven columns shop-side-right ">';
    }
    
    function my_theme_wrapper_end() {
     echo '</div><div class="four columns offset-by-one shop-side-left">';
     if (is_active_sidebar( 'obchod-sidebar' ) ):
    dynamic_sidebar( 'obchod-sidebar' );
    endif; 
     echo '</div><div class="clear"></div></div>';
    }
    
    add_action( 'after_setup_theme', 'woocommerce_support' );
    function woocommerce_support() {
        add_theme_support( 'woocommerce' );
    }
    
    // Sidebar for shop
    
    add_action( 'widgets_init', 'theme_slug_widgets_init' );
    function theme_slug_widgets_init() {
        register_sidebar( array(
            'name' => 'Obchod-Vpravo',
            'id' => 'obchod-sidebar',
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
    	'after_widget'  => '</div><div class="clear"></div>',
    	'before_title'  => '<h3 class="widget-title">',
    	'after_title'   => '</h3>',
        ) );
    }
    
    // delete cart messages
    
    add_filter( 'wc_add_to_cart_message', 'bbloomer_custom_add_to_cart_message' );
      
    function bbloomer_custom_add_to_cart_message() {
      
    echo '<style>.woocommerce-message {display: none !important;}</style>';
     
    }
    
    // ajax add to cart
    
    function cs_wc_loop_add_to_cart_scripts() {
        if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) : ?>
    
    <script>
        jQuery( document ).ready( function( $ ) {
            $( document ).on( 'change', '.quantity .qty', function() {
                $( this ).parent( '.quantity' ).next( '.add_to_cart_button' ).attr( 'data-quantity', $( this ).val() );
            });
        });
    </script>
    
        <?php endif;
    }
    add_action( 'wp_footer', 'cs_wc_loop_add_to_cart_scripts' );

    And I am using this for add to cart.php:

    <?php
    /**
     * Custom Loop Add to Cart.
     *
     * Template with quantity and ajax.
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
    
    global $product;
    ?>
    
    <?php if ( ! $product->is_in_stock() ) : ?>
    
        <a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></a>
    
    <?php else : ?>
    
        <?php
            $link = array(
                'url'   => '',
                'label' => '',
                'class' => ''
            );
    
            switch ( $product->product_type ) {
                case "variable" :
                    $link['url']    = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
                    $link['label']  = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
                break;
                case "grouped" :
                    $link['url']    = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
                    $link['label']  = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
                break;
                case "external" :
                    $link['url']    = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
                    $link['label']  = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
                break;
                default :
                    if ( $product->is_purchasable() ) {
                        $link['url']    = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
                        $link['label']  = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
                        $link['class']  = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
                    } else {
                        $link['url']    = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
                        $link['label']  = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
                    }
                break;
            }
    
            // If there is a simple product.
            if ( $product->product_type == 'simple' ) {
                ?>
                <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data">
                    <?php
                        // Displays the quantity box.
                        woocommerce_quantity_input();
    
                        // Display the submit button.
                        echo sprintf( '<button type="submit" data-product_id="%s" data-product_sku="%s" data-quantity="1" class="%s button ajax_add_to_cart">%s</button>', esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_html( $link['label'] ) );
                    ?>
                </form>
                <?php
            } else {
              echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
            }
    
        ?>
    
    <?php endif; ?>
    

    The webpage is https://www.bestonline.cz/google-g-suite/ (right side of pricing table have add to cart buttons (přidat do košíku)).

    And the problem is, that it works but everytime add only 1 product. It does not work with quantity.

    Can someone help me please? Love you guys!

    Best regards,

    Aleš

Viewing 2 replies - 1 through 2 (of 2 total)
  • Ok I changed the

    if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) : ?>

    to
    if ( !is_shop() || !is_product_category() || !is_product_tag() || !is_product() ) : ?>

    now it is working, but when you add 100 items to cart, and then 50, it still adds the 100
    .. Any Idea?

    Plugin Support RK

    (@riaanknoetze)

    Best bet would bet to get in touch with a developer community for more feedback on this; Usually these forums are mostly based on the default functioning of the plugin, not help with custom coding.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[add_to_cart] with quantity and AJAX – quantity not working properly’ is closed to new replies.