【问题标题】:Show brand tags in read more button on Woocommerce archive pages在 Woocommerce 存档页面上的阅读更多按钮中显示品牌标签
【发布时间】:2018-10-19 11:58:47
【问题描述】:

在 Woocommerce 中,我使用 YITH WooCommerce Brands plugin 处理产品品牌。我想在 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 存档页面中每个产品的特定品牌。

感谢任何帮助。

【问题讨论】:

  • 你用什么插件来做品牌?此外,您的代码具有自定义函数,例如 zget_option() 和 zn_woocommerce_more_info() 没有人可以猜到,因此您的代码不可测试......
  • 我正在使用 YITH Brands 插件

标签: php wordpress button woocommerce custom-taxonomy


【解决方案1】:

要在挂钩函数中获取当前产品的 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 Brands 插件,则必须将代码中的 'yith_product_brand' 替换为 'product_brand'。就是这样。

相关回答:Display category and brand name on single product page

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2018-04-23
    • 1970-01-01
    相关资源
    最近更新 更多