Failure to display more than 2 variation next to the product title in the shopping cart woocommerce

Viewed 18

I hope you answer

I used hook to display the variable next to the product name in the shopping cart

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_true' );

But if a product has more than 2 variables, it will only display the first 2 variables

true => product : variable 1 and variable 2 and variable 3

false => product : variable 1 and variable 2

path : /wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-variation-data-store-cpt.php

/*
|--------------------------------------------------------------------------
| Additional Methods
|--------------------------------------------------------------------------
*/

/**
 * Generates a title with attribute information for a variation.
 * Products will get a title of the form "Name - Value, Value" or just "Name".
 *
 * @since 3.0.0
 * @param WC_Product $product Product object.
 * @return string
 */
protected function generate_product_title( $product ) {
    $attributes = (array) $product->get_attributes();

    // Do not include attributes if the product has 3+ attributes.
    $should_include_attributes = count( $attributes ) < 3;

    // Do not include attributes if an attribute name has 2+ words and the
    // product has multiple attributes.
    if ( $should_include_attributes && 1 < count( $attributes ) ) {
        foreach ( $attributes as $name => $value ) {
            if ( false !== strpos( $name, '-' ) ) {
                $should_include_attributes = false;
                break;
            }
        }
    }

    $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 ) : '';

    return apply_filters( 'woocommerce_product_variation_title', $title_suffix ? $title_base . $separator . $title_suffix : $title_base, $product, $title_base, $title_suffix );
}

I have noticed something : enter image description here

if add gender-one -> Blue -> Short to cart, not show Short product title, I must create 2 variables to display the short

gender-one -> Blue -> Short

gender-one -> Blue -> Tall

1 Answers

You'll have to create a function to return the title in a way you want and you'll have to hook that function with filter woocommerce_product_variation_title

basically, wc_get_formatted_variation( $product, true, false ) this call returns the list of all attributes, you can make similar function to return only first 2 attributes and then you can use that function in your code.

Related