Home > Mobile >  Change Button Link depending of site in Woocommerce Template
Change Button Link depending of site in Woocommerce Template

Time:11-08

for some reasons I want to change the links from “add to cart” buttons. So when on archive-product.php the link of this button should NOT got to the add-to-cart page but to the single-product page. (single-product.php)

When on single-product.php the button should link to another custom page I’ll define but also not to the add-to-cart page. (Which I don’t need for this use-case)

I realize, that this must be a conditional filter in functions.php – but I have no clue of how to make this.

I managed to change the Button texts depending on the page I am. Like:

function woocommerce_custom_single_add_to_cart_text() {
    return __( 'Produkt verhindern', 'woocommerce' ); 
}


add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );  
function woocommerce_custom_product_add_to_cart_text() {
    return __( 'Mehr erfahren', 'woocommerce' );
}

CodePudding user response:

To modify add to cart link to single product link in Archive page, please refer below code:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product  ) {
    $button_text = __("Add to cart", "woocommerce");
    $button = '<a  href="' . $product->get_permalink() . '">' . $button_text . '</a>';

    return $button;
}

To modify add to cart link to global another redirect link then below is the code:

remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);

add_action('woocommerce_single_product_summary', 'custom_single_add_to_cart', 30);
function custom_single_add_to_cart()
{

 $button_text = __("Add to cart", "woocommerce");
    echo '<a  href="global_link_for_all_products">' . $button_text . '</a>';
}

Thanks

  • Related