gpt4 book ai didi

php - 在 Woocommerce 存档页面中的产品标题下显示特定产品属性

转载 作者:行者123 更新时间:2023-12-05 05:13:49 28 4
gpt4 key购买 nike

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

感谢任何帮助。

最佳答案

要在 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 文件。经过测试并有效。

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com