更改Woocommerce的“添加到购物车"按钮以“查看产品"按钮 [英] Changing Woocommerce "Add to cart" button to "view product" button

查看:73
本文介绍了更改Woocommerce的“添加到购物车"按钮以“查看产品"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将woocommerce按钮文本从添加到购物车"更改为阅读更多"并重定向它,以便单击按钮将用户带到单独的产品页面.到目前为止,该链接有效,但是当我需要它说阅读更多"时,按钮上的所有文本都为按钮".我将代码放在下面,任何人都可以告诉我问题出在哪里.

Trying to change the woocommerce button text from "add to cart" to "read more" and redirect it so that clicking the button takes the user to the individual product page. So far, the link works but all the text on the button says is "Button" when I need it to say "Read More". I'll place the code below, can anyone please tell me what the problem is.

/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */

function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');



/*STEP 2 -ADD NEW BUTTON THAT LINKS TO PRODUCT PAGE FOR EACH PRODUCT */

add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo do_shortcode('<br>[button link="' . esc_attr($link) . '"]Read More[/button]');
}

推荐答案

尝试使用此替代方法,该方法将替换为添加到购物车"按钮,并替换为商店"和存档"页面中产品的链接按钮

Try this alternative that will replace add to cart button by a linked button to the product in Shop and archives pages

// Replace add to cart button by a linked button to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Not needed for variable products
    if( $product->is_type( 'variable' ) ) return $button;

    // Button text here
    $button_text = __( "View product", "woocommerce" );

    return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}

代码进入您的活动子主题(或主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or theme). Tested and works.

这篇关于更改Woocommerce的“添加到购物车"按钮以“查看产品"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆