Changing Woocommerce "Add to cart" button to "view product" button

时间:2018-03-22 23:51:02

标签: php wordpress button woocommerce product

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]');
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

使用此代码将默认的“添加到购物车”按钮替换为链接到单个产品页面的“更多信息”(或您喜欢的任何内容)。

// First, remove Add to Cart Button

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

// Second, add View Product Button

add_action( 'woocommerce_after_shop_loop_item', 'shop_view_product_button', 10);
function shop_view_product_button() {
global $product;
$link = $product->get_permalink();
echo '<a href="' . $link . '" class="button addtocartbutton">View Product</a>';
}

您可以在活动子主题(或主主题)functions.php文件的最底部添加此PHP代码段。 Code Source