在Woocommerce单个产品页面中的产品标题下方显示属性

问题描述

我想在WooCommerce单个产品页面上的产品标题下方显示特定的产品属性,如下所示:

产品标题

标签| SKU

类型|样式

年份|格式|发布类型

其中标签,流派,样式,年份,格式,发行类型是具有多个可能值的属性,SKU是内部产品SKU, | -分隔符。

我正尝试根据this example通过以下代码获取特定属性,但对我而言不起作用。

/**
* Show specified WooCommerce product attributes below the title on single product page.
*/
function wc_get_specific_pa(){
  
    // Titles of the attributes I want to display
    $desired_atts = array( 'record-label','genre','style','format','release-date','release-type');
 
    // sanitize attributes into taxonomy slugs
    foreach ( $desired_atts as $att ) {
      $tax_slugs[] = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/','_',$att)));
    }
     
    global $product;
    $attributes = $product->get_attributes();
     
    if ( ! $attributes ) {
        return;
    }
      
    $out = '';
   
    foreach ( $attributes as $attribute ) {
        $name = $attribute->get_name();
          
        if ( $attribute->is_taxonomy() ) {
          
            $clean_name = $attribute['name'];
            // Trim pa_ prefix
            if ( 0 === strpos( $clean_name,'pa_' ) ) {
              $clean_name = substr( $clean_name,3 );
            }
             
            // get value and label
             
            if ( in_array( $clean_name,array( $tax_slugs ) ) ) {
              
                $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 .= $tax_label . ': ';
                $tax_terms = array();
                foreach ( $terms as $term ) {
                    $single_term = esc_html( $term->name );
                    array_push( $tax_terms,$single_term );
                }
                $out .= implode(',',$tax_terms) .  '<br />';
              
            } // our desired att
              
        } else {
          
            // for atts which are NOT registered as taxonomies
              
            // if this is desired att,get value and label
            if ( in_array( $name,array( $desired_atts ) ) ) {
                $out .= $name . ': ';
                $out .= esc_html( implode( ',$attribute->get_options() ) ) . '<br />';
            }
        }
      
    }
      
    echo $out;
}
add_action('woocommerce_single_product_summary','wc_get_specific_pa');

我是一名编码新手,但如果我理解正确,那么在设法完成“属性代码”工作后,我需要添加 get_sku 函数,然后根据需要调整其显示方式代码中的 $ out 变量。正确吗?

非常感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)