将 WooCommerce 外部产品循环“添加到购物车”按钮更改为链接到产品的“查看更多”

问题描述 投票:0回答:1

我正在尝试向 WordPress 上的外部产品添加“查看更多”按钮,以链接到产品页面而不是外部链接。这样,用户可以在进行外部点击之前获取更多详细信息(在本例中,它是指向预订系统的链接,而不是指向附属机构的链接)。

我能够添加一些代码,并且“查看更多”按钮现在显示,但仅显示在类别页面上 例子: https://faceup-romanorte.com/tratamiento-categoria/valoraciones/

它没有在我的其他页面上显示我添加了一个块来使用 Elementor 显示 WordPress 产品

例如请参阅我的主页www.faceup-romanorte.com

产品是可见的,如果你点击它们,你会进入详细信息页面,但这对用户来说有点混乱(我们可以通过hotjar看到这一点)

这是我添加的代码

//Modify
add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_replace_add_to_cart_button', 10, 2 );
function ts_replace_add_to_cart_button( $button, $product ) {
if (is_product_category() || is_shop()) {
$button_text = __("Ver más", "woocommerce");
$button_link = $product->get_permalink();
$button = '<a class="button" href="' . $button_link . '">' . $button_text . '</a>';
return $button;
}
}

但它似乎只在类别页面上激活它,而不是在所有其他页面上激活它。有人可以帮忙吗?

Main page before adding code

Category page before adding the code

当我添加代码时,类别页面上的按钮确实发生了变化

Category page with modified code(“Ver más”的意思是“查看详细信息”)

但是它隐藏了主页上的按钮,这也不理想

Main page when adding code - 按钮消失

任何人都可以帮助解决我在这里缺少的东西吗?

php html wordpress woocommerce product
1个回答
0
投票

对于外部产品,要使用“查看链接到单个产品的更多按钮”来更改存档页面上的循环“添加到购物车”按钮,请使用以下命令:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_external_product_loop_add_to_cart_link', 100, 3 );
function replace_external_product_loop_add_to_cart_link( $button, $product, $args ) {
    // Only for external products
    if ( $product->is_type('external') ) {
        $button = sprintf( '<a href="%s" class="%s" %s>%s</a>',
            esc_url( $product->get_permalink() ),
            esc_attr( 'button' ),
            isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
            esc_html__( 'View more' )
        );
    }
    return $button;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.