php - 在 Woocommerce 中的单个产品页面上显示特定的自定义产品属性

标签 php wordpress woocommerce hook-woocommerce custom-taxonomy

我找到了 following code在产品详细信息页面上显示所有自定义属性(具有我需要的特定栏式设计)。该代码就像一个魅力,我有适当的 CSS 来显示我的自定义属性的水平条。

Problem I have is that I only want to display specific named attributes and don't know how to change the loop to do that...

function isa_woocommerce_all_pa(){

global $product;
$attributes = $product->get_attributes();

if ( ! $attributes ) {
    return;
}

$out = '<ul class="taste-attributes">';

foreach ( $attributes as $attribute ) {


    // 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', 'isa_woocommerce_all_pa', 20);

最佳答案

在下面的代码中,您将首先在数组中定义所需的产品属性 slugs,它们将显示在单个产品页面中:

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;
}

代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。

关于php - 在 Woocommerce 中的单个产品页面上显示特定的自定义产品属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51936657/

相关文章:

php - woocommerce 自定义 ajax 顶级购物车

php - 使用 Jasny 文件输入使用 PHP 将文件上传到服务器

php - 根据 WooCommerce 中的用户类型启用不同的产品变体价格

css - 条纹输入字段宽度非常小

php - WooCommerce 产品图像缩放选项的可用参数详细信息

wordpress - 如何解决 StyleLint 中的 'property-no-unknown' 错误?

java - php和flume直接集成还是php和java集成?

php: SQLSTATE[HY000] [2002] 由于目标机器主动拒绝而无法建立连接

javascript - 在 CodeIgniter 中高效压缩 CSS 和 JS

c# - WordPress wp_check_password c#