首页 > 解决方案 > 在 Woocommerce 存档页面的产品标题下显示特定产品属性

问题描述

在 woocommerce 中,我想在产品标题下的商店页面上显示一些产品属性。该产品属性为“年份”、“型号”和“油品”。

这就是我现在所拥有的:

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

任何帮助表示赞赏。

标签: phpwordpresswoocommerceproductcustom-taxonomy

解决方案


要将 Woocommerce 存档页面中产品标题下的“年份”、“型号”和“石油”产品属性显示为商店,请使用以下命令:

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 文件中。测试和工作。


推荐阅读