From 29a77b0fe7833fab777241cf04af5765774ba785 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 26 Oct 2011 19:45:38 +0100 Subject: [PATCH] Made product loops a little more efficient --- classes/product.class.php | 36 +++++++++-------------------- classes/product_variation.class.php | 35 ++++++---------------------- shortcodes/shortcode-my_account.php | 4 ++-- woocommerce_template_functions.php | 2 +- 4 files changed, 21 insertions(+), 56 deletions(-) diff --git a/classes/product.class.php b/classes/product.class.php index 249a11d7c47..c35b34c6afa 100644 --- a/classes/product.class.php +++ b/classes/product.class.php @@ -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(); diff --git a/classes/product_variation.class.php b/classes/product_variation.class.php index 5ac2e1d73ea..ffda83a7cc7 100644 --- a/classes/product_variation.class.php +++ b/classes/product_variation.class.php @@ -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 diff --git a/shortcodes/shortcode-my_account.php b/shortcodes/shortcode-my_account.php index 7422883d60a..dc87e69565e 100644 --- a/shortcodes/shortcode-my_account.php +++ b/shortcodes/shortcode-my_account.php @@ -285,11 +285,11 @@ function woocommerce_edit_address() {

- +

- +

diff --git a/woocommerce_template_functions.php b/woocommerce_template_functions.php index 0a53b9f4382..5e40130fe95 100644 --- a/woocommerce_template_functions.php +++ b/woocommerce_template_functions.php @@ -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();