Use generic `ajax_add_to_cart` class for adding items in the loop to the cart via ajax.

This commit is contained in:
Kathy Darling 2015-10-30 12:54:49 -05:00
parent 37ed73f5b4
commit 856d192bff
3 changed files with 15 additions and 3 deletions

View File

@ -12,7 +12,7 @@ jQuery( function( $ ) {
// AJAX add to cart request // AJAX add to cart request
var $thisbutton = $( this ); var $thisbutton = $( this );
if ( $thisbutton.is( '.product_type_simple' ) ) { if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
if ( ! $thisbutton.attr( 'data-product_id' ) ) { if ( ! $thisbutton.attr( 'data-product_id' ) ) {
return true; return true;

View File

@ -750,6 +750,17 @@ class WC_Product {
return apply_filters( 'woocommerce_is_purchasable', $purchasable, $this ); return apply_filters( 'woocommerce_is_purchasable', $purchasable, $this );
} }
/**
* Returns whether or not the product can be added to the cart via ajax
*
* @since 2.4.9
* @return bool
*/
public function supports_ajax_add_to_cart() {
return apply_filters( 'woocommerce_product_supports_ajax_add_to_cart', $this->is_type( 'simple' ), $this );
}
/** /**
* Set a products price dynamically. * Set a products price dynamically.
* *

View File

@ -12,7 +12,7 @@
* @see http://docs.woothemes.com/document/template-structure/ * @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes * @author WooThemes
* @package WooCommerce/Templates * @package WooCommerce/Templates
* @version 2.1.0 * @version 2.4.9
*/ */
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
@ -22,12 +22,13 @@ if ( ! defined( 'ABSPATH' ) ) {
global $product; global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link', echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>', sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s %s product_type_%s">%s</a>',
esc_url( $product->add_to_cart_url() ), esc_url( $product->add_to_cart_url() ),
esc_attr( $product->id ), esc_attr( $product->id ),
esc_attr( $product->get_sku() ), esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ), esc_attr( isset( $quantity ) ? $quantity : 1 ),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports_ajax_add_to_cart() ? 'ajax_add_to_cart' : '',
esc_attr( $product->product_type ), esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() ) esc_html( $product->add_to_cart_text() )
), ),