Woocommerce replace the product page link in the cart url

I am using this shortcode to display products [product_ category category = "extras" orderby = "date"].

Variable products show "select options" and individual products show "add to cart". I was able to change the text of both to say "View Product".

Now the problem is that I need to change the url of those who used to say "add to cart" because they are not linking to the product page but to "Add to cart".

I know I can edit the woocommerce template, but I would need this as a function to be added to the function.php function

I don't need any button, just replacing the url.

So the goal is: Replace / redirect the Add to Cart link to link to the product page (only in a loop, obviously not on the product page).

Can anyone please help?

+3


source to share


4 answers


If anyone decides to change the woocommerce file (in a child theme, of course!).

In file: /loop/add-to-cart.php

Edit:



global $product;

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        esc_attr( $product->product_type ),
        esc_html( $product->add_to_cart_text() )
    ),
$product );

      

To:

global $product;

if ( $product->product_type == "simple" ) {
    $simpleURL = get_permalink();
    $simpleLabel =  "View Product";  // BUTTON LABEL HERE
} else {
    $simpleURL =  $product->add_to_cart_url();  
    $simpleLabel = $product->add_to_cart_text();
};

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $simpleURL ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        esc_attr( $product->product_type ),
        esc_html( $simpleLabel )
    ),
$product );

      

+2


source


I believe you can turn off the AJAX functionality for add to cart in your WooCommerce settings.

If this is somehow unsatisfactory, you can take a look at the template loop/add-to-cart.php

. The link to add to cart can be filtered. If you look at add-to-cart.js

, you can see that the AJAX add to cart function is triggered for any link with a class add_to_cart button

and only works for buttons with a class product_type_simple

.... i.e .: for simple products only, Depending on your styles, you can either remove product type class, or a class add_to_cart_button

from a link. In the example below, I have removed the class add_to_cart_button

.



add_filter( 'woocommerce_loop_add_to_cart_link', 'so_26247988_add_to_cart_link', 10, 2 );
function so_26247988_add_to_cart_link( $link, $product ){
    $link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->product_type ),
        esc_html( $product->add_to_cart_text() )
    )
    return $link;
}

      

+1


source


In the funtions.php of your theme add this code:

/*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 '<p style="text-align:center;margin-top:10px;">';
            $currentlang = get_bloginfo('language');
            //for multilanguage
            if($currentlang=="en-GB"){
                echo do_shortcode('<a  href="'.$link.'" class="button addtocartbutton">View Product</a>');
            } elseif($currentlang=="fr-FR"){
                echo do_shortcode('<a  href="'.$link.'" class="button addtocartbutton">Voir le produit</a>');
            }else {
               echo do_shortcode('<a  href="'.$link.'" class="button addtocartbutton">Ver Producto</a>');   
            }
            echo '</p>';
        }

      

+1


source


just copy the below code and paste the functions.php file which is in your child theme. tastes like Woocommerce 3.3.4

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 = __("View product", "woocommerce");
       $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}

      

-1


source







All Articles