Separate classes per product type, with new get_product function and hooks to change the classes which get loaded. #1497

This commit is contained in:
Mike Jolley 2012-11-21 18:07:45 +00:00
parent cb4f34a177
commit 75ca794bba
20 changed files with 1128 additions and 641 deletions

View File

@ -115,7 +115,7 @@ add_filter('manage_edit-product_columns', 'woocommerce_edit_product_columns');
*/
function woocommerce_custom_product_columns( $column ) {
global $post, $woocommerce;
$product = new WC_Product($post->ID);
$product = get_product($post);
switch ($column) {
case "thumb" :
@ -712,7 +712,7 @@ function woocommerce_admin_product_quick_edit_save( $post_id, $post ) {
global $woocommerce, $wpdb;
$product = new WC_Product( $post_id );
$product = get_product( $post );
// Save fields
if(isset($_POST['_sku'])) update_post_meta($post_id, '_sku', esc_html(stripslashes($_POST['_sku'])));
@ -1003,7 +1003,7 @@ function woocommerce_admin_product_bulk_edit_save( $post_id, $post ) {
global $woocommerce, $wpdb;
$product = new WC_Product( $post_id );
$product = get_product( $post );
// Save fields
if ( ! empty( $_REQUEST['change_weight'] ) && isset( $_REQUEST['_weight'] ) )

View File

@ -36,7 +36,7 @@ function woocommerce_order_downloads_meta_box() {
if ( $download_permissions && sizeof( $download_permissions ) > 0 ) foreach ( $download_permissions as $download ) {
if ( ! $product || $product->id != $download->product_id ) {
$product = new WC_Product( absint( $download->product_id ) );
$product = get_product( absint( $download->product_id ) );
$file_count = $loop = 0;
}

View File

@ -820,7 +820,7 @@ function woocommerce_process_product_meta( $post_id, $post ) {
else
update_post_meta( $post_id, '_price', stripslashes( $_POST['_regular_price'] ) );
if ( $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) )
if ( $_POST['_sale_price'] != '' && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) )
update_post_meta( $post_id, '_price', stripslashes($_POST['_sale_price']) );
if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {

View File

@ -125,10 +125,7 @@ class WC_Cart {
foreach ( $cart as $key => $values ) {
if ( $values['variation_id'] > 0 )
$_product = new WC_Product_Variation( $values['variation_id'] );
else
$_product = new WC_Product( $values['product_id'] );
$_product = get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
if ( $_product->exists() && $values['quantity'] > 0 ) {
@ -738,10 +735,7 @@ class WC_Cart {
// See if this product and its options is already in the cart
$cart_item_key = $this->find_product_in_cart( $cart_id );
if ( $variation_id > 0 )
$product_data = new WC_Product_Variation( $variation_id );
else
$product_data = new WC_Product( $product_id );
$product_data = get_product( $variation_id ? $variation_id : $product_id );
// Force quantity to 1 if sold individually
if ( $product_data->is_sold_individually() )

View File

@ -488,7 +488,7 @@ class WC_Customer {
if ( ! $_product || $_product->id != $result->product_id ) :
// new product
$file_number = 0;
$_product = new WC_Product( $result->product_id );
$_product = get_product( $result->product_id );
endif;
if ( ! $_product->exists() ) continue;

View File

@ -850,12 +850,7 @@ class WC_Order {
* @return WC_Product
*/
function get_product_from_item( $item ) {
if (isset($item['variation_id']) && $item['variation_id']>0) :
$_product = new WC_Product_Variation( $item['variation_id'] );
else :
$_product = new WC_Product( $item['product_id'] );
endif;
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
return $_product;
@ -1126,7 +1121,7 @@ class WC_Order {
global $wpdb;
$download_file = $variation_id > 0 ? $variation_id : $product_id;
$_product = new WC_Product( $download_file );
$_product = get_product( $download_file );
$user_email = $this->billing_email;

View File

@ -0,0 +1,72 @@
<?php
/**
* External Product Class
*
* External products cannot be bought; they link offsite.
*
* @class WC_Product
* @version 1.7.0
* @package WooCommerce/Classes/Products
* @author WooThemes
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_External extends WC_Product {
/**
* __construct function.
*
* @access public
* @param mixed $product
*/
function __construct( $product ) {
if ( is_object( $product ) ) {
$this->id = absint( $product->ID );
$this->post = $product;
} else {
$this->id = absint( $product );
}
$this->product_type = 'external';
$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(),
'sale_price_dates_from' => '',
'sale_price_dates_to' => '',
'featured' => 'no'
) );
$this->check_sale_price();
}
/**
* Returns false if the product cannot be bought.
*
* @access public
* @return cool
*/
function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', false, $this );
}
}

View File

@ -0,0 +1,290 @@
<?php
/**
* Grouped Product Class
*
* Grouped products cannot be purchased - they are wrappers for other products.
*
* @class WC_Product_Grouped
* @version 1.7.0
* @package WooCommerce/Classes/Products
* @author WooThemes
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_Grouped extends WC_Product {
/** @var array Array of child products/posts/variations. */
var $children;
/** @var string The product's total stock, including that of its children. */
var $total_stock;
/**
* __construct function.
*
* @access public
* @param mixed $product
*/
function __construct( $product ) {
if ( is_object( $product ) ) {
$this->id = absint( $product->ID );
$this->post = $product;
} else {
$this->id = absint( $product );
}
$this->product_type = 'grouped';
$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(),
'sale_price_dates_from' => '',
'sale_price_dates_to' => '',
'featured' => 'no'
) );
$this->check_sale_price();
}
/**
* Get total stock.
*
* This is the stock of parent and children combined.
*
* @access public
* @return int
*/
function get_total_stock() {
if ( is_null( $this->total_stock ) ) {
$transient_name = 'wc_product_total_stock_' . $this->id;
if ( false === ( $this->total_stock = get_transient( $transient_name ) ) ) {
$this->total_stock = $this->stock;
if ( sizeof( $this->get_children() ) > 0 ) {
foreach ($this->get_children() as $child_id) {
$stock = get_post_meta( $child_id, '_stock', true );
if ( $stock != '' ) {
$this->total_stock += intval( $stock );
}
}
}
set_transient( $transient_name, $this->total_stock );
}
}
return apply_filters( 'woocommerce_stock_amount', $this->total_stock );
}
/**
* Return the products children posts.
*
* @access public
* @return array
*/
function get_children() {
if ( ! is_array( $this->children ) ) {
$this->children = array();
$transient_name = 'wc_product_children_ids_' . $this->id;
if ( false === ( $this->children = get_transient( $transient_name ) ) ) {
$this->children = get_posts( 'post_parent=' . $this->id . '&post_type=product&orderby=menu_order&order=ASC&fields=ids&post_status=any&numberposts=-1' );
set_transient( $transient_name, $this->children );
}
}
return (array) $this->children;
}
/**
* get_child function.
*
* @access public
* @param mixed $child_id
* @return object WC_Product or WC_Product_variation
*/
function get_child( $child_id ) {
return get_product( $child_id );
}
/**
* Returns whether or not the product has any child product.
*
* @access public
* @return bool
*/
function has_child() {
return sizeof( $this->get_children() ) ? true : false;
}
/**
* Returns whether or not the product is on sale.
*
* @access public
* @return bool
*/
function is_on_sale() {
if ($this->has_child()) :
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;
else :
if ( $this->sale_price && $this->sale_price==$this->price ) return true;
endif;
return false;
}
/**
* Returns false if the product cannot be bought.
*
* @access public
* @return cool
*/
function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', false, $this );
}
/**
* Returns the price in html format.
*
* @access public
* @param string $price (default: '')
* @return string
*/
function get_price_html( $price = '' ) {
$child_prices = array();
foreach ( $this->get_children() as $child_id ) $child_prices[] = get_post_meta( $child_id, '_price', true );
$child_prices = array_unique( $child_prices );
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
} else {
$min_price = '';
}
if ( sizeof( $child_prices ) > 1 ) $price .= $this->get_price_html_from_text();
$price .= woocommerce_price( $min_price );
$price = apply_filters( 'woocommerce_grouped_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

@ -0,0 +1,524 @@
<?php
/**
* Variable Product Class
*
* The WooCommerce product class handles individual product data.
*
* @class WC_Product_Variable
* @version 1.7.0
* @package WooCommerce/Classes/Products
* @author WooThemes
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_Variable extends WC_Product {
/** @var array Array of child products/posts/variations. */
var $children;
/** @var string The product's total stock, including that of its children. */
var $total_stock;
/** @var string Used for variation prices. */
var $min_variation_price;
/** @var string Used for variation prices. */
var $max_variation_price;
/** @var string Used for variation prices. */
var $min_variation_regular_price;
/** @var string Used for variation prices. */
var $max_variation_regular_price;
/** @var string Used for variation prices. */
var $min_variation_sale_price;
/** @var string Used for variation prices. */
var $max_variation_sale_price;
/**
* __construct function.
*
* @access public
* @param mixed $product
*/
function __construct( $product ) {
if ( is_object( $product ) ) {
$this->id = absint( $product->ID );
$this->post = $product;
} else {
$this->id = absint( $product );
}
$this->product_type = 'variable';
$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(),
'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' => '',
) );
$this->check_sale_price();
}
/**
* Get total stock.
*
* This is the stock of parent and children combined.
*
* @access public
* @return int
*/
function get_total_stock() {
if ( is_null( $this->total_stock ) ) {
$transient_name = 'wc_product_total_stock_' . $this->id;
if ( false === ( $this->total_stock = get_transient( $transient_name ) ) ) {
$this->total_stock = $this->stock;
if ( sizeof( $this->get_children() ) > 0 ) {
foreach ($this->get_children() as $child_id) {
$stock = get_post_meta( $child_id, '_stock', true );
if ( $stock != '' ) {
$this->total_stock += intval( $stock );
}
}
}
set_transient( $transient_name, $this->total_stock );
}
}
return apply_filters( 'woocommerce_stock_amount', $this->total_stock );
}
/**
* Reduce stock level of the product.
*
* @access public
* @param int $by (default: 1) Amount to reduce by.
* @return int Stock
*/
function reduce_stock( $by = 1 ) {
global $woocommerce;
if ( $this->managing_stock() ) {
$this->stock = $this->stock - $by;
$this->total_stock = $this->get_total_stock() - $by;
update_post_meta($this->id, '_stock', $this->stock);
// Out of stock attribute
if ($this->managing_stock() && !$this->backorders_allowed() && $this->get_total_stock()<=0) :
update_post_meta($this->id, '_stock_status', 'outofstock');
endif;
$woocommerce->clear_product_transients( $this->id ); // Clear transient
return apply_filters( 'woocommerce_stock_amount', $this->stock );
}
}
/**
* Increase stock level of the product.
*
* @access public
* @param int $by (default: 1) Amount to increase by
* @return int Stock
*/
function increase_stock( $by = 1 ) {
global $woocommerce;
if ($this->managing_stock()) :
$this->stock = $this->stock + $by;
$this->total_stock = $this->get_total_stock() + $by;
update_post_meta($this->id, '_stock', $this->stock);
// Out of stock attribute
if ($this->managing_stock() && ($this->backorders_allowed() || $this->get_total_stock()>0)) :
update_post_meta($this->id, '_stock_status', 'instock');
endif;
$woocommerce->clear_product_transients( $this->id ); // Clear transient
return apply_filters( 'woocommerce_stock_amount', $this->stock );
endif;
}
/**
* Return the products children posts.
*
* @access public
* @return array
*/
function get_children() {
if (!is_array($this->children)) :
$this->children = array();
$transient_name = 'wc_product_children_ids_' . $this->id;
if ( false === ( $this->children = get_transient( $transient_name ) ) ) :
$this->children = get_posts( 'post_parent=' . $this->id . '&post_type=product_variation&orderby=menu_order&order=ASC&fields=ids&post_status=any&numberposts=-1' );
set_transient( $transient_name, $this->children );
endif;
endif;
return (array) $this->children;
}
/**
* get_child function.
*
* @access public
* @param mixed $child_id
* @return object WC_Product or WC_Product_variation
*/
function get_child( $child_id ) {
return get_product( $child_id, $this->id, $this->product_custom_fields );
}
/**
* Returns whether or not the product has any child product.
*
* @access public
* @return bool
*/
function has_child() {
return sizeof( $this->get_children() ) ? true : false;
}
/**
* Returns whether or not the product is on sale.
*
* @access public
* @return bool
*/
function is_on_sale() {
if ($this->has_child()) :
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;
else :
if ( $this->sale_price && $this->sale_price==$this->price ) return true;
endif;
return false;
}
/**
* Returns the price in html format.
*
* @access public
* @param string $price (default: '')
* @return string
*/
function get_price_html( $price = '' ) {
// Ensure variation prices are synced with variations
if ( $this->min_variation_price === '' || $this->min_variation_regular_price === '' ) {
$this->variable_product_sync();
$this->price = $this->min_variation_price;
}
// Get the price
if ($this->price > 0) {
if ( $this->is_on_sale() && isset( $this->min_variation_price ) && $this->min_variation_regular_price !== $this->get_price() ) {
if ( ! $this->min_variation_price || $this->min_variation_price !== $this->max_variation_price )
$price .= $this->get_price_html_from_text();
$price .= $this->get_price_html_from_to( $this->min_variation_regular_price, $this->get_price() );
$price = apply_filters( 'woocommerce_variable_sale_price_html', $price, $this );
} else {
if ( ! $this->min_variation_price || $this->min_variation_price !== $this->max_variation_price )
$price .= $this->get_price_html_from_text();
$price .= woocommerce_price( $this->get_price() );
$price = apply_filters('woocommerce_variable_price_html', $price, $this);
}
} elseif ($this->price === '' ) {
$price = apply_filters('woocommerce_variable_empty_price_html', '', $this);
} elseif ($this->price == 0 ) {
if ( $this->is_on_sale() && isset( $this->min_variation_regular_price ) && $this->min_variation_regular_price !== $this->get_price() ) {
if ( ! $this->min_variation_price || $this->min_variation_price !== $this->max_variation_price )
$price .= $this->get_price_html_from_text();
$price .= $this->get_price_html_from_to( $this->min_variation_regular_price, __( 'Free!', 'woocommerce' ) );
$price = apply_filters( 'woocommerce_variable_free_sale_price_html', $price, $this );
} else {
if ( ! $this->min_variation_price || $this->min_variation_price !== $this->max_variation_price )
$price .= $this->get_price_html_from_text();
$price .= __( 'Free!', 'woocommerce' );
$price = apply_filters( 'woocommerce_variable_free_price_html', $price, $this );
}
}
return apply_filters( 'woocommerce_get_price_html', $price, $this );
}
/**
* Return an array of attributes used for variations, as well as their possible values.
*
* @access public
* @return array of attributes and their available values
*/
function get_variation_attributes() {
$variation_attributes = array();
if ( ! $this->has_child() )
return $variation_attributes;
$attributes = $this->get_attributes();
foreach ( $attributes as $attribute ) {
if ( ! $attribute['is_variation'] )
continue;
$values = array();
$attribute_field_name = 'attribute_' . sanitize_title( $attribute['name'] );
foreach ( $this->get_children() as $child_id ) {
if ( get_post_status( $child_id ) != 'publish' )
continue; // Disabled
$child = $this->get_child( $child_id );
$child_variation_attributes = $child->get_variation_attributes();
foreach ( $child_variation_attributes as $name => $value )
if ( $name == $attribute_field_name )
$values[] = $value;
}
// empty value indicates that all options for given attribute are available
if ( in_array( '', $values ) ) {
$values = array();
// Get all options
if ( $attribute['is_taxonomy'] ) {
$post_terms = wp_get_post_terms( $this->id, $attribute['name'] );
foreach ( $post_terms as $term )
$values[] = $term->slug;
} else {
$values = explode( '|', $attribute['value'] );
}
$values = array_unique( array_map( 'trim', $values ) );
// Order custom attributes (non taxonomy) as defined
} else {
if ( ! $attribute['is_taxonomy'] ) {
$options = array_map( 'trim', explode( '|', $attribute['value'] ) );
$values = array_intersect( $options, $values );
}
}
$variation_attributes[ $attribute['name'] ] = array_unique( $values );
}
return $variation_attributes;
}
/**
* If set, get the default attributes for a variable product.
*
* @access public
* @return array
*/
function get_variation_default_attributes() {
$default = isset( $this->product_custom_fields['_default_attributes'][0] ) ? $this->product_custom_fields['_default_attributes'][0] : '';
return apply_filters( 'woocommerce_product_default_attributes', (array) maybe_unserialize( $default ), $this );
}
/**
* Get an array of available variations for the current product.
*
* @access public
* @return array
*/
function get_available_variations() {
$available_variations = array();
foreach ( $this->get_children() as $child_id ) {
$variation = $this->get_child( $child_id );
if ( $variation instanceof WC_Product_Variation ) {
if ( get_post_status( $variation->get_variation_id() ) != 'publish' || ! $variation->is_visible() )
continue; // Disabled or hidden
$variation_attributes = $variation->get_variation_attributes();
$availability = $variation->get_availability();
$availability_html = empty( $availability['availability'] ) ? '' : apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">'. wp_kses_post( $availability['availability'] ).'</p>', wp_kses_post( $availability['availability'] ) );
if ( has_post_thumbnail( $variation->get_variation_id() ) ) {
$attachment_id = get_post_thumbnail_id( $variation->get_variation_id() );
$attachment = wp_get_attachment_image_src( $attachment_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image = $attachment ? current( $attachment ) : '';
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
$image_link = $attachment ? current( $attachment ) : '';
$image_title = get_the_title( $attachment_id );
} else {
$image = $image_link = $image_title = '';
}
$available_variations[] = apply_filters( 'woocommerce_available_variation', array(
'variation_id' => $child_id,
'attributes' => $variation_attributes,
'image_src' => $image,
'image_link' => $image_link,
'image_title' => $image_title,
'price_html' => $this->min_variation_price != $this->max_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'availability_html' => $availability_html,
'sku' => $variation->get_sku(),
'weight' => $variation->get_weight() . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) ),
'dimensions' => $variation->get_dimensions(),
'min_qty' => 1,
'max_qty' => $this->backorders_allowed() ? '' : $variation->stock,
'backorders_allowed' => $this->backorders_allowed(),
'is_in_stock' => $variation->is_in_stock(),
'is_downloadable' => $variation->is_downloadable() ,
'is_virtual' => $variation->is_virtual(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
), $this, $variation );
}
}
return $available_variations;
}
/**
* Sync variable product prices with the childs lowest/highest prices.
*
* @access public
* @return void
*/
function variable_product_sync() {
global $woocommerce;
$children = get_posts( array(
'post_parent' => $this->id,
'posts_per_page'=> -1,
'post_type' => 'product_variation',
'fields' => 'ids',
'post_status' => 'publish'
));
$this->min_variation_price = $this->min_variation_regular_price = $this->min_variation_sale_price = $this->max_variation_price = $this->max_variation_regular_price = $this->max_variation_sale_price = '';
if ($children) {
foreach ( $children as $child ) {
$child_price = get_post_meta( $child, '_price', true );
$child_regular_price = get_post_meta( $child, '_regular_price', true );
$child_sale_price = get_post_meta( $child, '_sale_price', true );
// Regular prices
if ( ! is_numeric( $this->min_variation_regular_price ) || $child_regular_price < $this->min_variation_regular_price )
$this->min_variation_regular_price = $child_regular_price;
if ( ! is_numeric( $this->max_variation_regular_price ) || $child_regular_price > $this->max_variation_regular_price )
$this->max_variation_regular_price = $child_regular_price;
// Sale prices
if ( $child_price == $child_sale_price ) {
if ( $child_sale_price !== '' && ( ! is_numeric( $this->min_variation_sale_price ) || $child_sale_price < $this->min_variation_sale_price ) )
$this->min_variation_sale_price = $child_sale_price;
if ( $child_sale_price !== '' && ( ! is_numeric( $this->max_variation_sale_price ) || $child_sale_price > $this->max_variation_sale_price ) )
$this->max_variation_sale_price = $child_sale_price;
}
}
$this->min_variation_price = $this->min_variation_sale_price === '' || $this->min_variation_regular_price < $this->min_variation_sale_price ? $this->min_variation_regular_price : $this->min_variation_sale_price;
$this->max_variation_price = $this->max_variation_sale_price === '' || $this->max_variation_regular_price > $this->max_variation_sale_price ? $this->max_variation_regular_price : $this->max_variation_sale_price;
}
update_post_meta( $this->id, '_price', $this->min_variation_price );
update_post_meta( $this->id, '_min_variation_price', $this->min_variation_price );
update_post_meta( $this->id, '_max_variation_price', $this->max_variation_price );
update_post_meta( $this->id, '_min_variation_regular_price', $this->min_variation_regular_price );
update_post_meta( $this->id, '_max_variation_regular_price', $this->max_variation_regular_price );
update_post_meta( $this->id, '_min_variation_sale_price', $this->min_variation_sale_price );
update_post_meta( $this->id, '_max_variation_sale_price', $this->max_variation_sale_price );
$woocommerce->clear_product_transients( $this->id );
}
}

View File

@ -65,9 +65,13 @@ class WC_Product_Variation extends WC_Product {
* @param array $parent_custom_fields (default: '') Array of the parent products meta data
* @return void
*/
function __construct( $variation_id, $parent_id = '', $parent_custom_fields = '' ) {
function __construct( $variation, $parent_id = '', $parent_custom_fields = '' ) {
$this->variation_id = intval( $variation_id );
if ( is_object( $variation ) ) {
$this->variation_id = absint( $variation->ID );
} else {
$this->variation_id = absint( $variation );
}
$product_custom_fields = get_post_custom( $this->variation_id );
@ -324,7 +328,7 @@ class WC_Product_Variation extends WC_Product {
if ( ! $this->is_in_stock() ) {
// Check parent
$parent_product = new WC_Product( $this->id );
$parent_product = get_product( $this->id );
// Only continue if the parent has backorders off
if ( ! $parent_product->backorders_allowed() && $parent_product->get_total_stock() <= 0 ) {

File diff suppressed because it is too large Load Diff

View File

@ -192,7 +192,7 @@ class ShareYourCartWooCommerce extends ShareYourCartWordpressPlugin{
}
private function _getProductDetails($product_id){
$product = new WC_Product($product_id);
$product = get_product($product_id);
//WooCommerce actually echoes the image
ob_start();

View File

@ -415,11 +415,11 @@ function woocommerce_product_add_to_cart( $atts ) {
} elseif ($product_data->post_type=='product_variation') {
$product = new WC_Product( $product_data->post_parent );
$product = get_product( $product_data->post_parent );
$GLOBALS['product'] = $product;
$variation = new WC_Product_Variation( $product_data->ID );
$variation = get_product( $product_data );
ob_start();
?>
@ -474,7 +474,7 @@ function woocommerce_product_add_to_cart_url( $atts ){
if ($product_data->post_type!=='product') return;
$_product = new WC_Product( $product_data->ID );
$_product = get_product( $product_data );
return esc_url( $_product->add_to_cart_url() );
}

View File

@ -11,7 +11,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product;
if ( ! $product->is_purchasable() && ! in_array( $product->product_type, array( 'external', 'grouped' ) ) ) return;
if ( ! $product->is_purchasable() ) return;
?>
<?php if ( ! $product->is_in_stock() ) : ?>

View File

@ -40,11 +40,7 @@ $order = new WC_Order( $order_id );
foreach($order->get_items() as $item) :
if (isset($item['variation_id']) && $item['variation_id'] > 0) :
$_product = new WC_Product_Variation( $item['variation_id'] );
else :
$_product = new WC_Product( $item['product_id'] );
endif;
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
echo '
<tr class = "' . esc_attr( apply_filters('woocommerce_order_table_item_class', 'order_table_item', $item, $order ) ) . '">

View File

@ -80,7 +80,7 @@ class WooCommerce_Widget_Recent_Reviews extends WP_Widget {
foreach ( (array) $comments as $comment) {
$_product = new WC_Product( $comment->comment_post_ID );
$_product = get_product( $comment->comment_post_ID );
$star_size = intval( apply_filters( 'woocommerce_star_rating_size_recent_reviews', 16 ) );

View File

@ -513,7 +513,7 @@ function woocommerce_link_all_variations() {
$variations = array();
$_product = new WC_Product( $post_id );
$_product = get_product( $post_id );
// Put variation attributes into an array
foreach ( $_product->get_attributes() as $attribute ) {
@ -696,7 +696,7 @@ function woocommerce_grant_access_to_download() {
$file_count = 0;
$order = new WC_Order( $order_id );
$product = new WC_Product( $product_id );
$product = get_product( $product_id );
$user_email = sanitize_email( $order->billing_email );
@ -839,10 +839,7 @@ function woocommerce_ajax_add_order_item() {
if ( ! $post || ( $post->post_type !== 'product' && $post->post_type !== 'product_variation' ) )
die();
if ( $post->post_type != "product" )
$_product = new WC_Product_Variation( $post->ID );
else
$_product = new WC_Product( $post->ID );
$_product = get_product( $post->ID );
$order = new WC_Order( $order_id );
$class = 'new_row';
@ -1199,7 +1196,7 @@ function woocommerce_calc_line_taxes() {
// Get product details
if ( get_post_type( $item_id ) == 'product' ) {
$_product = new WC_Product( $item_id );
$_product = get_product( $item_id );
$item_tax_status = $_product->get_tax_status();
} else {
$item_tax_status = 'taxable';

View File

@ -19,6 +19,52 @@ 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
/**
* Main function for returning products.
*
* @access public
* @param mixed $the_product Post object or post ID of the product.
* @param string $parent_id (default: '') Used when calling variations
* @param string $meta (default: '') Used when calling variations
* @return void
*/
function get_product( $the_product = false, $parent_id = '', $meta = '' ) {
global $post;
if ( false === $the_product )
$the_product = $post;
elseif ( is_numeric( $the_product ) )
$the_product = get_post( $the_product );
$product_id = absint( $the_product->ID );
$post_type = $the_product->post_type;
if ( $post_type == 'product_variation' ) {
// Filter classname so that the class can be overridden if extended.
$classname = apply_filters( 'woocommerce_product_variation_class', 'WC_Product_Variation', $product_id );
if ( class_exists( $classname ) ) {
return new $classname( $the_product, $parent_id, $meta );
} else {
// Use simple
return new WC_Product_Variation( $the_product, $parent_id, $meta );
}
} else {
$terms = get_the_terms( $product_id, 'product_type' );
$product_type = 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_' . $product_type, $product_type, $post_type, $product_id );
if ( class_exists( $classname ) ) {
return new $classname( $the_product );
} else {
// Use simple
return new WC_Product( $the_product );
}
}
}
/**
* woocommerce_get_dimension function.
*

View File

@ -46,7 +46,7 @@ function woocommerce_redirects() {
// Redirect to the product page if we have a single product
if (is_search() && is_post_type_archive('product') && get_option('woocommerce_redirect_on_single_search_result')=='yes') {
if ($wp_query->post_count==1) {
$product = new WC_Product($wp_query->post->ID);
$product = get_product( $wp_query->post );
if ($product->is_visible()) wp_safe_redirect( get_permalink($product->id), 302 );
exit;
}
@ -246,7 +246,7 @@ function woocommerce_add_to_cart_action( $url = false ) {
$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
$was_added_to_cart = false;
$adding_to_cart = new WC_Product( $product_id );
$adding_to_cart = get_product( $product_id );
// Variable product handling
if ( $adding_to_cart->is_type( 'variable' ) ) {
@ -843,7 +843,7 @@ function woocommerce_download_product() {
$order_key = urldecode( $_GET['order'] );
$email = sanitize_email( str_replace( ' ', '+', urldecode( $_GET['email'] ) ) );
$download_id = isset( $_GET['key'] ) ? urldecode( $_GET['key'] ) : ''; // backwards compatibility for existing download URLs
$_product = new WC_Product( $product_id );
$_product = get_product( $product_id );
if ( ! is_email( $email) )
wp_die( __( 'Invalid email address.', 'woocommerce' ) . ' <a href="' . home_url() . '">' . __( 'Go to homepage &rarr;', 'woocommerce' ) . '</a>' );

View File

@ -171,8 +171,13 @@ class Woocommerce {
include( 'widgets/widget-init.php' ); // Widget classes
include( 'classes/class-wc-countries.php' ); // Defines countries and states
include( 'classes/class-wc-order.php' ); // Single order class
include( 'classes/class-wc-product.php' ); // Product class
include( 'classes/class-wc-product-external.php' ); // External product type class
include( 'classes/class-wc-product-variable.php' ); // Variable product type class
include( 'classes/class-wc-product-grouped.php' ); // Grouped product type class
include( 'classes/class-wc-product-variation.php' ); // Product variation class
include( 'classes/class-wc-tax.php' ); // Tax class
include( 'classes/class-wc-settings-api.php' ); // Settings API
@ -499,7 +504,7 @@ class Woocommerce {
if ( is_int( $post ) ) $post = get_post( $post );
if ( $post->post_type !== 'product' ) return;
unset( $GLOBALS['product'] );
$GLOBALS['product'] = new WC_Product( $post->ID );
$GLOBALS['product'] = get_product( $post );
return $GLOBALS['product'];
}