在Woocommerce存档页面上的阅读更多按钮中显示品牌标签



在Woocommerce中,我正在使用YITH WooCommerce Brands插件来处理产品品牌。我想在woocommerce中显示品牌标签 阅读更多 按钮。

这是我的代码:

// Shop Catalog mode
if ( zget_option( 'woo_catalog_mode', 'zn_woocommerce_options', false, 'no' ) == 'yes' ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// Add the read more button
// Fixes #917
add_action( 'woocommerce_after_shop_loop_item', 'zn_woocommerce_more_info' );
function zn_woocommerce_more_info(){
echo '<span class="kw-actions">';
echo '<a class="actions-moreinfo" href="'.get_permalink().'" title="'. __( "MORE INFO", 'zn_framework' ).'">';
if ( zget_option( 'woo_prod_layout', 'zn_woocommerce_options', false, 'classic' ) == 'style2' ) {
echo '<svg width="50px" height="24px" class="svg-moreIcon"><circle cx="12" cy="12" r="2"/><circle cx="20" cy="12" r="2"/><circle cx="28" cy="12" r="2"/></svg>';
} else {
echo __( "MORE INFO", 'zn_framework' );
}
echo '</a>';
echo '</span>';
}
}

我需要将"阅读更多"文本替换为woocommerce存档页面中每个产品的特定品牌。

任何帮助,不胜感激。

要在挂钩函数中获取当前产品的 YITH 品牌标签,您将使用:

// Get the YITH brands tags (array of term names)
$brands = wp_get_post_terms( get_the_id(), 'yith_product_brand', array( 'fields' => 'names' ) );
// Convert the array to a string (with coma separated brand tags)
$brands = implode(', ', $brands);

或者,如果您有按产品设置的独特品牌,您将使用:

// Get the YITH brands tags (array of term names)
$brands = wp_get_post_terms( get_the_id(), 'yith_product_brand', array( 'fields' => 'names' ) );
// Convert the array to a string
$brand = reset($brands);

现在,您可以轻松地在钩子函数中包含此代码以替换"阅读更多"文本:

add_action( 'woocommerce_after_shop_loop_item', 'zn_woocommerce_more_info' );
function zn_woocommerce_more_info(){
echo '<span class="kw-actions">';
$brands = wp_get_post_terms( get_the_id(), 'yith_product_brand', array( 'fields' => 'names' ) );
$brand = reset($brands);
echo '<a class="actions-moreinfo" href="'.get_permalink().'" title="'. $brand .'">';
if ( zget_option( 'woo_prod_layout', 'zn_woocommerce_options', false, 'classic' ) == 'style2' ) {
echo '<svg width="50px" height="24px" class="svg-moreIcon"><circle cx="12" cy="12" r="2"/><circle cx="20" cy="12" r="2"/><circle cx="28" cy="12" r="2"/></svg>';
} else {
echo $brand;
}
echo '</a>';
echo '</span>';
}

如果您使用Woocommerce品牌插件,则必须替换'product_brand''yith_product_brand'的代码。就这样。

相关答案:在单个产品页面上显示类别和品牌名称

最新更新