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>';
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 // Tax
woocommerce_wp_select( array( 'id' => '_tax_status', 'label' => __( 'Tax Status', 'woocommerce' ), 'options' => array( 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. * The WooCommerce product class handles individual product data.
* *
* @class WC_Product * @class WC_Product
* @version 1.6.4 * @version 1.7.0
* @package WooCommerce/Classes * @package WooCommerce/Classes
* @author WooThemes * @author WooThemes
*/ */
@ -103,6 +103,8 @@ abstract class WC_Product {
/** @var string Formatted LxWxH. */ /** @var string Formatted LxWxH. */
var $dimensions; var $dimensions;
/** @var string "Yes" if sold individually. */
var $sold_individually;
/** /**
* __construct function. * __construct function.
@ -111,12 +113,16 @@ abstract class WC_Product {
* @param mixed $product * @param mixed $product
*/ */
function __construct( $product ) { function __construct( $product ) {
if ( is_object( $product ) ) { if ( is_object( $product ) ) {
$this->id = absint( $product->ID ); $this->id = absint( $product->ID );
$this->post = $product; $this->post = $product;
} else { } else {
$this->id = absint( $product ); $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 * @access public
* @param mixed $fields * @param mixed $fields
* @param string $data (default: '')
* @return void * @return void
*/ */
function load_product_data( $fields ) { function load_product_data( $fields, $data = '' ) {
if ( ! $data )
$data = $this->product_custom_fields;
if ( $fields ) if ( $fields )
foreach ( $fields as $key => $default ) 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 * @return int
*/ */
function get_stock_quantity() { function get_stock_quantity() {
if ( ! $this->managing_stock() ) return $this->managing_stock() ? apply_filters( 'woocommerce_stock_amount', $this->stock ) : '';
return '';
return apply_filters( 'woocommerce_stock_amount', $this->stock );
} }
@ -189,8 +197,6 @@ abstract class WC_Product {
$woocommerce->clear_product_transients( $this->id ); // Clear transient $woocommerce->clear_product_transients( $this->id ); // Clear transient
do_action( );
return $this->get_stock_quantity(); return $this->get_stock_quantity();
} }
} }
@ -248,8 +254,7 @@ abstract class WC_Product {
* @return bool * @return bool
*/ */
function is_type( $type ) { function is_type( $type ) {
if ( $this->product_type == $type || ( is_array( $type ) && in_array( $this->product_type, $type ) ) ) return true; return ( $this->product_type == $type || ( is_array( $type ) && in_array( $this->product_type, $type ) ) ) ? true : false;
return false;
} }
@ -274,13 +279,7 @@ abstract class WC_Product {
* @return bool Whether downloadable product has a file attached. * @return bool Whether downloadable product has a file attached.
*/ */
function has_file( $download_id = '' ) { function has_file( $download_id = '' ) {
if ( ! $this->is_downloadable() ) return ( $this->is_downloadable() && $this->get_file_download_path( $download_id ) ) ? true : false;
return false;
if ( $this->get_file_download_path( $download_id ) )
return true;
return false;
} }
@ -342,7 +341,7 @@ abstract class WC_Product {
$return = false; $return = false;
// Sold individually if downloadable, virtual, and the option is enabled OR if intentionally a singular item // 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; $return = true;
} }
@ -378,9 +377,7 @@ abstract class WC_Product {
* @return bool * @return bool
*/ */
function exists() { function exists() {
global $wpdb; return empty( $this->post ) ? false : true;
return ! empty( $this->post ) || $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d LIMIT 1;", $this->id ) ) > 0 ? true : false;
} }
@ -391,7 +388,7 @@ abstract class WC_Product {
* @return bool * @return bool
*/ */
function is_taxable() { 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. * Get the title of the post.
* *
@ -426,7 +410,6 @@ abstract class WC_Product {
* @return string * @return string
*/ */
function get_title() { function get_title() {
$this->get_post_data();
return apply_filters( 'woocommerce_product_title', apply_filters( 'the_title', $this->post->post_title, $this->id ), $this ); 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 * @return int
*/ */
function get_parent() { function get_parent() {
$this->get_post_data();
return apply_filters('woocommerce_product_parent', $this->post->post_parent, $this); return apply_filters('woocommerce_product_parent', $this->post->post_parent, $this);
} }
@ -461,9 +443,7 @@ abstract class WC_Product {
* @return bool * @return bool
*/ */
function managing_stock() { function managing_stock() {
if ( ! isset( $this->manage_stock ) || $this->manage_stock == 'no' ) return false; return ( ! isset( $this->manage_stock ) || $this->manage_stock == 'no' || get_option('woocommerce_manage_stock') != 'yes' ) ? false : true;
if ( get_option('woocommerce_manage_stock') == 'yes' ) return true;
return false;
} }
@ -474,20 +454,29 @@ abstract class WC_Product {
* @return bool * @return bool
*/ */
function is_in_stock() { function is_in_stock() {
if ( $this->managing_stock() ) : if ( $this->managing_stock() ) {
if ( ! $this->backorders_allowed() ) :
if ( $this->get_total_stock() < 1 ) : if ( $this->backorders_allowed() ) {
return false;
else :
if ( $this->stock_status == 'instock' ) return true;
return false;
endif;
else :
return true; return true;
endif; } else {
endif; if ( $this->get_total_stock() < 1 ) {
if ( $this->stock_status == 'instock' ) return true; return false;
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 * @return bool
*/ */
function backorders_allowed() { 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 * @return bool
*/ */
function backorders_require_notification() { 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() { function get_availability() {
$availability = ""; $availability = $class = "";
$class = "";
if (!$this->managing_stock()) : if ( $this->managing_stock() ) {
if (!$this->is_in_stock()) : if ( $this->is_in_stock() ) {
$availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock'; if ( $this->get_total_stock() > 0 ) {
endif;
else :
if ($this->is_in_stock()) :
if ( $this->get_total_stock() > 0 ) :
$format_option = get_option( 'woocommerce_stock_format' ); $format_option = get_option( 'woocommerce_stock_format' );
@ -575,35 +559,36 @@ abstract class WC_Product {
$availability = sprintf( $format, $this->stock ); $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' ); $availability .= ' ' . __( '(backorders allowed)', 'woocommerce' );
endif;
else : } else {
if ($this->backorders_allowed()) : if ( $this->backorders_allowed() ) {
if ($this->backorders_require_notification()) : if ( $this->backorders_require_notification() ) {
$availability = __( 'Available on backorder', 'woocommerce' ); $availability = __( 'Available on backorder', 'woocommerce' );
$class = 'available-on-backorder'; $class = 'available-on-backorder';
else : } else {
$availability = __( 'In stock', 'woocommerce' ); $availability = __( 'In stock', 'woocommerce' );
endif; }
else : } else {
$availability = __( 'Out of stock', 'woocommerce' ); $availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock'; $class = 'out-of-stock';
endif; }
endif; }
else :
if ($this->backorders_allowed()) : } elseif ( $this->backorders_allowed() ) {
$availability = __( 'Available on backorder', 'woocommerce' ); $availability = __( 'Available on backorder', 'woocommerce' );
$class = 'available-on-backorder'; $class = 'available-on-backorder';
else : } else {
$availability = __( 'Out of stock', 'woocommerce' ); $availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock'; $class = 'out-of-stock';
endif; }
endif; } elseif ( ! $this->is_in_stock() ) {
endif; $availability = __( 'Out of stock', 'woocommerce' );
$class = 'out-of-stock';
}
return apply_filters( 'woocommerce_get_availability', array( 'availability' => $availability, 'class' => $class ), $this ); 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. * Adjust a products price dynamically.
* *
* @access public * @access public
* @param float $price Price to increase by. * @param mixed $price
* @return void * @return void
*/ */
function adjust_price( $price ) { function adjust_price( $price ) {
if ( $price > 0 ) : if ( $price > 0 )
$this->price += $price; $this->price += $price;
endif; else
$this->price = $this->price - $price;
} }
@ -738,16 +724,14 @@ abstract class WC_Product {
$price = $this->get_price(); $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 = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate( $this->tax_class );
$tax_rates = $_tax->get_shop_base_rate( $this->tax_class ); $taxes = $_tax->calc_tax( $price, $tax_rates, true );
$taxes = $_tax->calc_tax( $price, $tax_rates, true ); $tax_amount = $_tax->get_tax_total( $taxes );
$tax_amount = $_tax->get_tax_total( $taxes ); $price = round( $price - $tax_amount, 2);
$price = round( $price - $tax_amount, 2); }
endif;
return apply_filters( 'woocommerce_get_price_excluding_tax', $price, $this ); return apply_filters( 'woocommerce_get_price_excluding_tax', $price, $this );
} }
@ -785,6 +769,7 @@ abstract class WC_Product {
function get_price_html( $price = '' ) { function get_price_html( $price = '' ) {
if ( $this->price > 0 ) { if ( $this->price > 0 ) {
if ( $this->is_on_sale() && isset( $this->regular_price ) ) { if ( $this->is_on_sale() && isset( $this->regular_price ) ) {
$price .= $this->get_price_html_from_to( $this->regular_price, $this->get_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 ); $price = apply_filters( 'woocommerce_price_html', $price, $this );
} }
} elseif ($this->price === '' ) { } elseif ( $this->price === '' ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $this ); $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 ) ) { 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 ); $price = apply_filters( 'woocommerce_free_price_html', $price, $this );
} }
} }
return apply_filters( 'woocommerce_get_price_html', $price, $this ); return apply_filters( 'woocommerce_get_price_html', $price, $this );
@ -842,7 +826,7 @@ abstract class WC_Product {
* @return string * @return string
*/ */
function get_price_html_from_to( $from, $to ) { 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 * @return array
*/ */
function get_tags( $sep = ', ', $before = '', $after = '' ) { 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 * @return int
*/ */
function get_shipping_class_id() { function get_shipping_class_id() {
if ( ! $this->shipping_class_id ) : if ( ! $this->shipping_class_id ) {
$classes = get_the_terms( $this->id, 'product_shipping_class' ); $classes = get_the_terms( $this->id, 'product_shipping_class' );
if ( $classes && ! is_wp_error( $classes ) ) if ( $classes && ! is_wp_error( $classes ) )
$this->shipping_class_id = current( $classes )->term_id; $this->shipping_class_id = current( $classes )->term_id;
else else
$this->shipping_class_id = 0; $this->shipping_class_id = 0;
endif; }
return absint( $this->shipping_class_id ); return absint( $this->shipping_class_id );
} }
@ -1000,11 +984,11 @@ abstract class WC_Product {
// Get tags // Get tags
$terms = wp_get_post_terms($this->id, 'product_tag'); $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 // Get categories
$terms = wp_get_post_terms($this->id, 'product_cat'); $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 // Don't bother if none are set
if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array(); if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array();
@ -1016,22 +1000,22 @@ abstract class WC_Product {
// Get the posts // Get the posts
$related_posts = get_posts( apply_filters('woocommerce_product_related_posts', array( $related_posts = get_posts( apply_filters('woocommerce_product_related_posts', array(
'orderby' => 'rand', 'orderby' => 'rand',
'posts_per_page'=> $limit, 'posts_per_page' => $limit,
'post_type' => 'product', 'post_type' => 'product',
'fields' => 'ids', 'fields' => 'ids',
'meta_query' => $meta_query, 'meta_query' => $meta_query,
'tax_query' => array( 'tax_query' => array(
'relation' => 'OR', 'relation' => 'OR',
array( array(
'taxonomy' => 'product_cat', 'taxonomy' => 'product_cat',
'field' => 'id', 'field' => 'id',
'terms' => $cats_array 'terms' => $cats_array
), ),
array( array(
'taxonomy' => 'product_tag', 'taxonomy' => 'product_tag',
'field' => 'id', 'field' => 'id',
'terms' => $tags_array 'terms' => $tags_array
) )
) )
) ) ); ) ) );
@ -1069,7 +1053,6 @@ abstract class WC_Product {
} }
} }
return false; return false;
} }
@ -1084,7 +1067,7 @@ abstract class WC_Product {
if ( ! is_array( $this->attributes ) ) { 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] )); $this->attributes = maybe_unserialize( maybe_unserialize( $this->product_custom_fields['_product_attributes'][0] ));
else else
$this->attributes = array(); $this->attributes = array();
@ -1151,24 +1134,24 @@ abstract class WC_Product {
* @return string * @return string
*/ */
function get_dimensions() { function get_dimensions() {
if ( ! $this->dimensions ) : if ( ! $this->dimensions ) {
$this->dimensions = ''; $dimensions = array();
// Show length if ( $this->length )
if ( $this->length ) { $dimensions[] = $this->length;
$this->dimensions = $this->length;
// Show width also if ( $this->width )
if ( $this->width ) { $dimensions[] = $this->width;
$this->dimensions .= ' x ' . $this->width;
// Show height also if ( $this->height )
if ( $this->height ) { $dimensions[] = $this->height;
$this->dimensions .= ' x ' . $this->height;
} $this->dimensions = implode( ' x ', $dimensions );
}
// Append the unit if ( ! empty( $this->dimensions ) )
$this->dimensions .= ' '.get_option('woocommerce_dimension_unit'); $this->dimensions .= ' ' . get_option( 'woocommerce_dimension_unit' );
}
endif; }
return $this->dimensions; return $this->dimensions;
} }
@ -1181,7 +1164,7 @@ abstract class WC_Product {
*/ */
function list_attributes() { function list_attributes() {
woocommerce_get_template( 'single-product/product-attributes.php', array( woocommerce_get_template( 'single-product/product-attributes.php', array(
'product' => $this 'product' => $this
) ); ) );
} }
@ -1208,40 +1191,4 @@ abstract class WC_Product {
return $image; 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 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 * @class WC_Product_External
* @version 1.7.0 * @version 1.7.0
@ -11,7 +11,13 @@
*/ */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 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. * __construct function.
@ -22,38 +28,23 @@ class WC_Product_External extends WC_Product {
*/ */
function __construct( $product, $args ) { function __construct( $product, $args ) {
parent::__construct( $product ); parent::__construct( $product, $args );
$this->product_type = 'external'; $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( $this->load_product_data( array(
'sku' => '', 'product_url' => '',
'downloadable' => 'no', 'button_text' => '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'
) ); ) );
$this->check_sale_price();
} }
/** /**
@ -65,4 +56,24 @@ class WC_Product_External extends WC_Product {
function is_purchasable() { function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', false, $this ); 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 * The WooCommerce product factory creating the right product object
* *
* @class WC_Product_Factory * @class WC_Product_Factory
* @version 1.7 * @version 1.7.0
* @package WooCommerce/Classes * @package WooCommerce/Classes
* @author WooThemes * @author WooThemes
*/ */
class WC_Product_Factory { 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() ) { public function get_product( $the_product = false, $args = array() ) {
global $post; global $post;
@ -24,18 +29,18 @@ class WC_Product_Factory {
$the_product = get_post( $the_product ); $the_product = get_post( $the_product );
} }
$product_id = absint( $the_product->ID ); $product_id = absint( $the_product->ID );
$post_type = $the_product->post_type; $post_type = $the_product->post_type;
if ( 'product_variation' == $post_type ) { if ( 'product_variation' == $post_type ) {
$product_type = 'variation'; $product_type = 'variation';
} else { } else {
$terms = get_the_terms( $product_id, 'product_type' ); $terms = get_the_terms( $product_id, 'product_type' );
$product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple'; $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. // 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 ) ) { if ( class_exists( $classname ) ) {
return new $classname( $the_product, $args ); 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_type = 'grouped';
$this->product_custom_fields = get_post_custom( $this->id ); $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 // Load data from custom fields
$this->load_product_data( array( $this->load_product_data( array(
'sku' => '', 'sku' => '',
'downloadable' => 'no', 'price' => '',
'virtual' => 'no', 'visibility' => 'hidden',
'price' => '', 'sale_price' => '',
'visibility' => 'hidden', 'regular_price' => '',
'stock' => 0, 'upsell_ids' => array(),
'stock_status' => 'instock', 'crosssell_ids' => array(),
'backorders' => 'no', 'featured' => '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'
) ); ) );
$this->check_sale_price();
} }
/** /**
@ -153,18 +146,20 @@ class WC_Product_Grouped extends WC_Product {
* @return bool * @return bool
*/ */
function is_on_sale() { 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 ); $sale_price = get_post_meta( $child_id, '_sale_price', true );
if ( $sale_price!=="" && $sale_price >= 0 ) return true; if ( $sale_price !== "" && $sale_price >= 0 )
endforeach; 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; return false;
} }
@ -191,7 +186,8 @@ class WC_Product_Grouped extends WC_Product {
$child_prices = array(); $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 ); $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 ); 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 * Simple Product Class
* *
* The default product type kinda product * The default product type kinda product.
* *
* @class WC_Product_Simple * @class WC_Product_Simple
* @version 1.7.0 * @version 1.7.0
@ -25,34 +25,107 @@ class WC_Product_Simple extends WC_Product {
parent::__construct( $product ); parent::__construct( $product );
$this->product_type = 'simple'; $this->product_type = 'simple';
$this->product_custom_fields = get_post_custom( $this->id );
// Load data from custom fields // Load data from custom fields
$this->load_product_data( array( $this->load_product_data( array(
'sku' => '', 'sku' => '',
'downloadable' => 'no', 'downloadable' => 'no',
'virtual' => 'no', 'virtual' => 'no',
'price' => '', 'price' => '',
'visibility' => 'hidden', 'visibility' => 'hidden',
'stock' => 0, 'stock' => 0,
'stock_status' => 'instock', 'stock_status' => 'instock',
'backorders' => 'no', 'backorders' => 'no',
'manage_stock' => 'no', 'manage_stock' => 'no',
'sale_price' => '', 'sale_price' => '',
'regular_price' => '', 'regular_price' => '',
'weight' => '', 'weight' => '',
'length' => '', 'length' => '',
'width' => '', 'width' => '',
'height' => '', 'height' => '',
'tax_status' => 'taxable', 'tax_status' => 'taxable',
'tax_class' => '', 'tax_class' => '',
'upsell_ids' => array(), 'upsell_ids' => array(),
'crosssell_ids' => array(), 'crosssell_ids' => array(),
'sale_price_dates_from' => '', 'sale_price_dates_from' => '',
'sale_price_dates_to' => '', 'sale_price_dates_to' => '',
'featured' => 'no' 'featured' => 'no',
'sold_individually' => 'no'
) ); ) );
$this->check_sale_price(); $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 // Load data from custom fields
$this->load_product_data( array( $this->load_product_data( array(
'sku' => '', 'sku' => '',
'downloadable' => 'no', 'price' => '',
'virtual' => 'no', 'sale_price' => '',
'price' => '', 'regular_price' => '',
'visibility' => 'hidden', 'visibility' => 'hidden',
'stock' => 0, 'stock' => 0,
'stock_status' => 'instock', 'stock_status' => 'instock',
'backorders' => 'no', 'backorders' => 'no',
'manage_stock' => 'no', 'manage_stock' => 'no',
'sale_price' => '', 'weight' => '',
'regular_price' => '', 'length' => '',
'weight' => '', 'width' => '',
'length' => '', 'height' => '',
'width' => '', 'tax_status' => 'taxable',
'height' => '', 'tax_class' => '',
'tax_status' => 'taxable', 'upsell_ids' => array(),
'tax_class' => '', 'crosssell_ids' => array(),
'upsell_ids' => array(), 'featured' => 'no',
'crosssell_ids' => array(), 'min_variation_price' => '',
'sale_price_dates_from' => '', 'max_variation_price' => '',
'sale_price_dates_to' => '', 'min_variation_regular_price' => '',
'featured' => 'no', 'max_variation_regular_price' => '',
'min_variation_price' => '', 'min_variation_sale_price' => '',
'max_variation_price' => '', 'max_variation_sale_price' => '',
'min_variation_regular_price' => '', 'sold_individually' => 'no'
'max_variation_regular_price' => '',
'min_variation_sale_price' => '',
'max_variation_sale_price' => '',
) ); ) );
$this->check_sale_price();
} }
/** /**
@ -116,7 +111,6 @@ class WC_Product_Variable extends WC_Product {
set_transient( $transient_name, $this->total_stock ); set_transient( $transient_name, $this->total_stock );
} }
} }
return apply_filters( 'woocommerce_stock_amount', $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 * @return object WC_Product or WC_Product_variation
*/ */
function get_child( $child_id ) { 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 * @return bool
*/ */
function is_on_sale() { 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 ); $sale_price = get_post_meta( $child_id, '_sale_price', true );
if ( $sale_price!=="" && $sale_price >= 0 ) return true; if ( $sale_price !== "" && $sale_price >= 0 )
endforeach; 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; return false;
} }

View File

@ -5,7 +5,7 @@
* The WooCommerce product variation class handles product variation data. * The WooCommerce product variation class handles product variation data.
* *
* @class WC_Product_Variation * @class WC_Product_Variation
* @version 1.6.4 * @version 1.7.0
* @package WooCommerce/Classes * @package WooCommerce/Classes
* @author WooThemes * @author WooThemes
*/ */
@ -56,6 +56,12 @@ class WC_Product_Variation extends WC_Product {
/** @var bool True if the variation has a tax class. */ /** @var bool True if the variation has a tax class. */
var $variation_has_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 * Loads all product data from custom fields
* *
@ -66,130 +72,130 @@ class WC_Product_Variation extends WC_Product {
*/ */
function __construct( $variation, $args = array() ) { function __construct( $variation, $args = array() ) {
$this->product_type = 'variable';
if ( is_object( $variation ) ) { if ( is_object( $variation ) ) {
$this->variation_id = absint( $variation->ID ); $this->variation_id = absint( $variation->ID );
} else { } else {
$this->variation_id = absint( $variation ); $this->variation_id = absint( $variation );
} }
/* Get main product data from parent */ /* 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 ); $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; 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(); $this->variation_data = array();
foreach ( $product_custom_fields as $name => $value ) { foreach ( $this->product_custom_fields as $name => $value ) {
if ( ! strstr( $name, 'attribute_' ) ) continue; if ( ! strstr( $name, 'attribute_' ) ) continue;
$this->variation_data[ $name ] = $value[0]; $this->variation_data[ $name ] = $value[0];
} }
$parent_custom_fields = ! empty( $args['meta'] ) ? $args['meta'] : get_post_custom( $this->id ); // Now get variation meta and override the parent variable product
// 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';
$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; $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 */ /* 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->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->variation_has_stock = true;
$this->manage_stock = 'yes'; $this->manage_stock = 'yes';
$this->stock = $product_custom_fields['_stock'][0]; $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->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->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->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->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'; $this->downloadable = 'yes';
} else { } else {
$this->downloadable = 'no'; $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'; $this->virtual = 'yes';
} else { } else {
$this->virtual = 'no'; $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->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->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->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->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 // Backwards compat for prices
if ( $this->variation_has_price && ! $this->variation_has_regular_price ) { if ( $this->variation_has_price && ! $this->variation_has_regular_price ) {
update_post_meta( $this->variation_id, '_regular_price', $this->price ); update_post_meta( $this->variation_id, '_regular_price', $this->price );
$this->variation_has_regular_price = true; $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 ) { if ( $this->variation_has_sale_price && $this->sale_price < $this->regular_price ) {
update_post_meta( $this->variation_id, '_price', $this->sale_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] ) ) if ( isset( $this->product_custom_fields['_sale_price_dates_from'][0] ) )
$this->sale_price_dates_from = $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] ) ) if ( isset( $this->product_custom_fields['_sale_price_dates_to'][0] ) )
$this->sale_price_dates_from = $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; $this->total_stock = $this->stock;
@ -266,7 +272,7 @@ class WC_Product_Variation extends WC_Product {
$price = ''; $price = '';
if ( $this->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 .= '<del>' . woocommerce_price( $this->regular_price ) . '</del> <ins>' . woocommerce_price( $this->sale_price ) . '</ins>';
$price = apply_filters( 'woocommerce_variation_sale_price_html', $price, $this ); $price = apply_filters( 'woocommerce_variation_sale_price_html', $price, $this );
} else { } else {
@ -457,7 +463,7 @@ class WC_Product_Variation extends WC_Product {
update_post_meta( $this->variation_id, '_price', $this->price ); update_post_meta( $this->variation_id, '_price', $this->price );
// Variable product prices and sale status are affected by children // 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', '' ); update_post_meta( $this->variation_id, '_sale_price_dates_to', '' );
// Variable product prices and sale status are affected by children // 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 ) : ?> <?php foreach ( $grouped_products as $child_product ) : ?>
<tr> <tr>
<td> <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 ); <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>
$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>
<?php elseif ( ! $quantites_required ) : ?> <?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', 'sanitize_text_field' );
add_filter( 'woocommerce_coupon_code', 'strtolower' ); // Coupons case-insensitive by default 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. * 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() { function woocommerce_external_add_to_cart() {
global $product; global $product;
$product_url = get_post_meta( $product->id, '_product_url', true ); if ( ! $product->get_product_url() ) return;
$button_text = get_post_meta( $product->id, '_button_text', true );
if ( ! $product_url ) return;
woocommerce_get_template( 'single-product/add-to-cart/external.php', array( woocommerce_get_template( 'single-product/add-to-cart/external.php', array(
'product_url' => $product_url, 'product_url' => $product->get_product_url(),
'button_text' => ( $button_text ) ? $button_text : __( 'Buy product', 'woocommerce' ) , 'button_text' => $product->get_button_text()
) ); ) );
} }
} }