首页 文章

在Woocommerce存档页面中的产品 Headers 下显示特定产品属性

提问于
浏览
1

在woocommerce中,我想在产品 Headers 下的商店页面上显示一些产品属性 . 该产品属性为“年”,“模型”和“油” .

这就是我现在所拥有的:

add_action('woocommerce_shop_loop_item_title', 'wh_insertAfterShopProductTitle', 15);

function wh_insertAfterShopProductTitle()
{
    global $product;

    $abv = $product->get_attribute('pa_year');
    if (empty($abv))
        return;
    echo __($abv, 'woocommerce');
}

任何帮助表示赞赏 .

1 回答

  • 0

    要在Woocommerce存档页面中的产品 Headers 下显示“年”,“模型”和“油”产品属性作为商店,请使用以下内容:

    add_action('woocommerce_shop_loop_item_title', 'display_attributes_after_product_loop_title', 15);
    function display_attributes_after_product_loop_title(){
        global $product;
    
        $output = array(); // Initializing
    
        // The year
        if( $year = $product->get_attribute('pa_year') ){
            // Save the value in the array
            $output[] = $year; 
        }
    
        // The model
        if( $model = $product->get_attribute('pa_model') ){
            // Save the value in the array
            $output[] = $model;
        }
    
        // The type of oil
        if( $oil = $product->get_attribute('pa_oil') ){
            // Save the value in the array
            $output[] = $oil;
        }
    
        // Output
        if( sizeof($output) > 0 ){
            // Display product attributes coma separated values (you can change the separator by something else below).
            echo implode( ', ', $output);
        }
    }
    

    代码放在活动子主题(活动主题)的function.php文件中 . 经过测试和工作 .

相关问题