Wordpress Woocommerce在商店页面上显示属性

时间:2020-05-30 07:42:52

标签: php wordpress woocommerce

我想向wordpress的商店页面添加一些属性。 我在Stackoverflow上发现的这段代码显示了所有属性标签,但都显示了相同的属性名称。

add_action('woocommerce_after_shop_loop_item_title','add_attribute');
function add_attribute() {
global $product;

$product_attributes = array( 'pa_country','pa_class','pa_faction','pa_gender' );
$attr_output = array();


foreach( $product_attributes as $taxonomy ){
    if( taxonomy_exists($taxonomy) ){
        $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
        $value = $product->get_attribute('pa_country','pa_class','pa_faction','pa_gender');

        if( ! empty($value) ){

            $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
        }
    }}


echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';

}

current state

我只需要一点帮助就可以显示所有正确的属性。

2 个答案:

答案 0 :(得分:0)

我解决了这个问题。它不漂亮,但可以。

add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_country';
    echo '<span class="attribute-s">Country: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_class';
    echo '<span class="attribute-s">Class: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_faction';
    echo '<span class="attribute-s">Faction: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_gender';
    echo '<span class="attribute-s">Gender: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}}

答案 1 :(得分:0)

您的代码中有一些小错误。请尝试以下操作:

add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
    global $product;

    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
    $attr_output = array(); // Initializing

    // Loop through your defined product attribute taxonomies
    foreach( $product_attribute_taxonomies as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = wc_attribute_label( $taxonomy, $product );

            $term_names = $product->get_attribute( $taxonomy );

            if( ! empty($term_names) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
            }
        }
    }

    // Output
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试并可以正常工作。


仅对于简单产品,您将使用以下内容:

add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
    global $product;

    // Only for simple products
    if ( ! $product->is_type( 'simple' ) ) return;

    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
    $attr_output = array(); // Initializing

    // Loop through your defined product attribute taxonomies
    foreach( $product_attribute_taxonomies as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = wc_attribute_label( $taxonomy, $product );

            $term_names = $product->get_attribute( $taxonomy );

            if( ! empty($term_names) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
            }
        }
    }

    // Output
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试并可以正常工作。

相关问题