Class tweaks, need more testing. Closes #1874.

This commit is contained in:
Mike Jolley 2012-11-29 16:48:40 +00:00
parent 37de4f62cb
commit 3c66ddae42
11 changed files with 422 additions and 465 deletions

View File

@ -170,7 +170,7 @@ function woocommerce_product_data_box() {
echo '</div>';
echo '<div class="options_group show_if_simple show_if_variable">';
echo '<div class="options_group show_if_simple show_if_external show_if_variable">';
// Tax
woocommerce_wp_select( array( 'id' => '_tax_status', 'label' => __( 'Tax Status', 'woocommerce' ), 'options' => array(

View File

@ -5,7 +5,7 @@
* The WooCommerce product class handles individual product data.
*
* @class WC_Product
* @version 1.6.4
* @version 1.7.0
* @package WooCommerce/Classes
* @author WooThemes
*/
@ -103,6 +103,8 @@ abstract class WC_Product {
/** @var string Formatted LxWxH. */
var $dimensions;
/** @var string "Yes" if sold individually. */
var $sold_individually;
/**
* __construct function.
@ -111,12 +113,16 @@ abstract class WC_Product {
* @param mixed $product
*/
function __construct( $product ) {
if ( is_object( $product ) ) {
$this->id = absint( $product->ID );
$this->post = $product;
} else {
$this->id = absint( $product );
$this->post = get_post( $this->id );
}
$this->product_custom_fields = get_post_custom( $this->id );
}
@ -125,12 +131,17 @@ abstract class WC_Product {
*
* @access public
* @param mixed $fields
* @param string $data (default: '')
* @return void
*/
function load_product_data( $fields ) {
function load_product_data( $fields, $data = '' ) {
if ( ! $data )
$data = $this->product_custom_fields;
if ( $fields )
foreach ( $fields as $key => $default )
$this->$key = isset( $this->product_custom_fields[ '_' . $key ][0] ) && $this->product_custom_fields[ '_' . $key ][0] !== '' ? $this->product_custom_fields[ '_' . $key ][0] : $default;
$this->$key = isset( $data[ '_' . $key ][0] ) && $data[ '_' . $key ][0] !== '' ? $data[ '_' . $key ][0] : $default;
}
@ -151,10 +162,7 @@ abstract class WC_Product {
* @return int
*/
function get_stock_quantity() {
if ( ! $this->managing_stock() )
return '';
return apply_filters( 'woocommerce_stock_amount', $this->stock );
return $this->managing_stock() ? apply_filters( 'woocommerce_stock_amount', $this->stock ) : '';
}
@ -189,8 +197,6 @@ abstract class WC_Product {
$woocommerce->clear_product_transients( $this->id ); // Clear transient
do_action( );
return $this->get_stock_quantity();
}
}
@ -248,8 +254,7 @@ abstract class WC_Product {
* @return bool
*/
function is_type( $type ) {
if ( $this->product_type == $type || ( is_array( $type ) && in_array( $this->product_type, $type ) ) ) return true;
return false;
return ( $this->product_type == $type || ( is_array( $type ) && in_array( $this->product_type, $type ) ) ) ? true : false;
}
@ -274,13 +279,7 @@ abstract class WC_Product {
* @return bool Whether downloadable product has a file attached.
*/
function has_file( $download_id = '' ) {
if ( ! $this->is_downloadable() )
return false;
if ( $this->get_file_download_path( $download_id ) )
return true;
return false;
return ( $this->is_downloadable() && $this->get_file_download_path( $download_id ) ) ? true : false;
}
@ -342,7 +341,7 @@ abstract class WC_Product {
$return = false;
// Sold individually if downloadable, virtual, and the option is enabled OR if intentionally a singular item
if ( 'yes' == get_post_meta( $this->id, '_sold_individually', true ) || ( $this->is_downloadable() && $this->is_virtual() && get_option('woocommerce_limit_downloadable_product_qty') == 'yes' ) || ( ! $this->backorders_allowed() && $this->get_stock_quantity() == 1 ) ) {
if ( 'yes' == $this->sold_individually || ( $this->is_downloadable() && $this->is_virtual() && get_option('woocommerce_limit_downloadable_product_qty') == 'yes' ) || ( ! $this->backorders_allowed() && $this->get_stock_quantity() == 1 ) ) {
$return = true;
}
@ -378,9 +377,7 @@ abstract class WC_Product {
* @return bool
*/
function exists() {
global $wpdb;
return ! empty( $this->post ) || $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d LIMIT 1;", $this->id ) ) > 0 ? true : false;
return empty( $this->post ) ? false : true;
}
@ -391,7 +388,7 @@ abstract class WC_Product {
* @return bool
*/
function is_taxable() {
return $this->tax_status == 'taxable' && get_option('woocommerce_calc_taxes') == 'yes' ? true : false;
return $this->tax_status == 'taxable' && get_option( 'woocommerce_calc_taxes' ) == 'yes' ? true : false;
}
@ -406,19 +403,6 @@ abstract class WC_Product {
}
/**
* Get the product's post data.
*
* @access public
* @return object
*/
function get_post_data() {
if ( empty( $this->post ) )
$this->post = get_post( $this->id );
return $this->post;
}
/**
* Get the title of the post.
*
@ -426,7 +410,6 @@ abstract class WC_Product {
* @return string
*/
function get_title() {
$this->get_post_data();
return apply_filters( 'woocommerce_product_title', apply_filters( 'the_title', $this->post->post_title, $this->id ), $this );
}
@ -438,7 +421,6 @@ abstract class WC_Product {
* @return int
*/
function get_parent() {
$this->get_post_data();
return apply_filters('woocommerce_product_parent', $this->post->post_parent, $this);
}
@ -461,9 +443,7 @@ abstract class WC_Product {
* @return bool
*/
function managing_stock() {
if ( ! isset( $this->manage_stock ) || $this->manage_stock == 'no' ) return false;
if ( get_option('woocommerce_manage_stock') == 'yes' ) return true;
return false;
return ( ! isset( $this->manage_stock ) || $this->manage_stock == 'no' || get_option('woocommerce_manage_stock') != 'yes' ) ? false : true;
}
@ -474,20 +454,29 @@ abstract class WC_Product {
* @return bool
*/
function is_in_stock() {
if ( $this->managing_stock() ) :
if ( ! $this->backorders_allowed() ) :
if ( $this->get_total_stock() < 1 ) :
return false;
else :
if ( $this->stock_status == 'instock' ) return true;
return false;
endif;
else :
if ( $this->managing_stock() ) {
if ( $this->backorders_allowed() ) {
return true;
endif;
endif;
if ( $this->stock_status == 'instock' ) return true;
return false;
} else {
if ( $this->get_total_stock() < 1 ) {
return false;
} else {
if ( $this->stock_status == 'instock' )
return true;
else
return false;
}
}
} else {
if ( $this->stock_status == 'instock' )
return true;
else
return false;
}
}
@ -498,7 +487,7 @@ abstract class WC_Product {
* @return bool
*/
function backorders_allowed() {
return $this->backorders=='yes' || $this->backorders=='notify' ? true : false;
return $this->backorders == 'yes' || $this->backorders == 'notify' ? true : false;
}
@ -509,7 +498,7 @@ abstract class WC_Product {
* @return bool
*/
function backorders_require_notification() {
return $this->managing_stock() && $this->backorders=='notify' ? true : false;
return $this->managing_stock() && $this->backorders == 'notify' ? true : false;
}
@ -545,17 +534,12 @@ abstract class WC_Product {
*/
function get_availability() {
$availability = "";
$class = "";
$availability = $class = "";
if (!$this->managing_stock()) :
if (!$this->is_in_stock()) :
$availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock';
endif;
else :
if ($this->is_in_stock()) :
if ( $this->get_total_stock() > 0 ) :
if ( $this->managing_stock() ) {
if ( $this->is_in_stock() ) {
if ( $this->get_total_stock() > 0 ) {
$format_option = get_option( 'woocommerce_stock_format' );
@ -575,35 +559,36 @@ abstract class WC_Product {
$availability = sprintf( $format, $this->stock );
if ($this->backorders_allowed() && $this->backorders_require_notification()) :
if ( $this->backorders_allowed() && $this->backorders_require_notification() )
$availability .= ' ' . __( '(backorders allowed)', 'woocommerce' );
endif;
else :
} else {
if ($this->backorders_allowed()) :
if ($this->backorders_require_notification()) :
if ( $this->backorders_allowed() ) {
if ( $this->backorders_require_notification() ) {
$availability = __( 'Available on backorder', 'woocommerce' );
$class = 'available-on-backorder';
else :
$class = 'available-on-backorder';
} else {
$availability = __( 'In stock', 'woocommerce' );
endif;
else :
}
} else {
$availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock';
endif;
$class = 'out-of-stock';
}
endif;
else :
if ($this->backorders_allowed()) :
$availability = __( 'Available on backorder', 'woocommerce' );
$class = 'available-on-backorder';
else :
$availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock';
endif;
endif;
endif;
}
} elseif ( $this->backorders_allowed() ) {
$availability = __( 'Available on backorder', 'woocommerce' );
$class = 'available-on-backorder';
} else {
$availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock';
}
} elseif ( ! $this->is_in_stock() ) {
$availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock';
}
return apply_filters( 'woocommerce_get_availability', array( 'availability' => $availability, 'class' => $class ), $this );
}
@ -685,13 +670,14 @@ abstract class WC_Product {
* Adjust a products price dynamically.
*
* @access public
* @param float $price Price to increase by.
* @param mixed $price
* @return void
*/
function adjust_price( $price ) {
if ( $price > 0 ) :
if ( $price > 0 )
$this->price += $price;
endif;
else
$this->price = $this->price - $price;
}
@ -738,16 +724,14 @@ abstract class WC_Product {
$price = $this->get_price();
if ( $this->is_taxable() && get_option('woocommerce_prices_include_tax')=='yes' ) :
if ( $this->is_taxable() && get_option('woocommerce_prices_include_tax') == 'yes' ) {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate( $this->tax_class );
$taxes = $_tax->calc_tax( $price, $tax_rates, true );
$tax_amount = $_tax->get_tax_total( $taxes );
$price = round( $price - $tax_amount, 2);
endif;
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate( $this->tax_class );
$taxes = $_tax->calc_tax( $price, $tax_rates, true );
$tax_amount = $_tax->get_tax_total( $taxes );
$price = round( $price - $tax_amount, 2);
}
return apply_filters( 'woocommerce_get_price_excluding_tax', $price, $this );
}
@ -785,6 +769,7 @@ abstract class WC_Product {
function get_price_html( $price = '' ) {
if ( $this->price > 0 ) {
if ( $this->is_on_sale() && isset( $this->regular_price ) ) {
$price .= $this->get_price_html_from_to( $this->regular_price, $this->get_price() );
@ -798,11 +783,11 @@ abstract class WC_Product {
$price = apply_filters( 'woocommerce_price_html', $price, $this );
}
} elseif ($this->price === '' ) {
} elseif ( $this->price === '' ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $this );
} elseif ($this->price == 0 ) {
} elseif ( $this->price == 0 ) {
if ( $this->is_on_sale() && isset( $this->regular_price ) ) {
@ -817,7 +802,6 @@ abstract class WC_Product {
$price = apply_filters( 'woocommerce_free_price_html', $price, $this );
}
}
return apply_filters( 'woocommerce_get_price_html', $price, $this );
@ -842,7 +826,7 @@ abstract class WC_Product {
* @return string
*/
function get_price_html_from_to( $from, $to ) {
return '<del>' . ((is_numeric($from)) ? woocommerce_price( $from ) : $from) . '</del> <ins>' . ((is_numeric($to)) ? woocommerce_price( $to ) : $to) . '</ins>';
return '<del>' . ( ( is_numeric( $from ) ) ? woocommerce_price( $from ) : $from ) . '</del> <ins>' . ( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) . '</ins>';
}
@ -944,7 +928,7 @@ abstract class WC_Product {
* @return array
*/
function get_tags( $sep = ', ', $before = '', $after = '' ) {
return get_the_term_list($this->id, 'product_tag', $before, $sep, $after);
return get_the_term_list( $this->id, 'product_tag', $before, $sep, $after );
}
@ -973,13 +957,13 @@ abstract class WC_Product {
* @return int
*/
function get_shipping_class_id() {
if ( ! $this->shipping_class_id ) :
if ( ! $this->shipping_class_id ) {
$classes = get_the_terms( $this->id, 'product_shipping_class' );
if ( $classes && ! is_wp_error( $classes ) )
$this->shipping_class_id = current( $classes )->term_id;
else
$this->shipping_class_id = 0;
endif;
}
return absint( $this->shipping_class_id );
}
@ -1000,11 +984,11 @@ abstract class WC_Product {
// Get tags
$terms = wp_get_post_terms($this->id, 'product_tag');
foreach ($terms as $term) $tags_array[] = $term->term_id;
foreach ( $terms as $term ) $tags_array[] = $term->term_id;
// Get categories
$terms = wp_get_post_terms($this->id, 'product_cat');
foreach ($terms as $term) $cats_array[] = $term->term_id;
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
// Don't bother if none are set
if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array();
@ -1016,22 +1000,22 @@ abstract class WC_Product {
// Get the posts
$related_posts = get_posts( apply_filters('woocommerce_product_related_posts', array(
'orderby' => 'rand',
'posts_per_page'=> $limit,
'post_type' => 'product',
'fields' => 'ids',
'meta_query' => $meta_query,
'tax_query' => array(
'relation' => 'OR',
'orderby' => 'rand',
'posts_per_page' => $limit,
'post_type' => 'product',
'fields' => 'ids',
'meta_query' => $meta_query,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
)
) ) );
@ -1069,7 +1053,6 @@ abstract class WC_Product {
}
}
return false;
}
@ -1084,7 +1067,7 @@ abstract class WC_Product {
if ( ! is_array( $this->attributes ) ) {
if (isset($this->product_custom_fields['_product_attributes'][0]))
if ( isset( $this->product_custom_fields['_product_attributes'][0] ) )
$this->attributes = maybe_unserialize( maybe_unserialize( $this->product_custom_fields['_product_attributes'][0] ));
else
$this->attributes = array();
@ -1151,24 +1134,24 @@ abstract class WC_Product {
* @return string
*/
function get_dimensions() {
if ( ! $this->dimensions ) :
$this->dimensions = '';
if ( ! $this->dimensions ) {
$dimensions = array();
// Show length
if ( $this->length ) {
$this->dimensions = $this->length;
// Show width also
if ( $this->width ) {
$this->dimensions .= ' x ' . $this->width;
// Show height also
if ( $this->height ) {
$this->dimensions .= ' x ' . $this->height;
}
}
// Append the unit
$this->dimensions .= ' '.get_option('woocommerce_dimension_unit');
}
endif;
if ( $this->length )
$dimensions[] = $this->length;
if ( $this->width )
$dimensions[] = $this->width;
if ( $this->height )
$dimensions[] = $this->height;
$this->dimensions = implode( ' x ', $dimensions );
if ( ! empty( $this->dimensions ) )
$this->dimensions .= ' ' . get_option( 'woocommerce_dimension_unit' );
}
return $this->dimensions;
}
@ -1181,7 +1164,7 @@ abstract class WC_Product {
*/
function list_attributes() {
woocommerce_get_template( 'single-product/product-attributes.php', array(
'product' => $this
'product' => $this
) );
}
@ -1208,40 +1191,4 @@ abstract class WC_Product {
return $image;
}
/**
* Checks sale data to see if the product is due to go on sale/sale has expired, and updates the main price.
*
* @access public
* @return void
*/
function check_sale_price() {
if ( $this->sale_price_dates_from && $this->sale_price_dates_from < current_time('timestamp') ) {
if ( $this->sale_price && $this->price !== $this->sale_price ) {
// Update price
$this->price = $this->sale_price;
update_post_meta( $this->id, '_price', $this->price );
}
}
if ( $this->sale_price_dates_to && $this->sale_price_dates_to < current_time('timestamp') ) {
if ( $this->regular_price && $this->price !== $this->regular_price ) {
$this->price = $this->regular_price;
update_post_meta( $this->id, '_price', $this->price );
// Sale has expired - clear the schedule boxes
update_post_meta( $this->id, '_sale_price', '' );
update_post_meta( $this->id, '_sale_price_dates_from', '' );
update_post_meta( $this->id, '_sale_price_dates_to', '' );
}
}
}
}

View File

@ -2,7 +2,7 @@
/**
* External Product Class
*
* External products cannot be bought; they link offsite.
* External products cannot be bought; they link offsite. Extends simple products.
*
* @class WC_Product_External
* @version 1.7.0
@ -11,7 +11,13 @@
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_External extends WC_Product {
class WC_Product_External extends WC_Product_Simple {
/** @var string URL to external product. */
var $product_url;
/** @var string Text for the buy/link button. */
var $button_text;
/**
* __construct function.
@ -22,38 +28,23 @@ class WC_Product_External extends WC_Product {
*/
function __construct( $product, $args ) {
parent::__construct( $product );
parent::__construct( $product, $args );
$this->product_type = 'external';
$this->product_custom_fields = get_post_custom( $this->id );
$this->downloadable = 'no';
$this->virtual = 'no';
$this->stock = '';
$this->stock_status = 'instock';
$this->manage_stock = 'no';
$this->weight = '';
$this->length = '';
$this->width = '';
$this->height = '';
// Load data from custom fields
$this->load_product_data( array(
'sku' => '',
'downloadable' => 'no',
'virtual' => 'no',
'price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'sale_price_dates_from' => '',
'sale_price_dates_to' => '',
'featured' => 'no'
'product_url' => '',
'button_text' => 'no'
) );
$this->check_sale_price();
}
/**
@ -65,4 +56,24 @@ class WC_Product_External extends WC_Product {
function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', false, $this );
}
/**
* get_product_url function.
*
* @access public
* @return void
*/
function get_product_url() {
return $this->product_url;
}
/**
* get_button_text function.
*
* @access public
* @return void
*/
function get_button_text() {
return $this->button_text ? $this->button_text : __( 'Buy product', 'woocommerce' );
}
}

View File

@ -6,15 +6,20 @@
* The WooCommerce product factory creating the right product object
*
* @class WC_Product_Factory
* @version 1.7
* @version 1.7.0
* @package WooCommerce/Classes
* @author WooThemes
*/
class WC_Product_Factory {
public function __construct() {
}
/**
* get_product function.
*
* @access public
* @param bool $the_product (default: false)
* @param array $args (default: array())
* @return void
*/
public function get_product( $the_product = false, $args = array() ) {
global $post;
@ -24,18 +29,18 @@ class WC_Product_Factory {
$the_product = get_post( $the_product );
}
$product_id = absint( $the_product->ID );
$post_type = $the_product->post_type;
$product_id = absint( $the_product->ID );
$post_type = $the_product->post_type;
if ( 'product_variation' == $post_type ) {
$product_type = 'variation';
} else {
$terms = get_the_terms( $product_id, 'product_type' );
$product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
$terms = get_the_terms( $product_id, 'product_type' );
$product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
}
// Filter classname so that the class can be overridden if extended.
$classname = apply_filters( 'woocommerce_product_class', 'WC_Product_' . ucfirst($product_type), $product_type, $post_type, $product_id );
$classname = apply_filters( 'woocommerce_product_class', 'WC_Product_' . ucfirst( $product_type ), $product_type, $post_type, $product_id );
if ( class_exists( $classname ) ) {
return new $classname( $the_product, $args );

View File

@ -32,34 +32,27 @@ class WC_Product_Grouped extends WC_Product {
$this->product_type = 'grouped';
$this->product_custom_fields = get_post_custom( $this->id );
$this->downloadable = 'no';
$this->virtual = 'no';
$this->stock = '';
$this->stock_status = 'instock';
$this->manage_stock = 'no';
$this->weight = '';
$this->length = '';
$this->width = '';
$this->height = '';
// Load data from custom fields
$this->load_product_data( array(
'sku' => '',
'downloadable' => 'no',
'virtual' => 'no',
'price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'sale_price_dates_from' => '',
'sale_price_dates_to' => '',
'featured' => 'no'
'sku' => '',
'price' => '',
'visibility' => 'hidden',
'sale_price' => '',
'regular_price' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'featured' => 'no'
) );
$this->check_sale_price();
}
/**
@ -153,18 +146,20 @@ class WC_Product_Grouped extends WC_Product {
* @return bool
*/
function is_on_sale() {
if ($this->has_child()) :
if ( $this->has_child() ) {
foreach ($this->get_children() as $child_id) :
foreach ( $this->get_children() as $child_id ) {
$sale_price = get_post_meta( $child_id, '_sale_price', true );
if ( $sale_price!=="" && $sale_price >= 0 ) return true;
endforeach;
if ( $sale_price !== "" && $sale_price >= 0 )
return true;
}
else :
} else {
if ( $this->sale_price && $this->sale_price==$this->price ) return true;
if ( $this->sale_price && $this->sale_price == $this->price )
return true;
endif;
}
return false;
}
@ -191,7 +186,8 @@ class WC_Product_Grouped extends WC_Product {
$child_prices = array();
foreach ( $this->get_children() as $child_id ) $child_prices[] = get_post_meta( $child_id, '_price', true );
foreach ( $this->get_children() as $child_id )
$child_prices[] = get_post_meta( $child_id, '_price', true );
$child_prices = array_unique( $child_prices );
@ -209,78 +205,4 @@ class WC_Product_Grouped extends WC_Product {
return apply_filters( 'woocommerce_get_price_html', $price, $this );
}
/**
* Checks sale data to see if the product is due to go on sale/sale has expired, and updates the main price.
*
* @access public
* @return void
*/
function check_sale_price() {
if ( $this->sale_price_dates_from && $this->sale_price_dates_from < current_time('timestamp') ) {
if ( $this->sale_price && $this->price !== $this->sale_price ) {
// Update price
$this->price = $this->sale_price;
update_post_meta( $this->id, '_price', $this->price );
// Grouped product prices and sale status are affected by children
$this->grouped_product_sync();
}
}
if ( $this->sale_price_dates_to && $this->sale_price_dates_to < current_time('timestamp') ) {
if ( $this->regular_price && $this->price !== $this->regular_price ) {
$this->price = $this->regular_price;
update_post_meta( $this->id, '_price', $this->price );
// Sale has expired - clear the schedule boxes
update_post_meta( $this->id, '_sale_price', '' );
update_post_meta( $this->id, '_sale_price_dates_from', '' );
update_post_meta( $this->id, '_sale_price_dates_to', '' );
// Grouped product prices and sale status are affected by children
$this->grouped_product_sync();
}
}
}
/**
* Sync grouped products with the childs lowest price (so they can be sorted by price accurately).
*
* @access public
* @return void
*/
function grouped_product_sync() {
global $wpdb, $woocommerce;
$post_parent = $wpdb->get_var( $wpdb->prepare( "SELECT post_parent FROM $wpdb->posts WHERE ID = %d;"), $this->id );
if (!$post_parent) return;
$children_by_price = get_posts( array(
'post_parent' => $post_parent,
'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => '_price',
'posts_per_page' => 1,
'post_type' => 'product',
'fields' => 'ids'
));
if ($children_by_price) :
foreach ($children_by_price as $child) :
$child_price = get_post_meta($child, '_price', true);
update_post_meta( $post_parent, '_price', $child_price );
endforeach;
endif;
$woocommerce->clear_product_transients( $this->id );
}
}

View File

@ -2,7 +2,7 @@
/**
* Simple Product Class
*
* The default product type kinda product
* The default product type kinda product.
*
* @class WC_Product_Simple
* @version 1.7.0
@ -25,34 +25,107 @@ class WC_Product_Simple extends WC_Product {
parent::__construct( $product );
$this->product_type = 'simple';
$this->product_custom_fields = get_post_custom( $this->id );
// Load data from custom fields
$this->load_product_data( array(
'sku' => '',
'downloadable' => 'no',
'virtual' => 'no',
'price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'sku' => '',
'downloadable' => 'no',
'virtual' => 'no',
'price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'sale_price_dates_from' => '',
'sale_price_dates_to' => '',
'featured' => 'no'
'sale_price_dates_to' => '',
'featured' => 'no',
'sold_individually' => 'no'
) );
$this->check_sale_price();
}
/**
* Checks sale data to see if the product is due to go on sale/sale has expired, and updates the main price.
*
* @access public
* @return bool
*/
function check_sale_price() {
if ( $this->sale_price_dates_from && $this->sale_price_dates_from < current_time('timestamp') ) {
if ( $this->sale_price && $this->price !== $this->sale_price ) {
// Update price
$this->price = $this->sale_price;
update_post_meta( $this->id, '_price', $this->price );
// Grouped product prices and sale status are affected by children
$this->grouped_product_sync();
}
}
if ( $this->sale_price_dates_to && $this->sale_price_dates_to < current_time('timestamp') ) {
if ( $this->regular_price && $this->price !== $this->regular_price ) {
$this->price = $this->regular_price;
update_post_meta( $this->id, '_price', $this->price );
// Sale has expired - clear the schedule boxes
update_post_meta( $this->id, '_sale_price', '' );
update_post_meta( $this->id, '_sale_price_dates_from', '' );
update_post_meta( $this->id, '_sale_price_dates_to', '' );
// Grouped product prices and sale status are affected by children
$this->grouped_product_sync();
}
}
}
/**
* Sync grouped products with the childs lowest price (so they can be sorted by price accurately).
*
* @access public
* @return void
*/
function grouped_product_sync() {
global $wpdb, $woocommerce;
if ( ! $this->get_parent() ) return;
$children_by_price = get_posts( array(
'post_parent' => $this->get_parent(),
'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => '_price',
'posts_per_page' => 1,
'post_type' => 'product',
'fields' => 'ids'
));
if ( $children_by_price ) {
foreach ( $children_by_price as $child ) {
$child_price = get_post_meta( $child, '_price', true );
update_post_meta( $post_parent, '_price', $child_price );
}
}
$woocommerce->clear_product_transients( $this->id );
}
}

View File

@ -53,37 +53,32 @@ class WC_Product_Variable extends WC_Product {
// Load data from custom fields
$this->load_product_data( array(
'sku' => '',
'downloadable' => 'no',
'virtual' => 'no',
'price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'sale_price_dates_from' => '',
'sale_price_dates_to' => '',
'featured' => 'no',
'min_variation_price' => '',
'max_variation_price' => '',
'min_variation_regular_price' => '',
'max_variation_regular_price' => '',
'min_variation_sale_price' => '',
'max_variation_sale_price' => '',
'sku' => '',
'price' => '',
'sale_price' => '',
'regular_price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'featured' => 'no',
'min_variation_price' => '',
'max_variation_price' => '',
'min_variation_regular_price' => '',
'max_variation_regular_price' => '',
'min_variation_sale_price' => '',
'max_variation_sale_price' => '',
'sold_individually' => 'no'
) );
$this->check_sale_price();
}
/**
@ -116,7 +111,6 @@ class WC_Product_Variable extends WC_Product {
set_transient( $transient_name, $this->total_stock );
}
}
return apply_filters( 'woocommerce_stock_amount', $this->total_stock );
}
@ -208,7 +202,11 @@ class WC_Product_Variable extends WC_Product {
* @return object WC_Product or WC_Product_variation
*/
function get_child( $child_id ) {
return get_product( $child_id, array( 'parent_id' => $this->id, 'meta' => $this->product_custom_fields ) );
return get_product( $child_id, array(
'parent_id' => $this->id,
'parent' => $this,
'meta' => $this->product_custom_fields
) );
}
@ -230,18 +228,20 @@ class WC_Product_Variable extends WC_Product {
* @return bool
*/
function is_on_sale() {
if ($this->has_child()) :
if ( $this->has_child() ) {
foreach ($this->get_children() as $child_id) :
foreach ( $this->get_children() as $child_id ) {
$sale_price = get_post_meta( $child_id, '_sale_price', true );
if ( $sale_price!=="" && $sale_price >= 0 ) return true;
endforeach;
if ( $sale_price !== "" && $sale_price >= 0 )
return true;
}
else :
} else {
if ( $this->sale_price && $this->sale_price==$this->price ) return true;
if ( $this->sale_price && $this->sale_price == $this->price )
return true;
endif;
}
return false;
}

View File

@ -5,7 +5,7 @@
* The WooCommerce product variation class handles product variation data.
*
* @class WC_Product_Variation
* @version 1.6.4
* @version 1.7.0
* @package WooCommerce/Classes
* @author WooThemes
*/
@ -56,6 +56,12 @@ class WC_Product_Variation extends WC_Product {
/** @var bool True if the variation has a tax class. */
var $variation_has_tax_class;
/** @var array Array of custom fields (meta) containing product data. */
var $parent_custom_fields;
/** @var object Parent Variable product object. */
var $parent;
/**
* Loads all product data from custom fields
*
@ -66,130 +72,130 @@ class WC_Product_Variation extends WC_Product {
*/
function __construct( $variation, $args = array() ) {
$this->product_type = 'variable';
if ( is_object( $variation ) ) {
$this->variation_id = absint( $variation->ID );
} else {
$this->variation_id = absint( $variation );
}
/* Get main product data from parent */
$this->id = ! empty( $args['parent_id'] ) ? intval( $args['parent_id'] ) : wp_get_post_parent_id( $this->variation_id );
/* Get main product data from parent (args) */
$this->id = ! empty( $args['parent_id'] ) ? intval( $args['parent_id'] ) : wp_get_post_parent_id( $this->variation_id );
// The post doesn't have a parent id, so it must be the parent
// The post doesn't have a parent id, therefore its invalid.
if ( empty( $this->id ) ) return false;
$product_custom_fields = get_post_custom( $this->variation_id );
// Get post data
$this->parent = ! empty( $args['parent'] ) ? $args['parent'] : get_product( $this->id );
$this->post = $this->parent->post;
// Get custom fields
$this->product_custom_fields = get_post_custom( $this->variation_id );
$this->parent_custom_fields = ! empty( $args['meta'] ) ? $args['meta'] : get_post_custom( $this->id );
$this->load_product_data( array(
'sku' => '',
'price' => 0,
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array()
), $this->parent_custom_fields );
// Get the variation attributes from meta
$this->variation_data = array();
foreach ( $product_custom_fields as $name => $value ) {
foreach ( $this->product_custom_fields as $name => $value ) {
if ( ! strstr( $name, 'attribute_' ) ) continue;
$this->variation_data[ $name ] = $value[0];
}
$parent_custom_fields = ! empty( $args['meta'] ) ? $args['meta'] : get_post_custom( $this->id );
// Define the data we're going to load from the parent: Key => Default value
$load_data = array(
'sku' => '',
'price' => 0,
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array()
);
// Load the data from the custom fields
foreach ( $load_data as $key => $default )
$this->$key = ( isset( $parent_custom_fields['_' . $key ][0] ) && $parent_custom_fields['_' . $key ][0] !== '' ) ? $parent_custom_fields['_' . $key ][0] : $default;
$this->product_type = 'variable';
// Now get variation meta and override the parent variable product
$this->variation_has_sku = $this->variation_has_stock = $this->variation_has_weight = $this->variation_has_length = $this->variation_has_width = $this->variation_has_height = $this->variation_has_price = $this->variation_has_regular_price = $this->variation_has_sale_price = false;
/* Override parent data with variation */
if ( isset( $product_custom_fields['_sku'][0] ) && ! empty( $product_custom_fields['_sku'][0] ) ) {
if ( ! empty( $this->product_custom_fields['_sku'][0] ) ) {
$this->variation_has_sku = true;
$this->sku = $product_custom_fields['_sku'][0];
$this->sku = $this->product_custom_fields['_sku'][0];
}
if ( isset( $product_custom_fields['_stock'][0] ) && $product_custom_fields['_stock'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_stock'][0] ) && $this->product_custom_fields['_stock'][0] !== '' ) {
$this->variation_has_stock = true;
$this->manage_stock = 'yes';
$this->stock = $product_custom_fields['_stock'][0];
$this->manage_stock = 'yes';
$this->stock = $this->product_custom_fields['_stock'][0];
}
if ( isset( $product_custom_fields['_weight'][0] ) && $product_custom_fields['_weight'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_weight'][0] ) && $this->product_custom_fields['_weight'][0] !== '' ) {
$this->variation_has_weight = true;
$this->weight = $product_custom_fields['_weight'][0];
$this->weight = $this->product_custom_fields['_weight'][0];
}
if ( isset( $product_custom_fields['_length'][0] ) && $product_custom_fields['_length'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_length'][0] ) && $this->product_custom_fields['_length'][0] !== '' ) {
$this->variation_has_length = true;
$this->length = $product_custom_fields['_length'][0];
$this->length = $this->product_custom_fields['_length'][0];
}
if ( isset( $product_custom_fields['_width'][0] ) && $product_custom_fields['_width'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_width'][0] ) && $this->product_custom_fields['_width'][0] !== '' ) {
$this->variation_has_width = true;
$this->width = $product_custom_fields['_width'][0];
$this->width = $this->product_custom_fields['_width'][0];
}
if ( isset( $product_custom_fields['_height'][0] ) && $product_custom_fields['_height'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_height'][0] ) && $this->product_custom_fields['_height'][0] !== '' ) {
$this->variation_has_height = true;
$this->height = $product_custom_fields['_height'][0];
$this->height = $this->product_custom_fields['_height'][0];
}
if ( isset( $product_custom_fields['_downloadable'][0] ) && $product_custom_fields['_downloadable'][0] == 'yes' ) {
if ( isset( $this->product_custom_fields['_downloadable'][0] ) && $this->product_custom_fields['_downloadable'][0] == 'yes' ) {
$this->downloadable = 'yes';
} else {
$this->downloadable = 'no';
}
if ( isset( $product_custom_fields['_virtual'][0] ) && $product_custom_fields['_virtual'][0] == 'yes' ) {
if ( isset( $this->product_custom_fields['_virtual'][0] ) && $this->product_custom_fields['_virtual'][0] == 'yes' ) {
$this->virtual = 'yes';
} else {
$this->virtual = 'no';
}
if ( isset( $product_custom_fields['_tax_class'][0] ) ) {
if ( isset( $this->product_custom_fields['_tax_class'][0] ) ) {
$this->variation_has_tax_class = true;
$this->tax_class = $product_custom_fields['_tax_class'][0];
$this->tax_class = $this->product_custom_fields['_tax_class'][0];
}
if ( isset( $product_custom_fields['_price'][0] ) && $product_custom_fields['_price'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_price'][0] ) && $this->product_custom_fields['_price'][0] !== '' ) {
$this->variation_has_price = true;
$this->price = $product_custom_fields['_price'][0];
$this->price = $this->product_custom_fields['_price'][0];
}
if ( isset( $product_custom_fields['_regular_price'][0] ) && $product_custom_fields['_regular_price'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_regular_price'][0] ) && $this->product_custom_fields['_regular_price'][0] !== '' ) {
$this->variation_has_regular_price = true;
$this->regular_price = $product_custom_fields['_regular_price'][0];
$this->regular_price = $this->product_custom_fields['_regular_price'][0];
}
if ( isset( $product_custom_fields['_sale_price'][0] ) && $product_custom_fields['_sale_price'][0] !== '' ) {
if ( isset( $this->product_custom_fields['_sale_price'][0] ) && $this->product_custom_fields['_sale_price'][0] !== '' ) {
$this->variation_has_sale_price = true;
$this->sale_price = $product_custom_fields['_sale_price'][0];
$this->sale_price = $this->product_custom_fields['_sale_price'][0];
}
// Backwards compat for prices
if ( $this->variation_has_price && ! $this->variation_has_regular_price ) {
update_post_meta( $this->variation_id, '_regular_price', $this->price );
$this->variation_has_regular_price = true;
$this->regular_price = $this->price;
$this->regular_price = $this->price;
if ( $this->variation_has_sale_price && $this->sale_price < $this->regular_price ) {
update_post_meta( $this->variation_id, '_price', $this->sale_price );
@ -197,11 +203,11 @@ class WC_Product_Variation extends WC_Product {
}
}
if ( isset( $product_custom_fields['_sale_price_dates_from'][0] ) )
$this->sale_price_dates_from = $product_custom_fields['_sale_price_dates_from'][0];
if ( isset( $this->product_custom_fields['_sale_price_dates_from'][0] ) )
$this->sale_price_dates_from = $this->product_custom_fields['_sale_price_dates_from'][0];
if ( isset( $product_custom_fields['_sale_price_dates_to'][0] ) )
$this->sale_price_dates_from = $product_custom_fields['_sale_price_dates_to'][0];
if ( isset( $this->product_custom_fields['_sale_price_dates_to'][0] ) )
$this->sale_price_dates_from = $this->product_custom_fields['_sale_price_dates_to'][0];
$this->total_stock = $this->stock;
@ -266,7 +272,7 @@ class WC_Product_Variation extends WC_Product {
$price = '';
if ( $this->price !== '' ) {
if ( $this->price == $this->sale_price ) {
if ( $this->price == $this->sale_price && $this->sale_price < $this->regular_price ) {
$price .= '<del>' . woocommerce_price( $this->regular_price ) . '</del> <ins>' . woocommerce_price( $this->sale_price ) . '</ins>';
$price = apply_filters( 'woocommerce_variation_sale_price_html', $price, $this );
} else {
@ -457,7 +463,7 @@ class WC_Product_Variation extends WC_Product {
update_post_meta( $this->variation_id, '_price', $this->price );
// Variable product prices and sale status are affected by children
$this->variable_product_sync();
$this->parent->variable_product_sync();
}
}
@ -475,7 +481,7 @@ class WC_Product_Variation extends WC_Product {
update_post_meta( $this->variation_id, '_sale_price_dates_to', '' );
// Variable product prices and sale status are affected by children
$this->variable_product_sync();
$this->parent->variable_product_sync();
}
}

View File

@ -36,13 +36,9 @@ foreach ( $product->get_children() as $child_id ) {
<?php foreach ( $grouped_products as $child_product ) : ?>
<tr>
<td>
<?php if ( $child_product['product']->is_type('external') ) :
<?php if ( $child_product['product']->is_type('external') ) : ?>
$product_url = get_post_meta( $child_product['product']->id, '_product_url', true );
$button_text = get_post_meta( $child_product['product']->id, '_button_text', true );
?>
<a href="<?php echo esc_url( $product_url ); ?>" rel="nofollow" class="button alt"><?php echo apply_filters('single_add_to_cart_text', esc_html( $button_text ), 'external'); ?></a>
<a href="<?php echo esc_url( $child_product['product']->get_product_url() ); ?>" rel="nofollow" class="button alt"><?php echo apply_filters('single_add_to_cart_text', esc_html( $child_product['product']->get_button_text() ), 'external'); ?></a>
<?php elseif ( ! $quantites_required ) : ?>

View File

@ -17,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
*/
add_filter( 'woocommerce_coupon_code', 'sanitize_text_field' );
add_filter( 'woocommerce_coupon_code', 'strtolower' ); // Coupons case-insensitive by default
add_filter( 'woocommerce_stock_amount', 'absint' ); // Stock amounts are integers by default
add_filter( 'woocommerce_stock_amount', 'intval' ); // Stock amounts are integers by default
/**
* Main function for returning products, uses the WC_Product_Factory class.

View File

@ -645,14 +645,11 @@ if ( ! function_exists( 'woocommerce_external_add_to_cart' ) ) {
function woocommerce_external_add_to_cart() {
global $product;
$product_url = get_post_meta( $product->id, '_product_url', true );
$button_text = get_post_meta( $product->id, '_button_text', true );
if ( ! $product_url ) return;
if ( ! $product->get_product_url() ) return;
woocommerce_get_template( 'single-product/add-to-cart/external.php', array(
'product_url' => $product_url,
'button_text' => ( $button_text ) ? $button_text : __( 'Buy product', 'woocommerce' ) ,
'product_url' => $product->get_product_url(),
'button_text' => $product->get_button_text()
) );
}
}