<?php | |
/** | |
* start the customisation | |
*/ | |
function custom_woo_before_shop_link() { | |
add_filter('woocommerce_loop_add_to_cart_link', 'custom_woo_loop_add_to_cart_link', 10, 2); | |
add_action('woocommerce_after_shop_loop', 'custom_woo_after_shop_loop'); | |
} | |
add_action('woocommerce_before_shop_loop', 'custom_woo_before_shop_link'); | |
/** | |
* customise Add to Cart link/button for product loop | |
* @param string $button | |
* @param object $product | |
* @return string | |
*/ | |
function custom_woo_loop_add_to_cart_link($button, $product) { | |
// not for variable, grouped or external products | |
if (!in_array($product->product_type, array('variable', 'grouped', 'external'))) { | |
// only if can be purchased | |
if ($product->is_purchasable()) { | |
// show qty +/- with button | |
ob_start(); | |
woocommerce_simple_add_to_cart(); | |
$button = ob_get_clean(); | |
// modify button so that AJAX add-to-cart script finds it | |
$replacement = sprintf('data-product_id="%d" data-quantity="1" $1 ajax_add_to_cart add_to_cart_button product_type_simple ', $product->id); | |
$button = preg_replace('/(class="single_add_to_cart_button)/', $replacement, $button); | |
} | |
} | |
return $button; | |
} | |
/** | |
* add the required JavaScript | |
*/ | |
function custom_woo_after_shop_loop() { | |
?> | |
<script> | |
jQuery(function($) { | |
<?php /* when product quantity changes, update quantity attribute on add-to-cart button */ ?> | |
$("form.cart").on("change", "input.qty", function() { | |
$(this.form).find("button[data-quantity]").data("quantity", this.value); | |
}); | |
<?php /* remove old "view cart" text, only need latest one thanks! */ ?> | |
$(document.body).on("adding_to_cart", function() { | |
$("a.added_to_cart").remove(); | |
}); | |
}); | |
</script> | |
<?php | |
} |
This comment has been minimized.
This comment has been minimized.
copying this into my functions help |
This comment has been minimized.
This comment has been minimized.
You are life saver! thanks a lot... |
This comment has been minimized.
This comment has been minimized.
Thanks a lot! |
This comment has been minimized.
This comment has been minimized.
Thanks for this great Solution, I have a simple question: |
This comment has been minimized.
This comment has been minimized.
If anyone gets this error |
This comment has been minimized.
This comment has been minimized.
Not working with latest woocommerce on latest WP (tested with Storefront theme). |
This comment has been minimized.
This comment has been minimized.
When I put number into quantity field and then click add to cart I get the same number in quantity fields of all products. Why is this happening |
This comment has been minimized.
This comment has been minimized.
I have the same issue as you johnymas, I wonder if you or other found a workaround already? |
This comment has been minimized.
This comment has been minimized.
@dvrcthewrld @johnymas @radicaldesk I just updated the script to add a new class into the button, so that WooCommerce 2.5 picks it up and uses AJAX to add to cart. View the revision history of this gist to see the changes. |
This comment has been minimized.
This comment has been minimized.
Thanks, But it still not working. I think my issue was the Add to Cart button is not being updated with the correct classes. Do you have a demo of this script that I can take a look? Thanks |
This comment has been minimized.
This comment has been minimized.
We have a issue, cause when we choose a quantity (2) and click in add_to_cart_button in the first time works fine, but after that, if we change the qty value for (3) or any other value in the cart increment just (2) that was the first value. |
This comment has been minimized.
This comment has been minimized.
I have the same problem as esilvajr:
|
This comment has been minimized.
This comment has been minimized.
@esilvajr as this is now using WooCommerce built-in functionality to send the value, I'd assume that it's a bug in WooCommerce. The font-end code in this seems to just update the button quantity data attribute. Check this is updating, but if it is, then it's a woocommerce bug. Update: Could be the use of Another thing to check-out https://gist.github.com/claudiosmweb/5114131 |
This comment has been minimized.
This comment has been minimized.
Not working for me :( |
This comment has been minimized.
This comment has been minimized.
Work great !!! :) |
This comment has been minimized.
This comment has been minimized.
Hi, i put the code in my function.php but still not using ajax... do i still have to modify the template with css class as the blog says? thank you |
This comment has been minimized.
This comment has been minimized.
Your code is working perfectly, but its not allowing to
So its not useful for me. Also on error its redirecting me to the shop page which is not required by me. Can I show error near the "add to cart" button? Also how can i add variations? By default woocommerce handle variation very well. I need to add product with variation using ajax. |
This comment has been minimized.
This comment has been minimized.
@esilvajr I had to add the following to "reset" the quantities on page load: |
This comment has been minimized.
This comment has been minimized.
This can not replace "single_add_to_cart_button" , how can we remove it? |
This comment has been minimized.
This comment has been minimized.
Not working or doing anything for me? I am testing it on the "Products/Archive" page |
This comment has been minimized.
This comment has been minimized.
Gracias, me ayudó bastante |
This comment has been minimized.
This comment has been minimized.
Hi, |
This comment has been minimized.
This comment has been minimized.
Hello, |
This comment has been minimized.
Works great!