is_purchasable. Closes #1331.

This commit is contained in:
Mike Jolley 2012-08-07 00:33:52 +01:00
parent 714f441ace
commit cedcca7fe7
5 changed files with 332 additions and 310 deletions

View File

@ -667,15 +667,9 @@ class WC_Cart {
if ( $product_data->is_sold_individually() )
$quantity = 1;
// Type/Exists check
if ( $product_data->is_type('external') || ! $product_data->exists() ) {
$woocommerce->add_error( __('This product cannot be purchased.', 'woocommerce') );
return false;
}
// Price set check
if( $product_data->get_price() === '' ) {
$woocommerce->add_error( __('This product cannot be purchased - the price is not yet set.', 'woocommerce') );
// Check product is_purchasable
if ( ! $product_data->is_purchasable() ) {
$woocommerce->add_error( __('Sorry, this product cannot be purchased.', 'woocommerce') );
return false;
}

View File

@ -581,6 +581,33 @@ class WC_Product {
return apply_filters( 'woocommerce_get_price', $this->price, $this );
}
/**
* is_purchasable function.
*
* Returns false if the product cannot be bought
*
* @access public
* @return void
*/
function is_purchasable() {
$purchasable = true;
// Products must exist of course
if ( ! $this->exists() )
$purchasable = false;
// External and grouped products cannot be bought
elseif ( $this->is_type( array( 'grouped', 'external' ) ) )
$purchasable = false;
// Other products types need a price to be set
elseif ( $this->get_price() === '' )
$purchasable = false;
return apply_filters( 'woocommerce_is_purchasable', $purchasable );
}
/** Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting */
function get_price_excluding_tax() {

View File

@ -172,6 +172,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Tweak - Added generate_title_html() to settings API
* Tweak - PayPal standard: No longer using shipping_1 because a) paypal ignore it if *any* shipping rules are within paypal, b) paypal ignore anyhing over 5 digits, so 999.99 is the max
* Tweak - Optimised dashboard icons for retina displays
* Tweak - added is_purchasable class method for products
* Fix - Removed session_name from 1.6 to prevent issues when other plugins start a session first. Instead, added a KB article on how to do it manually (if needed, this is an edge case) - http://wcdocs.woothemes.com/codex/extending/multiple-installs-on-one-domain-sessions-conflictsession-sharing-workaround/
* Fix - Product categories shortcode loop
* Fix - selected state for variation options

View File

@ -5,7 +5,7 @@
global $product;
if( $product->get_price() === '' && $product->product_type != 'external' ) return;
if ( $product->get_price() === '' && $product->product_type != 'external' ) return;
?>
<?php if ( ! $product->is_in_stock() ) : ?>

View File

@ -5,7 +5,7 @@
global $woocommerce, $product;
if( $product->get_price() === '') return;
if ( $product->get_price() === '' ) return;
?>
<?php