Merge pull request #15355 from woocommerce/fix/15315

Update title if changed
This commit is contained in:
Claudiu Lodromanean 2017-05-31 09:30:41 -07:00 committed by GitHub
commit ea3f46e52f
2 changed files with 21 additions and 21 deletions

View File

@ -73,13 +73,11 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
/** /**
* If a variation title is not in sync with the parent e.g. saved prior to 3.0, or if the parent title has changed, detect here and update. * If a variation title is not in sync with the parent e.g. saved prior to 3.0, or if the parent title has changed, detect here and update.
*/ */
$parent_title = get_post_field( 'post_title', $product->get_parent_id() );
if ( 0 !== strpos( $post_object->post_title, $parent_title ) ) {
global $wpdb;
$new_title = $this->generate_product_title( $product ); $new_title = $this->generate_product_title( $product );
if ( $post_object->post_title !== $new_title ) {
$product->set_name( $new_title ); $product->set_name( $new_title );
$wpdb->update( $wpdb->posts, array( 'post_title' => $new_title ), array( 'ID' => $product->get_id() ) ); $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, array( 'post_title' => $new_title ), array( 'ID' => $product->get_id() ) );
clean_post_cache( $product->get_id() ); clean_post_cache( $product->get_id() );
} }
@ -98,11 +96,17 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
$product->set_date_created( current_time( 'timestamp', true ) ); $product->set_date_created( current_time( 'timestamp', true ) );
} }
$new_title = $this->generate_product_title( $product );
if ( $product->get_name( 'edit' ) !== $new_title ) {
$product->set_name( $new_title );
}
$id = wp_insert_post( apply_filters( 'woocommerce_new_product_variation_data', array( $id = wp_insert_post( apply_filters( 'woocommerce_new_product_variation_data', array(
'post_type' => 'product_variation', 'post_type' => 'product_variation',
'post_status' => $product->get_status() ? $product->get_status() : 'publish', 'post_status' => $product->get_status() ? $product->get_status() : 'publish',
'post_author' => get_current_user_id(), 'post_author' => get_current_user_id(),
'post_title' => $this->generate_product_title( $product ), 'post_title' => $product->get_name( 'edit' ),
'post_content' => '', 'post_content' => '',
'post_parent' => $product->get_parent_id(), 'post_parent' => $product->get_parent_id(),
'comment_status' => 'closed', 'comment_status' => 'closed',
@ -139,13 +143,18 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
*/ */
public function update( &$product ) { public function update( &$product ) {
$product->save_meta_data(); $product->save_meta_data();
$new_title = $this->generate_product_title( $product );
if ( $product->get_name( 'edit' ) !== $new_title ) {
$product->set_name( $new_title );
}
$changes = $product->get_changes(); $changes = $product->get_changes();
$title = $this->generate_product_title( $product );
// Only update the post when the post data changes. // Only update the post when the post data changes.
if ( $title !== $product->get_name( 'edit' ) || array_intersect( array( 'parent_id', 'status', 'menu_order', 'date_created', 'date_modified' ), array_keys( $changes ) ) ) { if ( array_intersect( array( 'name', 'parent_id', 'status', 'menu_order', 'date_created', 'date_modified' ), array_keys( $changes ) ) ) {
$post_data = array( $post_data = array(
'post_title' => $title, 'post_title' => $product->get_name( 'edit' ),
'post_parent' => $product->get_parent_id( 'edit' ), 'post_parent' => $product->get_parent_id( 'edit' ),
'comment_status' => 'closed', 'comment_status' => 'closed',
'post_status' => $product->get_status( 'edit' ) ? $product->get_status( 'edit' ) : 'publish', 'post_status' => $product->get_status( 'edit' ) ? $product->get_status( 'edit' ) : 'publish',

View File

@ -247,7 +247,6 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$this->assertEquals( 'Variable Product', $product->get_name() ); $this->assertEquals( 'Variable Product', $product->get_name() );
$variation = new WC_Product_Variation; $variation = new WC_Product_Variation;
$variation->set_name( 'Variation #1 of Dummy Variable CRUD Product' );
$variation->set_parent_id( $product->get_id() ); $variation->set_parent_id( $product->get_id() );
$variation->set_regular_price( 10 ); $variation->set_regular_price( 10 );
$variation->set_sku( 'CRUD DUMMY SKU VARIABLE GREEN' ); $variation->set_sku( 'CRUD DUMMY SKU VARIABLE GREEN' );
@ -258,7 +257,6 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$variation->set_attributes( array( 'pa_color' => 'green' ) ); $variation->set_attributes( array( 'pa_color' => 'green' ) );
$variation->save(); $variation->save();
$this->assertEquals( 'Variation #1 of Dummy Variable CRUD Product', $variation->get_name() );
$this->assertEquals( 'CRUD DUMMY SKU VARIABLE GREEN', $variation->get_sku() ); $this->assertEquals( 'CRUD DUMMY SKU VARIABLE GREEN', $variation->get_sku() );
$this->assertEquals( 10, $variation->get_price() ); $this->assertEquals( 10, $variation->get_price() );
@ -270,7 +268,6 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$this->assertEquals( $expected_attributes, $product->get_variation_attributes() ); $this->assertEquals( $expected_attributes, $product->get_variation_attributes() );
$variation_2 = new WC_Product_Variation; $variation_2 = new WC_Product_Variation;
$variation_2->set_name( 'Variation #2 of Dummy Variable CRUD Product' );
$variation_2->set_parent_id( $product->get_id() ); $variation_2->set_parent_id( $product->get_id() );
$variation_2->set_regular_price( 10 ); $variation_2->set_regular_price( 10 );
$variation_2->set_sku( 'CRUD DUMMY SKU VARIABLE RED' ); $variation_2->set_sku( 'CRUD DUMMY SKU VARIABLE RED' );
@ -281,7 +278,6 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$variation_2->set_attributes( array( 'pa_color' => 'red' ) ); $variation_2->set_attributes( array( 'pa_color' => 'red' ) );
$variation_2->save(); $variation_2->save();
$this->assertEquals( 'Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name() );
$this->assertEquals( 'CRUD DUMMY SKU VARIABLE RED', $variation_2->get_sku() ); $this->assertEquals( 'CRUD DUMMY SKU VARIABLE RED', $variation_2->get_sku() );
$this->assertEquals( 10, $variation_2->get_price() ); $this->assertEquals( 10, $variation_2->get_price() );
@ -293,7 +289,6 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$expected_attributes = array( 'pa_color' => array( 'green', 'red' ) ); $expected_attributes = array( 'pa_color' => array( 'green', 'red' ) );
$this->assertEquals( $expected_attributes, $product->get_variation_attributes() ); $this->assertEquals( $expected_attributes, $product->get_variation_attributes() );
$variation_2->set_name( 'UPDATED - Variation #2 of Dummy Variable CRUD Product' );
$variation_2->set_regular_price( 15 ); $variation_2->set_regular_price( 15 );
$variation_2->set_sale_price( 9.99 ); $variation_2->set_sale_price( 9.99 );
$variation_2->set_date_on_sale_to( '32532537600' ); $variation_2->set_date_on_sale_to( '32532537600' );
@ -308,7 +303,6 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$expected_prices['sale_price'][ $children[1] ] = 9.99; $expected_prices['sale_price'][ $children[1] ] = 9.99;
$this->assertEquals( $expected_prices, $product->get_variation_prices() ); $this->assertEquals( $expected_prices, $product->get_variation_prices() );
$this->assertEquals( 'UPDATED - Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name() );
$product->set_name( 'Renamed Variable Product' ); $product->set_name( 'Renamed Variable Product' );
$product->save(); $product->save();
@ -469,16 +463,13 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$multiword_attribute_variation->save(); $multiword_attribute_variation->save();
// Check the one attribute variation title. // Check the one attribute variation title.
$loaded_variation = wc_get_product( $one_attribute_variation->get_id() ); $this->assertEquals( "Test Product - Green", $one_attribute_variation->get_name() );
$this->assertEquals( "Test Product - Green", $loaded_variation->get_name() );
// Check the two attribute variation title. // Check the two attribute variation title.
$loaded_variation = wc_get_product( $two_attribute_variation->get_id() ); $this->assertEquals( "Test Product - Green, Large", $two_attribute_variation->get_name() );
$this->assertEquals( "Test Product - Green, Large", $loaded_variation->get_name() );
// Check the variation with a multiword attribute name. // Check the variation with a multiword attribute name.
$loaded_variation = wc_get_product( $multiword_attribute_variation->get_id() ); $this->assertEquals( "Test Product", $multiword_attribute_variation->get_name() );
$this->assertEquals( "Test Product", $loaded_variation->get_name() );
} }
function test_generate_product_title_disable() { function test_generate_product_title_disable() {