-
-
Save rexrony/3da7b31f9489a81acf7b78c62ac241ee to your computer and use it in GitHub Desktop.
WooCommerce purchase page add-to-cart with quantity and AJAX, by customising the add-to-cart template in the WooCommerce loop. See blog post for details: http://snippets.webaware.com.au/snippets/woocommerce-add-to-cart-with-quantity-and-ajax/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Loop Add to Cart -- with quantity and AJAX | |
| * requires associated JavaScript file qty-add-to-cart.js | |
| * | |
| * @link http://snippets.webaware.com.au/snippets/woocommerce-add-to-cart-with-quantity-and-ajax/ | |
| * @link https://gist.github.com/mikejolley/2793710/ | |
| */ | |
| // add this file to folder "woocommerce/loop" inside theme | |
| global $product; | |
| if( $product->get_price() === '' && $product->product_type != 'external' ) return; | |
| // script for add-to-cart with qty | |
| wp_enqueue_script('qty-add-to-cart', get_stylesheet_directory_uri() . '/js/qty-add-to-cart.js', array('jquery'), '1.0.1', true); | |
| ?> | |
| <?php if ( ! $product->is_in_stock() ) : ?> | |
| <a href="<?php echo get_permalink($product->id); ?>" class="button"><?php echo apply_filters('out_of_stock_add_to_cart_text', __('Read More', 'woocommerce')); ?></a> | |
| <?php else : ?> | |
| <?php | |
| switch ( $product->product_type ) { | |
| case "variable" : | |
| $link = get_permalink($product->id); | |
| $label = apply_filters('variable_add_to_cart_text', __('Select options', 'woocommerce')); | |
| break; | |
| case "grouped" : | |
| $link = get_permalink($product->id); | |
| $label = apply_filters('grouped_add_to_cart_text', __('View options', 'woocommerce')); | |
| break; | |
| case "external" : | |
| $link = get_permalink($product->id); | |
| $label = apply_filters('external_add_to_cart_text', __('Read More', 'woocommerce')); | |
| break; | |
| default : | |
| $link = esc_url( $product->add_to_cart_url() ); | |
| $label = apply_filters('add_to_cart_text', __('Add to cart', 'woocommerce')); | |
| break; | |
| } | |
| //printf('<a href="%s" rel="nofollow" data-product_id="%s" class="button add_to_cart_button product_type_%s">%s</a>', $link, $product->id, $product->product_type, $label); | |
| 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 woocommerce_quantity_input(); ?> | |
| <button type="submit" data-quantity="1" data-product_id="<?php echo $product->id; ?>" | |
| class="button alt ajax_add_to_cart add_to_cart_button product_type_simple"><?php echo $label; ?></button> | |
| </form> | |
| <?php | |
| } else { | |
| printf('<a href="%s" rel="nofollow" data-product_id="%s" class="button add_to_cart_button product_type_%s">%s</a>', $link, $product->id, $product->product_type, $label); | |
| } | |
| ?> | |
| <?php endif; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*! | |
| script for WooCommerce add to cart with quantity, via AJAX | |
| Author: support@webaware.com.au | |
| Author URI: http://snippets.webaware.com.au/ | |
| License: GPLv2 or later | |
| Version: 1.0.1 | |
| */ | |
| // @link http://snippets.webaware.com.au/snippets/woocommerce-add-to-cart-with-quantity-and-ajax/ | |
| // @link https://gist.github.com/mikejolley/2793710/ | |
| // add this file to folder "js" inside theme | |
| jQuery(function ($) { | |
| /* when product quantity changes, update quantity attribute on add-to-cart button */ | |
| $("form.cart").on("change", "input.qty", function() { | |
| if (this.value === "0") | |
| this.value = "1"; | |
| $(this.form).find("button[data-quantity]").data("quantity", this.value); | |
| }); | |
| /* remove old "view cart" text, only need latest one thanks! */ | |
| $(document.body).on("adding_to_cart", function() { | |
| $("a.added_to_cart").remove(); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I have been using successfully these add-to-cart.php and qty-add-to-cart.js since March-2017. Everything worked until last week. I am not able to add-to-cart more than 1pc from each product. When I go to cart I can change the qty from there. Is there some changes that i have to do in order to make it work again? Please help!
I increase the qty and click "add-to-cart" button, but it only adds 1 pc (I marked with red the actual qty I wanted and the qty that added to cart)

If I go in the shopping cart from there I can modify the qty. It worked since few days ago. It is probably because I updated some plugins.
PLEASE HELP.