From 2b464d2e521b15c7e43de81406d7c3bf2edffc68 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Tue, 24 Jan 2017 10:51:08 -0800 Subject: [PATCH 1/6] Deprecate get_title and it's hook, use get_name instead and pull name from parent. --- .../abstracts/abstract-wc-legacy-product.php | 16 +++++++++++++ includes/abstracts/abstract-wc-product.php | 9 ------- includes/class-wc-product-variation.php | 24 ++++++++++++------- ...ss-wc-product-variation-data-store-cpt.php | 2 +- includes/wc-deprecated-functions.php | 1 + 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/includes/abstracts/abstract-wc-legacy-product.php b/includes/abstracts/abstract-wc-legacy-product.php index 4148564e4c0..2c553a5ca88 100644 --- a/includes/abstracts/abstract-wc-legacy-product.php +++ b/includes/abstracts/abstract-wc-legacy-product.php @@ -164,6 +164,22 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data { return $this->get_gallery_image_ids(); } + /** + * Get the product's title. + * + * @deprecated 2.7.0 + * @return string + */ + public function get_title() { + wc_deprecated_function( 'WC_Product::get_title', '2.7', 'WC_Product::get_name' ); + + if ( $this->is_type( 'variation' ) ) { + return apply_filters( 'woocommerce_product_title', $this->parent_data['name'], $this ); + } + + return apply_filters( 'woocommerce_product_title', $this->get_name(), $this ); + } + /** * Set stock level of the product. * diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index 133e5bed008..0c274ad5931 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -1626,15 +1626,6 @@ class WC_Product extends WC_Abstract_Legacy_Product { |-------------------------------------------------------------------------- */ - /** - * Get the product's title. For products this is the product name. - * - * @return string - */ - public function get_title() { - return apply_filters( 'woocommerce_product_title', $this->get_name(), $this ); - } - /** * Product permalink. * @return string diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index 9d68f77e20d..cf7e3874c73 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -27,6 +27,7 @@ class WC_Product_Variation extends WC_Product_Simple { * @var array */ protected $parent_data = array( + 'name' => '', 'sku' => '', 'manage_stock' => '', 'stock_quantity' => '', @@ -64,15 +65,6 @@ class WC_Product_Variation extends WC_Product_Simple { return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id(); } - /** - * Get the product's title. For variations this is the parent product name. - * - * @return string - */ - public function get_title() { - return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this ); - } - /** * Get variation attribute values. Keys are prefixed with attribute_, as stored. * @@ -120,6 +112,20 @@ class WC_Product_Variation extends WC_Product_Simple { return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this ); } + /** + * Get product name. + * + * @param string $context + * @return string + */ + public function get_name( $context = 'view' ) { + if ( 'view' === $context ) { + return $this->parent_data['name']; + } else { + return $this->get_prop( 'name', $context ); + } + } + /** * Get SKU (Stock-keeping unit) - product unique ID. * diff --git a/includes/data-stores/class-wc-product-variation-data-store-cpt.php b/includes/data-stores/class-wc-product-variation-data-store-cpt.php index e70aa37797f..4b233a6e7c5 100644 --- a/includes/data-stores/class-wc-product-variation-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variation-data-store-cpt.php @@ -198,7 +198,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl } $product->set_parent_data( array( - 'title' => get_the_title( $product->get_parent_id() ), + 'name' => get_the_title( $product->get_parent_id() ), 'sku' => get_post_meta( $product->get_parent_id(), '_sku', true ), 'manage_stock' => get_post_meta( $product->get_parent_id(), '_manage_stock', true ), 'backorders' => get_post_meta( $product->get_parent_id(), '_backorders', true ), diff --git a/includes/wc-deprecated-functions.php b/includes/wc-deprecated-functions.php index cf32c818685..a81a4234b77 100644 --- a/includes/wc-deprecated-functions.php +++ b/includes/wc-deprecated-functions.php @@ -595,6 +595,7 @@ $wc_map_deprecated_filters = array( 'woocommerce_product_get_gallery_image_ids' => 'woocommerce_product_gallery_attachment_ids', 'woocommerce_product_get_review_count' => 'woocommerce_product_review_count', 'woocommerce_product_get_downloads' => 'woocommerce_product_files', + 'woocommerce_product_get_name' => 'woocommerce_product_title', 'woocommerce_order_get_currency' => 'woocommerce_get_currency', 'woocommerce_order_get_discount_total' => 'woocommerce_order_amount_discount_total', 'woocommerce_order_get_discount_tax' => 'woocommerce_order_amount_discount_tax', From f344143b63671bb4d4117d4a024e6607b872c5f2 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Wed, 25 Jan 2017 10:27:28 -0800 Subject: [PATCH 2/6] Catch variation text and update --- includes/class-wc-product-variation.php | 17 +++++++++++------ ...ss-wc-product-variation-data-store-cpt.php | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index cf7e3874c73..0050f7cd258 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -27,7 +27,6 @@ class WC_Product_Variation extends WC_Product_Simple { * @var array */ protected $parent_data = array( - 'name' => '', 'sku' => '', 'manage_stock' => '', 'stock_quantity' => '', @@ -119,11 +118,7 @@ class WC_Product_Variation extends WC_Product_Simple { * @return string */ public function get_name( $context = 'view' ) { - if ( 'view' === $context ) { - return $this->parent_data['name']; - } else { - return $this->get_prop( 'name', $context ); - } + return $this->get_prop( 'name', $context ); } /** @@ -305,6 +300,16 @@ class WC_Product_Variation extends WC_Product_Simple { $this->parent_data = $parent_data; } + /** + * Get the parent data array for this variation. + * + * @since 2.7.0 + * @return array + */ + public function get_parent_data() { + return $this->parent_data; + } + /** * Set attributes. Unlike the parent product which uses terms, variations are assigned * specific attributes using name value pairs. diff --git a/includes/data-stores/class-wc-product-variation-data-store-cpt.php b/includes/data-stores/class-wc-product-variation-data-store-cpt.php index 4b233a6e7c5..81726f909e2 100644 --- a/includes/data-stores/class-wc-product-variation-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variation-data-store-cpt.php @@ -55,8 +55,10 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl throw new Exception( sprintf( 'Invalid parent for variation #%d', $product->get_id() ), 422 ); } + $product_name = get_the_title( $post_object ); + $product->set_props( array( - 'name' => get_the_title( $post_object ), + 'name' => $product_name, 'slug' => $post_object->post_name, 'date_created' => $post_object->post_date, 'date_modified' => $post_object->post_modified, @@ -68,6 +70,21 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl $this->read_product_data( $product ); $product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) ); + error_log( print_r( $product_name, 1 ) ); + + if ( __( 'Variation #', 'woocommerce' ) === substr( $product_name, 0, 11 ) ) { + error_log( 'clean up' ); + error_log( print_r( $product_name, 1 ) ); + $parent_data = $product->get_parent_data(); + $new_title = $parent_data['name'] . ' – ' . wc_get_formatted_variation( $product, true, false ); + $product->set_name( $new_title ); + wp_update_post( array( + 'ID' => $product->get_id(), + 'post_title' => $new_title, + ) ); + error_log( print_r( $new_title, 1 ) ); + } + // Set object_read true once all data is read. $product->set_object_read( true ); } From 9889efec75b14b28a18ce0e1476534695d8818d0 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Wed, 25 Jan 2017 10:37:48 -0800 Subject: [PATCH 3/6] Catch Product # titles and remove error logs --- .../class-wc-product-variation-data-store-cpt.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/includes/data-stores/class-wc-product-variation-data-store-cpt.php b/includes/data-stores/class-wc-product-variation-data-store-cpt.php index 81726f909e2..f094318d3c6 100644 --- a/includes/data-stores/class-wc-product-variation-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variation-data-store-cpt.php @@ -70,19 +70,15 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl $this->read_product_data( $product ); $product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) ); - error_log( print_r( $product_name, 1 ) ); - - if ( __( 'Variation #', 'woocommerce' ) === substr( $product_name, 0, 11 ) ) { - error_log( 'clean up' ); - error_log( print_r( $product_name, 1 ) ); + // Clean up old variation titles. + if ( __( 'Variation #', 'woocommerce' ) === substr( $product_name, 0, 11 ) || ( 'Product #' . $product->get_parent_id() . ' Variation' ) === $product_name ) { $parent_data = $product->get_parent_data(); - $new_title = $parent_data['name'] . ' – ' . wc_get_formatted_variation( $product, true, false ); + $new_title = $parent_data['name'] . ' – ' . wc_get_formatted_variation( $product, true, false ); $product->set_name( $new_title ); wp_update_post( array( 'ID' => $product->get_id(), 'post_title' => $new_title, ) ); - error_log( print_r( $new_title, 1 ) ); } // Set object_read true once all data is read. From 318046789c1ac2c0eebbe37b3f5459a9e2514160 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Wed, 25 Jan 2017 10:40:14 -0800 Subject: [PATCH 4/6] Comment clarifying translation wrapper. --- .../class-wc-product-variation-data-store-cpt.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/includes/data-stores/class-wc-product-variation-data-store-cpt.php b/includes/data-stores/class-wc-product-variation-data-store-cpt.php index f094318d3c6..319333bfaf0 100644 --- a/includes/data-stores/class-wc-product-variation-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variation-data-store-cpt.php @@ -70,7 +70,11 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl $this->read_product_data( $product ); $product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) ); - // Clean up old variation titles. + /** + * Clean up old variation titles. + * The "Product #" text is intentionally not wrapped in translation functions for a faster comparision. It was not inserted as a translated string: + * https://github.com/woocommerce/woocommerce/blob/5fc88694d211e2e176bded16d7fb95cf6285249e/includes/class-wc-ajax.php#L776 + */ if ( __( 'Variation #', 'woocommerce' ) === substr( $product_name, 0, 11 ) || ( 'Product #' . $product->get_parent_id() . ' Variation' ) === $product_name ) { $parent_data = $product->get_parent_data(); $new_title = $parent_data['name'] . ' – ' . wc_get_formatted_variation( $product, true, false ); From 7b1adfb98fc0c0179c89285f73a55f10cc6c0385 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Thu, 26 Jan 2017 07:46:59 -0800 Subject: [PATCH 5/6] Bake out of get_title deprecation changes --- .../abstracts/abstract-wc-legacy-product.php | 16 ---------------- includes/abstracts/abstract-wc-product.php | 9 +++++++++ includes/class-wc-product-variation.php | 9 +++++++++ ...class-wc-product-variation-data-store-cpt.php | 4 ++-- includes/wc-deprecated-functions.php | 1 - 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/includes/abstracts/abstract-wc-legacy-product.php b/includes/abstracts/abstract-wc-legacy-product.php index 2c553a5ca88..4148564e4c0 100644 --- a/includes/abstracts/abstract-wc-legacy-product.php +++ b/includes/abstracts/abstract-wc-legacy-product.php @@ -164,22 +164,6 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data { return $this->get_gallery_image_ids(); } - /** - * Get the product's title. - * - * @deprecated 2.7.0 - * @return string - */ - public function get_title() { - wc_deprecated_function( 'WC_Product::get_title', '2.7', 'WC_Product::get_name' ); - - if ( $this->is_type( 'variation' ) ) { - return apply_filters( 'woocommerce_product_title', $this->parent_data['name'], $this ); - } - - return apply_filters( 'woocommerce_product_title', $this->get_name(), $this ); - } - /** * Set stock level of the product. * diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index 0c274ad5931..133e5bed008 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -1626,6 +1626,15 @@ class WC_Product extends WC_Abstract_Legacy_Product { |-------------------------------------------------------------------------- */ + /** + * Get the product's title. For products this is the product name. + * + * @return string + */ + public function get_title() { + return apply_filters( 'woocommerce_product_title', $this->get_name(), $this ); + } + /** * Product permalink. * @return string diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index 0050f7cd258..9e46a8b2eda 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -64,6 +64,15 @@ class WC_Product_Variation extends WC_Product_Simple { return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id(); } + /** + * Get the product's title. For variations this is the parent product name. + * + * @return string + */ + public function get_title() { + return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this ); + } + /** * Get variation attribute values. Keys are prefixed with attribute_, as stored. * diff --git a/includes/data-stores/class-wc-product-variation-data-store-cpt.php b/includes/data-stores/class-wc-product-variation-data-store-cpt.php index 319333bfaf0..c0541abf7d6 100644 --- a/includes/data-stores/class-wc-product-variation-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variation-data-store-cpt.php @@ -77,7 +77,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl */ if ( __( 'Variation #', 'woocommerce' ) === substr( $product_name, 0, 11 ) || ( 'Product #' . $product->get_parent_id() . ' Variation' ) === $product_name ) { $parent_data = $product->get_parent_data(); - $new_title = $parent_data['name'] . ' – ' . wc_get_formatted_variation( $product, true, false ); + $new_title = $parent_data['title'] . ' – ' . wc_get_formatted_variation( $product, true, false ); $product->set_name( $new_title ); wp_update_post( array( 'ID' => $product->get_id(), @@ -215,7 +215,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl } $product->set_parent_data( array( - 'name' => get_the_title( $product->get_parent_id() ), + 'title' => get_the_title( $product->get_parent_id() ), 'sku' => get_post_meta( $product->get_parent_id(), '_sku', true ), 'manage_stock' => get_post_meta( $product->get_parent_id(), '_manage_stock', true ), 'backorders' => get_post_meta( $product->get_parent_id(), '_backorders', true ), diff --git a/includes/wc-deprecated-functions.php b/includes/wc-deprecated-functions.php index a81a4234b77..cf32c818685 100644 --- a/includes/wc-deprecated-functions.php +++ b/includes/wc-deprecated-functions.php @@ -595,7 +595,6 @@ $wc_map_deprecated_filters = array( 'woocommerce_product_get_gallery_image_ids' => 'woocommerce_product_gallery_attachment_ids', 'woocommerce_product_get_review_count' => 'woocommerce_product_review_count', 'woocommerce_product_get_downloads' => 'woocommerce_product_files', - 'woocommerce_product_get_name' => 'woocommerce_product_title', 'woocommerce_order_get_currency' => 'woocommerce_get_currency', 'woocommerce_order_get_discount_total' => 'woocommerce_order_amount_discount_total', 'woocommerce_order_get_discount_tax' => 'woocommerce_order_amount_discount_tax', From 0b1685c76b071a66d63ffaff01cd995a491bf0ed Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Thu, 26 Jan 2017 07:48:04 -0800 Subject: [PATCH 6/6] Remove variation get_name, it will get it from parent. --- includes/class-wc-product-variation.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index 9e46a8b2eda..99fec657b14 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -120,16 +120,6 @@ class WC_Product_Variation extends WC_Product_Simple { return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this ); } - /** - * Get product name. - * - * @param string $context - * @return string - */ - public function get_name( $context = 'view' ) { - return $this->get_prop( 'name', $context ); - } - /** * Get SKU (Stock-keeping unit) - product unique ID. *