Ajax add to cart with quantity is working only once for my woocommerce website -


on shop page website, have implemented add cart ajax functionality along quantity input.

i have used code snippet referred website.

the functionality working ajax. so, if enter 10 in quantity box , click on 'add cart' 10 products added cart through ajax. problem if in same quantity box (for same product) if want add 5 more , if enter 5 in box , hit 'add cart' adds 10 items again , not 5.

so, holds 10 value , keeps adding no matter put in quantity box. can check on website.

so, if can me solve problem having @ code below.

the code snippets follows:

replaced add-to-cart.php file following code:

add-to-cart.php

<?php /**  * custom loop add cart.  *  * template quantity , 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 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 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 quantity box.                     woocommerce_quantity_input();                      // display submit button.                     echo sprintf( '<button type="submit" data-product_id="%s" data-product_sku="%s" data-quantity="1" class="%s button product_type_simple">%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; ?> 

added following code functions.php file:

functions.php

function cs_wc_loop_add_to_cart_scripts() {     if ( is_shop() || is_product_category() || is_product_tag() || is_product() || is_front_page() || is_home() ) : ?>  <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' ); 

quantity-input.php

<div class="quantity"><a href="javascript:void(0)" class="minus">-</a><input type="number" step="<?php echo esc_attr( $step ); ?>" <?php if ( is_numeric( $min_value ) ) : ?>min="<?php echo esc_attr( $min_value ); ?>"<?php endif; ?> <?php if ( is_numeric( $max_value ) ) : ?>max="<?php echo esc_attr( $max_value ); ?>"<?php endif; ?> 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' ) ?>" class="input-text qty text" size="4" /><a href="javascript:void(0)" class="plus">+</a></div> 

i think data-quantity not work correctly .attr().

use .data().. replace code..

$(this).parent('.quantity').next('.add_to_cart_button').attr('data-quantity', $(this).val()); 

try this..

$(this).closest('form').find('.add_to_cart_button').data('quantity', this.value); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -