Made product loops a little more efficient

This commit is contained in:
Mike Jolley 2011-10-26 19:45:38 +01:00
parent 3a6ae6de92
commit 29a77b0fe7
4 changed files with 21 additions and 56 deletions

View File

@ -12,6 +12,7 @@
class woocommerce_product {
var $id;
var $product_custom_fields;
var $exists;
var $attributes;
var $children;
@ -49,8 +50,10 @@ class woocommerce_product {
$this->id = (int) $id;
$product_custom_fields = get_post_custom( $this->id );
$this->product_custom_fields = get_post_custom( $this->id );
$this->exists = (sizeof($this->product_custom_fields)>0) ? true : false;
// Define the data we're going to load: Key => Default value
$load_data = array(
'sku' => $this->id,
@ -78,26 +81,15 @@ class woocommerce_product {
// Load the data from the custom fields
foreach ($load_data as $key => $default) :
if (isset($product_custom_fields[$key][0]) && $product_custom_fields[$key][0]!=='') :
$this->$key = $product_custom_fields[$key][0];
else :
$this->$key = $default;
endif;
$this->$key = (isset($this->product_custom_fields[$key][0]) && $this->product_custom_fields[$key][0]!=='') ? $this->product_custom_fields[$key][0] : $default;
endforeach;
// Load serialised data, unserialise twice to fix WP bug
if (isset($product_custom_fields['product_attributes'][0])) $this->attributes = maybe_unserialize( maybe_unserialize( $product_custom_fields['product_attributes'][0] )); else $this->attributes = array();
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();
// Get product type
$terms = wp_get_object_terms( $id, 'product_type' );
if (!is_wp_error($terms) && $terms) :
$term = current($terms);
$this->product_type = $term->slug;
else :
$this->product_type = 'simple';
endif;
$this->get_children();
$terms = wp_get_object_terms( $id, 'product_type', array('fields' => 'names') );
$this->product_type = (isset($terms[0])) ? sanitize_title($terms[0]) : 'simple';
// total_stock (stock of parent and children combined)
$this->total_stock = $this->stock;
@ -113,12 +105,6 @@ class woocommerce_product {
// Check sale
$this->check_sale_price();
if ($product_custom_fields) :
$this->exists = true;
else :
$this->exists = false;
endif;
}
/**
@ -140,14 +126,14 @@ class woocommerce_product {
if ($this->is_type('variable') || $this->is_type('grouped')) :
if ($this->is_type('variable')) $child_post_type = 'product_variation'; else $child_post_type = 'product';
$child_post_type = ($this->is_type('variable')) ? 'product_variation' : 'product';
if ( $children_products =& get_children( 'post_parent='.$this->id.'&post_type='.$child_post_type.'&orderby=menu_order&order=ASC' ) ) :
if ($children_products) foreach ($children_products as $child) :
if ($this->is_type('variable')) :
$child->product = &new woocommerce_product_variation( $child->ID );
$child->product = &new woocommerce_product_variation( $child->ID, $this->id, $this->product_custom_fields );
else :
$child->product = &new woocommerce_product( $child->ID );
endif;
@ -730,7 +716,7 @@ class woocommerce_product {
if ($variation instanceof woocommerce_product_variation) {
if ($variation->variation->post_status != 'publish') continue; // Disabled
if (get_post_status( $variation->get_variation_id() ) != 'publish') continue; // Disabled
$vattributes = $variation->get_variation_attributes();

View File

@ -11,7 +11,6 @@
*/
class woocommerce_product_variation extends woocommerce_product {
var $variation;
var $variation_data;
var $variation_id;
var $variation_has_weight;
@ -25,12 +24,14 @@ class woocommerce_product_variation extends woocommerce_product {
*
* @param int $id ID of the product to load
*/
function woocommerce_product_variation( $variation_id ) {
function woocommerce_product_variation( $variation_id, $parent_id = '', $parent_custom_fields = '' ) {
$this->variation_id = $variation_id;
$product_custom_fields = get_post_custom( $this->variation_id );
$this->exists = (sizeof($product_custom_fields)>0) ? true : false;
$this->variation_data = array();
foreach ($product_custom_fields as $name => $value) :
@ -41,14 +42,11 @@ class woocommerce_product_variation extends woocommerce_product {
endforeach;
$this->get_variation_post_data();
/* Get main product data from parent */
$this->id = $this->variation->post_parent;
$this->id = ($parent_id>0) ? $parent_id : wp_get_post_parent_id( $this->variation_id );
if (!$parent_custom_fields) $parent_custom_fields = get_post_custom( $this->id );
$parent_custom_fields = get_post_custom( $this->id );
// Define the data we're going to load: Key => Default value
// Define the data we're going to load from the parent: Key => Default value
$load_data = array(
'sku' => $this->id,
'price' => 0,
@ -68,24 +66,13 @@ class woocommerce_product_variation extends woocommerce_product {
// Load the data from the custom fields
foreach ($load_data as $key => $default) :
if (isset($parent_custom_fields[$key][0]) && !empty($parent_custom_fields[$key][0])) :
$this->$key = $parent_custom_fields[$key][0];
else :
$this->$key = $default;
endif;
$this->$key = (isset($product_custom_fields[$key][0]) && $product_custom_fields[$key][0]!=='') ? $product_custom_fields[$key][0] : $default;
endforeach;
// Load serialised data, unserialise twice to fix WP bug
if (isset($product_custom_fields['product_attributes'][0])) $this->attributes = maybe_unserialize( maybe_unserialize( $product_custom_fields['product_attributes'][0] )); else $this->attributes = array();
$this->product_type = 'variable';
if ($parent_custom_fields) :
$this->exists = true;
else :
$this->exists = false;
endif;
$this->variation_has_sku = $this->variation_has_stock = $this->variation_has_weight = $this->variation_has_price = $this->variation_has_sale_price = false;
@ -120,14 +107,6 @@ class woocommerce_product_variation extends woocommerce_product {
$this->total_stock = $this->stock;
}
/** Get the product's post data */
function get_variation_post_data() {
if (empty($this->variation)) :
$this->variation = get_post( $this->variation_id );
endif;
return $this->variation;
}
/**
* Get variation ID

View File

@ -285,11 +285,11 @@ function woocommerce_edit_address() {
<p class="form-row form-row-first">
<label for="address_address"><?php _e('Address', 'woothemes'); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="address_address" id="address_address" placeholder="<?php _e('1 Infinite Loop', 'woothemes'); ?>" value="<?php echo esc_attr( $address['address'] ); ?>" />
<input type="text" class="input-text" name="address_address" id="address_address" placeholder="<?php _e('Address line 1', 'woothemes'); ?>" value="<?php echo esc_attr( $address['address'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="address_address2" class="hidden"><?php _e('Address 2', 'woothemes'); ?></label>
<input type="text" class="input-text" name="address_address2" id="address_address2" placeholder="<?php _e('Cupertino', 'woothemes'); ?>" value="<?php echo esc_attr( $address['address2'] ); ?>" />
<input type="text" class="input-text" name="address_address2" id="address_address2" placeholder="<?php _e('Address line 2', 'woothemes'); ?>" value="<?php echo esc_attr( $address['address2'] ); ?>" />
</p>
<div class="clear"></div>

View File

@ -361,7 +361,7 @@ if (!function_exists('woocommerce_variable_add_to_cart')) {
if($variation instanceof woocommerce_product_variation) {
if ($variation->variation->post_status != 'publish') continue; // Disabled
if (get_post_status( $variation->get_variation_id() ) != 'publish') continue; // Disabled
$variation_attributes = $variation->get_variation_attributes();
$availability = $variation->get_availability();