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

Change WooCommerce external product loop add to cart button to a "view more" linked to the product

提问人:Alex Dubois 提问时间:11/10/2023 最后编辑:LoicTheAztecAlex Dubois 更新时间:11/10/2023 访问量:42

问:

我正在尝试在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;
}
}

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

添加代码前的主页

添加代码前的类别页面

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

带有修改代码的类别页面(“Ver más”表示“查看详细信息”)

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

添加代码时的主页 - 按钮消失

谁能帮助我在这里错过什么?

php html wordpress woocommerce 产品

评论


答:

0赞 LoicTheAztec 11/10/2023 #1

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

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

代码位于子主题的函数.php文件中(或插件中)。经过测试并有效。

评论

0赞 Alex Dubois 11/10/2023
谢谢你,这才成功。一个问题。我的“实体”产品不是外部链接,现在不显示任何按钮。我需要在代码中添加什么才能在这些代码上也有一个“查看更多”按钮?您可以在礼品卡部分看到我在 www.faceup-romanorte.com 上的意思。此外,按钮未对齐。当我选择元素或对齐按钮时,它确实对齐它们,但随后它们是全宽的。有没有办法将它们对齐到底部,但不能将它们扩展到完全宽度?