Catch variation text and update

This commit is contained in:
Justin Shreve 2017-01-25 10:27:28 -08:00
parent 2b464d2e52
commit f344143b63
2 changed files with 29 additions and 7 deletions

View File

@ -27,7 +27,6 @@ class WC_Product_Variation extends WC_Product_Simple {
* @var array * @var array
*/ */
protected $parent_data = array( protected $parent_data = array(
'name' => '',
'sku' => '', 'sku' => '',
'manage_stock' => '', 'manage_stock' => '',
'stock_quantity' => '', 'stock_quantity' => '',
@ -119,12 +118,8 @@ class WC_Product_Variation extends WC_Product_Simple {
* @return string * @return string
*/ */
public function get_name( $context = 'view' ) { 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 );
} }
}
/** /**
* Get SKU (Stock-keeping unit) - product unique ID. * Get SKU (Stock-keeping unit) - product unique ID.
@ -305,6 +300,16 @@ class WC_Product_Variation extends WC_Product_Simple {
$this->parent_data = $parent_data; $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 * Set attributes. Unlike the parent product which uses terms, variations are assigned
* specific attributes using name value pairs. * specific attributes using name value pairs.

View File

@ -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 ); throw new Exception( sprintf( 'Invalid parent for variation #%d', $product->get_id() ), 422 );
} }
$product_name = get_the_title( $post_object );
$product->set_props( array( $product->set_props( array(
'name' => get_the_title( $post_object ), 'name' => $product_name,
'slug' => $post_object->post_name, 'slug' => $post_object->post_name,
'date_created' => $post_object->post_date, 'date_created' => $post_object->post_date,
'date_modified' => $post_object->post_modified, '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 ); $this->read_product_data( $product );
$product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) ); $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. // Set object_read true once all data is read.
$product->set_object_read( true ); $product->set_object_read( true );
} }