supports() method which mirrors gateways

Closes #9471
This commit is contained in:
Mike Jolley 2015-11-02 11:23:50 +00:00
parent 856d192bff
commit aec0e0e327
3 changed files with 25 additions and 16 deletions

View File

@ -57,7 +57,7 @@ class WC_Product {
public $product_type = null;
/**
* Prouduct shipping class
* Product shipping class
*
* @var string
*/
@ -70,6 +70,12 @@ class WC_Product {
*/
protected $shipping_class_id = 0;
/**
* Supported features such as 'ajax_add_to_cart'
* @var array
*/
protected $supports = array();
/**
* Constructor gets the post object and sets the ID for the loaded product.
*
@ -144,6 +150,19 @@ class WC_Product {
return $this->post;
}
/**
* Check if a product supports a given feature.
*
* Product classes should override this to declare support (or lack of support) for a feature.
*
* @param string $feature string The name of a feature to test support for.
* @return bool True if the product supports the feature, false otherwise.
* @since 2.5.0
*/
public function supports( $feature ) {
return apply_filters( 'woocommerce_product_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this );
}
/**
* get_gallery_attachment_ids function.
*
@ -750,17 +769,6 @@ class WC_Product {
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.
*

View File

@ -24,6 +24,7 @@ class WC_Product_Simple extends WC_Product {
*/
public function __construct( $product ) {
$this->product_type = 'simple';
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
}

View File

@ -12,11 +12,11 @@
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.4.9
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
global $product;
@ -28,7 +28,7 @@ echo apply_filters( 'woocommerce_loop_add_to_cart_link',
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports_ajax_add_to_cart() ? 'ajax_add_to_cart' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )
),