WooCommerce:显示活动属性的描述


WooCommerce: Showing Descriptions for Active Attribute

WooCommerce允许您编写属性描述,但它不会在任何地方显示。

我想在类别页面上显示这个属性描述,但只有一种类型的属性,当它被选中时(pa_color)。

解决方案如下。希望能为您节省一些时间!

/*
* If color filter active, show its attribute description on the Archive page
*/
add_action('woocommerce_archive_description', 'custom_attribute_description');
function custom_attribute_description() { 
    global $_chosen_attributes;
    if ( isset($_chosen_attributes['pa_color']) ) {
        $_chosen_color_id = $_chosen_attributes['pa_color']['terms'][0];
        // Prevent non-number IDs from being used
        $_chosen_color_id = preg_replace('/'D/', '', $_chosen_color_id);
        $_chosen_color_details = get_term( $_chosen_color_id, 'pa_color' );
        echo '<div class="chosen-color-description">';
        echo '<strong>' . $_chosen_color_details->name . '</strong>: ';
        echo $_chosen_color_details->description;
        echo '</div>';
    }
}