未能在购物车wooccommerce中的产品标题旁边显示2个以上的变体



我希望你回答

我用钩子在购物车中显示产品名称旁边的变量

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_true' );

但如果一个产品有两个以上的变量,它将只显示前两个变量

true=>产品:变量1、变量2和变量3

false=>产品:变量1和变量2

路径:/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 );
}

我注意到了一些事情:在这里输入图像描述

如果添加性别一->蓝色->短到购物车,不显示短产品标题,我必须创建2个变量来显示短

性别一->蓝色->短期

性别一->蓝色->高

您必须创建一个函数来以您想要的方式返回标题,并且必须将该函数与过滤器woocommerce_product_variation_title挂钩

基本上,wc_get_formatted_variation( $product, true, false )这个调用返回所有属性的列表,您可以制作类似的函数,只返回前两个属性,然后您可以在代码中使用该函数。

最新更新