Php 在Woocommerce中的单个产品页面上显示特定的自定义产品属性
我发现可以在产品详细信息页面上显示所有自定义属性(使用我需要的特定条形图样式设计)。代码工作起来很有魅力,我有合适的CSS来显示自定义属性的水平条 我的问题是,我只想显示指定的 属性,但不知道如何更改循环来实现这一点Php 在Woocommerce中的单个产品页面上显示特定的自定义产品属性,php,wordpress,woocommerce,hook-woocommerce,custom-taxonomy,Php,Wordpress,Woocommerce,Hook Woocommerce,Custom Taxonomy,我发现可以在产品详细信息页面上显示所有自定义属性(使用我需要的特定条形图样式设计)。代码工作起来很有魅力,我有合适的CSS来显示自定义属性的水平条 我的问题是,我只想显示指定的 属性,但不知道如何更改循环来实现这一点 函数isa_woodcommerce_all_pa(){ 全球$产品; $attributes=$product->get_attributes(); 如果(!$attributes){ 返回; } $out=''; foreach($attributes作为$attribute)
函数isa_woodcommerce_all_pa(){
全球$产品;
$attributes=$product->get_attributes();
如果(!$attributes){
返回;
}
$out='';
foreach($attributes作为$attribute){
//跳过变体
如果($attribute->get_variation()){
继续;
}
$name=$attribute->get_name();
如果($attribute->is_taxonomy()){
$terms=wp_get_post_terms($product->get_id(),$name,'all');
//获取分类法
$tax=$terms[0]->分类法;
//获取税务对象
$tax\u object=get\u分类法($tax);
//获得税务标签
if(设置($tax\u object->labels->singular\u name)){
$tax\u label=$tax\u object->labels->singular\u name;
}elseif(isset($tax\u object->label)){
$tax\u label=$tax\u object->label;
//自WC 3.0以来的修剪标签前缀
如果(0==STRPO($tax_标签,'Product')){
$tax_标签=substr($tax_标签,8);
}
}
$out.='- ”;
$out.='
'.esc_html($tax_label)。:
';
$tax_terms=array();
foreach($terms作为$term){
$single_term=esc_html($term->name);
//如果要将术语显示为链接,请在此处插入额外代码。
数组推送($tax\u terms,$single\u term);
}
$out.=''。内爆('',',$tax_术语)。''
.内爆(“,”,$tax_术语)。“ ”;
}否则{
$value_string=内爆(“,”,$attribute->get_options());
$out.='- ”;
$out.='
。$name.:
';
$out.=' ';
}
}
$out.='
';
回音$out;
}
添加行动(“单一产品汇总”、“isa所有产品汇总”,20);
在以下代码中,您将首先在一个数组中定义所需的产品属性段塞,该段塞将显示在单个产品页面中:
add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
function display_some_product_attributes(){
// HERE define the desired product attributes to be displayed
$defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '<ul class="taste-attributes">';
foreach ( $attributes as $attribute ) {
// Get the product attribute slug from the taxonomy
$attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );
// skip all non desired product attributes
if ( ! in_array($attribute_slug, $defined_attributes) ) {
continue;
}
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}
$out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
'" max="10"><div class="progress-bar"><span style="width:'
. implode(', ', $tax_terms) . '0%">'
. implode(', ', $tax_terms) . '</span></div></progress></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<p class="attribute-label">' . $name . ': </p> ';
$out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
}
}
$out .= '</ul>';
echo $out;
}
add_action('woocommerce_single_product_summary','display_some_product_attributes',25);
函数显示\u某些产品\u属性(){
//此处定义要显示的所需产品属性
$defined_attributes=数组('fyllighet','carrier','billing-e-number');
全球$产品;
$attributes=$product->get_attributes();
如果(!$attributes){
返回;
}
$out='';
foreach($attributes作为$attribute){
//从分类中获取产品属性slug
$attribute\u slug=str\u replace('pa\u','',$attribute->get\u name());
//跳过所有不需要的产品属性
if(!in_数组($attribute_slug,$defined_attributes)){
继续;
}
//跳过变体
如果($attribute->get_variation()){
继续;
}
$name=$attribute->get_name();
如果($attribute->is_taxonomy()){
$terms=wp_get_post_terms($product->get_id(),$name,'all');
//获取分类法
$tax=$terms[0]->分类法;
//获取税务对象
$tax\u object=get\u分类法($tax);
//获得税务标签
if(设置($tax\u object->labels->singular\u name)){
$tax\u label=$tax\u object->labels->singular\u name;
}elseif(isset($tax\u object->label)){
$tax\u label=$tax\u object->label;
//自WC 3.0以来的修剪标签前缀
如果(0==STRPO($tax_标签,'Product')){
$tax_标签=substr($tax_标签,8);
}
}
$out.='- ”;
$out.='
'.esc_html($tax_label)。:
';
$tax_terms=array();
foreach($terms作为$term){
$single_term=esc_html($term->name);
//如果要将术语显示为链接,请在此处插入额外代码。
数组推送($tax\u terms,$single\u term);
}
$out.=''。内爆('',',$tax_术语)。''
.内爆(“,”,$tax_术语)。“ ”;
}否则{
$value_string=内爆(“,”,$attribute->get_options());
$out.='- ”;
$out.='
。$name.:
';
$out.=' ';
}
}
$out.='
';
回音$out;
}
代码进入活动子主题(或活动主题)的function.php文件。经过测试,效果良好。三个属性之一的slug(我需要在上面的设计中列出)是“fyllighet”。我找到了正确的方法,可以让它按照您的意愿工作……试试看,然后告诉我。
add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
function display_some_product_attributes(){
// HERE define the desired product attributes to be displayed
$defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '<ul class="taste-attributes">';
foreach ( $attributes as $attribute ) {
// Get the product attribute slug from the taxonomy
$attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );
// skip all non desired product attributes
if ( ! in_array($attribute_slug, $defined_attributes) ) {
continue;
}
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}
$out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
'" max="10"><div class="progress-bar"><span style="width:'
. implode(', ', $tax_terms) . '0%">'
. implode(', ', $tax_terms) . '</span></div></progress></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<p class="attribute-label">' . $name . ': </p> ';
$out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
}
}
$out .= '</ul>';
echo $out;
}