Skip to content

Commit

Permalink
Simplify wc_is_attribute_in_product_name and change separator
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Apr 11, 2017
1 parent 7156569 commit 5cfed75
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
16 changes: 5 additions & 11 deletions includes/data-stores/class-wc-product-variation-data-store-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected function generate_product_title( $product ) {

// Don't include attributes if an attribute name has 2+ words.
if ( $should_include_attributes ) {
foreach( $attributes as $name => $value ) {
foreach ( $attributes as $name => $value ) {
if ( false !== strpos( $name, '-' ) ) {
$should_include_attributes = false;
break;
Expand All @@ -202,17 +202,11 @@ protected function generate_product_title( $product ) {
}

$should_include_attributes = apply_filters( 'woocommerce_product_variation_title_include_attributes', $should_include_attributes, $product );
$separator = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', $product );
$title_base = get_post_field( 'post_title', $product->get_parent_id() );
$title_suffix = $should_include_attributes ? wc_get_formatted_variation( $product, true, false ) : '';

$title_base_text = get_post_field( 'post_title', $product->get_parent_id() );
$title_attributes_text = $should_include_attributes ? wc_get_formatted_variation( $product, true, false ) : '';
$separator = ! empty( $title_attributes_text ) ? ' – ' : '';

return apply_filters( 'woocommerce_product_variation_title',
$title_base_text . $separator . $title_attributes_text,
$product,
$title_base_text,
$title_attributes_text
);
return apply_filters( 'woocommerce_product_variation_title', rtrim( $title_base . $separator . $title_suffix, $separator ), $product, $title_base, $title_suffix );
}

/**
Expand Down
16 changes: 2 additions & 14 deletions includes/wc-attribute-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,13 @@ function wc_attributes_array_filter_variation( $attribute ) {
}

/**
* Check if an attribute is included in the attributes area of a product name.
* Check if an attribute is included in the attributes area of a variation name.
*
* @since 3.0.2
* @param string $attribute Attribute value to check for
* @param string $name Product name to check in
* @return bool
*/
function wc_is_attribute_in_product_name( $attribute, $name ) {
$attribute = strtolower( $attribute );
$name = strtolower( $name );
$product_name_sections = explode( '–', $name );

// Only one main area exists, so no attribute area exists.
if ( count( $product_name_sections ) < 2 ) {
return false;
}

$product_attributes_section = end( $product_name_sections );
$product_attributes = array_map( 'trim', explode( ',', $product_attributes_section ) );

return in_array( $attribute, $product_attributes );
return stristr( $name, ' ' . $attribute . ',' ) || 0 === stripos( strrev( $name ), strrev( ' ' . $attribute ) );
}
4 changes: 2 additions & 2 deletions tests/unit-tests/product/data-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ function test_generate_product_title() {

// Check the one attribute variation title.
$loaded_variation = wc_get_product( $one_attribute_variation->get_id() );
$this->assertEquals( "Test Product &ndash; Green", $loaded_variation->get_name() );
$this->assertEquals( "Test Product - Green", $loaded_variation->get_name() );

// Check the two attribute variation title.
$loaded_variation = wc_get_product( $two_attribute_variation->get_id() );
$this->assertEquals( "Test Product &ndash; Green, Large", $loaded_variation->get_name() );
$this->assertEquals( "Test Product - Green, Large", $loaded_variation->get_name() );

// Check the variation with a multiword attribute name.
$loaded_variation = wc_get_product( $multiword_attribute_variation->get_id() );
Expand Down

0 comments on commit 5cfed75

Please sign in to comment.