Handle product titles with no attributes

This commit is contained in:
Claudiu Lodromanean 2017-02-13 11:45:25 -08:00
parent 8a05f72f78
commit a8b491b55b
2 changed files with 18 additions and 3 deletions

View File

@ -175,7 +175,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
$include_attribute_names = false;
$attributes = (array) $product->get_attributes();
//Determine whether to include attribute names through counting the number of one-word attribute values
// Determine whether to include attribute names through counting the number of one-word attribute values.
$one_word_attributes = 0;
foreach ( $attributes as $name => $value ) {
if ( false === strpos( $value, '-' ) ) {
@ -190,9 +190,10 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
$include_attribute_names = apply_filters( 'woocommerce_product_variation_title_include_attribute_names', $include_attribute_names, $product );
$title_base_text = get_post_field( 'post_title', $product->get_parent_id() );
$title_attributes_text = wc_get_formatted_variation( $product, true, $include_attribute_names );
$separator = ! empty( $title_attributes_text ) ? ' – ' : '';
return apply_filters( 'woocommerce_product_variation_title',
$title_base_text . ' – ' . $title_attributes_text,
return apply_filters( 'woocommerce_product_variation_title',
$title_base_text . $separator . $title_attributes_text,
$product,
$title_base_text,
$title_attributes_text

View File

@ -410,6 +410,20 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
//Check the variation with multiple attributes but only one 1-word attribute
$loaded_variation = wc_get_product( $multiword_attribute_variation->get_id() );
$this->assertEquals( "Test Product – Green, Galaxy S6, One Year", $loaded_variation->get_name() );
}
function test_generate_product_title_no_attributes() {
$product = new WC_Product;
$product->set_name( 'Test Product' );
$product->save();
$variation = new WC_Product_Variation;
$variation->set_parent_id( $product->get_id() );
$variation->set_attributes( array() );
$variation->save();
//Check the one attribute variation title
$loaded_variation = wc_get_product( $variation->get_id() );
$this->assertEquals( "Test Product", $loaded_variation->get_name() );
}
}